gtcp
模块提供了连接池的特性,由 gtcp.PoolConn
对象实现,连接池缓存固定存活时间为600秒,且内部实现了数据发送时的断开重连机制。连接池非常适合于频繁的短链接操作且连接并发量大的场景。我们接下来使用两个示例来演示一下连接池的作用。
使用方式:
import "github.com/gogf/gf/v2/net/gtcp"
接口文档:
https://pkg.go.dev/github.com/gogf/gf/v2/net/gtcp
type PoolConn
func NewPoolConn(addr string, timeout ...int) (*PoolConn, error)
func (c *PoolConn) Close() error
func (c *PoolConn) Recv(length int, retry ...Retry) ([]byte, error)
func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error)
func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error)
func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) ([]byte, error)
func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
func (c *PoolConn) Send(data []byte, retry ...Retry) error
func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error)
func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) error
func (c *PoolConn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
func (c *PoolConn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error)
func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
func (c *PoolConn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error