mirror of
https://github.com/hanzo-ds/native.git
synced 2026-10-30 20:00:34 +00:00
Module path, package names, and identifiers de-branded to native/proto/compress/ pool/tracing. Server wire-protocol strings preserved. Vendored codec surface used by hanzo-ds/go (proto + compress).
58 lines
913 B
Go
58 lines
913 B
Go
package pool
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/puddle/v2"
|
|
|
|
"github.com/hanzo-ds/native"
|
|
)
|
|
|
|
// Client is an acquired *native.Client from a Pool.
|
|
type Client struct {
|
|
res *puddle.Resource[*connResource]
|
|
p *Pool
|
|
}
|
|
|
|
// Release returns client to the pool.
|
|
func (c *Client) Release() {
|
|
if c.res == nil {
|
|
return
|
|
}
|
|
|
|
client := c.client()
|
|
|
|
if client.IsClosed() || time.Since(c.res.CreationTime()) > c.p.options.MaxConnLifetime {
|
|
c.res.Destroy()
|
|
return
|
|
}
|
|
|
|
c.res.Release()
|
|
}
|
|
|
|
func (c *Client) Do(ctx context.Context, q native.Query) (err error) {
|
|
return c.client().Do(ctx, q)
|
|
}
|
|
|
|
func (c *Client) Ping(ctx context.Context) error {
|
|
return c.client().Ping(ctx)
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
var err error
|
|
|
|
client := c.client()
|
|
if !client.IsClosed() {
|
|
err = client.Close()
|
|
}
|
|
|
|
c.res.Destroy()
|
|
|
|
return err
|
|
}
|
|
|
|
func (c *Client) client() *native.Client {
|
|
return c.res.Value().client
|
|
}
|