vconnpool

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 10

README

vconnpool Build Status

golang vconnpool,可以TCP连接复用,使用方法和 net.Dialer 接口是相同。

列表:

type Dialer interface {                                                 // net.Dialer 接口
    Dial(network, address string) (net.Conn, error)                             // 拨号
    DialContext(ctx context.Context, network, address string) (net.Conn, error) // 拨号(支持上下文)
}
type Conn interface{                                                    // 连接接口
    net.Conn                                                                    // net连接接口
    Discard() error                                                             // 废弃(这条连接不再回收)
    IsReuseConn() bool                                                          // 判断这条连接是否是从池中读取出来的
    RawConn() net.Conn                                                          // 原始连接,这个连接使用 Close 关闭后,不会回收
}
type ConnPool struct {                                                          // 连接池
    Dialer                                                                 // 拨号
    Host        func(oldAddress string) (newAddress string)                     // 拨号地址变更
    IdeConn     int                                                             // 空闲连接数,0为不复用连接
    MaxConn     int                                                             // 最大连接数,0为无限制连接
    IdeTimeout  time.Duration                                                   // 空闲自动超时,0为不超时
}
    func (T *ConnPool) Dial(network, address string) (net.Conn, error)         // 拨号,如果 address 参数是host域名,.Get(...)将无法读取到连接。请再次使用 .Dial(...) 来读取。
    func (T *ConnPool) DialContext(ctx context.Context, network, address string) (net.Conn, error) //拨号(支持上下文),如果 address 参数是host域名,.Get(...)将无法读取到连接。请再次使用 .Dial(...) 来读取。
    func (T *ConnPool) Add(conn net.Conn) error                                // 增加连接
    func (T *ConnPool) Put(conn net.Conn, addr net.Addr) error                 // 增加连接,支持 addr
    func (T *ConnPool) Get(addr net.Addr) (net.Conn, error)                    // 读取连接,读取出来的连接不会自动回收,需要调用 .Add(...) 收入
    func (T *ConnPool) ConnNum() int                                           // 当前连接数量
    func (T *ConnPool) ConnNumIde(network, address string) int                 // 当前连接数量(空闲),不是实时的空闲连接数,存在多线程!
    func (T *ConnPool) CloseIdleConnections()                                  // 关闭空闲连接
    func (T *ConnPool) Close() error                                           // 关闭连接池

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnPoolMax = errors.New("vconnpool: the number of connections in the connection pool has reached the maximum limit")

	ErrConnNotAvailable = errors.New("vconnpool: no connections available in the pool")
	ErrPoolFull         = errors.New("vconnpool: the number of idle connections has reached the maximum")
)
View Source
var PriorityContextKey = &contextKey{"priority"}

Functions

func ResolveAddr added in v2.1.9

func ResolveAddr(network, address string) (net.Addr, error)

Types

type Conn

type Conn interface {
	net.Conn                            // 连接
	Discard() error                     // 废弃(这条连接不再回收)
	IsReuseConn() bool                  // 判断这条连接是否是从池中读取出来的
	RawConn() net.Conn                  // 原始连接,这个连接使用 Close 关闭后,不会回收
	RawConnFull([]byte) (net.Conn, int) // 原始连接,这个连接使用 Close 关闭后,不会回收
}

Conn 连接接口,包含了 net.Conn

type ConnPool

type ConnPool struct {
	Dialer                                                      // 拨号
	ResolveAddr func(network, address string) (net.Addr, error) // 拨号地址变更
	IdeConn     int                                             // 空闲连接数,0为不支持连接入池
	IdeTimeout  time.Duration                                   // 空闲自动超时,0为不超时
	MaxConn     int                                             // 最大连接数,0为无限制连接
	// contains filtered or unexported fields
}

ConnPool 连接池

func (*ConnPool) Add

func (T *ConnPool) Add(conn net.Conn) error

Add 增加一个连接到池中,适用于 Dial 的连接。默认使用 RemoteAddr 作为 key 存放在池中。 如果你的需求特殊的,请使用 Put 方法。

conn net.Conn   连接
error           错误

func (*ConnPool) Close

func (cp *ConnPool) Close() error

Close 关闭连接池

func (*ConnPool) CloseIdleConnections

func (T *ConnPool) CloseIdleConnections()

CloseIdleConnections 关闭空闲连接池

func (*ConnPool) ConnNum

func (T *ConnPool) ConnNum() int

ConnNum 当前可用连接数量

int     数量

func (*ConnPool) ConnNumIde

func (T *ConnPool) ConnNumIde(network, address string) int

ConnNumIde 当前空闲连接数量。这不是实时的空闲连接数量。 入池后读取,得到真实数量。出池后读取,得到的不真实数量,因为存在多线程处理。

int     数量

func (*ConnPool) Dial

func (T *ConnPool) Dial(network, address string) (net.Conn, error)

Dial 见 DialContext

network string      连接类型
address string      连接地址
net.Conn            连接
error               错误

func (*ConnPool) DialContext

func (T *ConnPool) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext 拨号,如果ctx 携带键值是(priority=true),是创建新连接,否则从池中读取。 注意:远程地址支持 host 或 ip,一个 host 会有多个 ip 地址,所以无法用 host 的 ip 做为存储地址。 DialContext 支持 hsot 和 ip 读取或创建连接。 而.Get 仅支持 ip 读取池中连接。 DialContext 创建的连接,调用 Close 关闭后,自动收回。

ctx context.Context 上下文
network string      连接类型
address string      连接地址
net.Conn            连接
error               错误

func (*ConnPool) Get

func (T *ConnPool) Get(addr net.Addr) (conn net.Conn, err error)

Get 从池中读取一条连接。读取出来的连接不会自动回收,如果你.Close() 是真的关闭连接,不是回收。 注意:池中有可用连接数量,而无法读出连接。原因是连接存在后台数据,被判断为不完整。

addr net.Addr   地址,为远程地址RemoteAddr
conn net.Conn	连接,源是 *vconn.Conn 类型
error           错误

func (*ConnPool) Put added in v2.1.0

func (T *ConnPool) Put(conn net.Conn, addr net.Addr) error

Put 增加一个连接到池中,适用于 Dial 和 listen 的连接。 Dial 连接使用RemoteAddr,listen 连接使用LocalAddr 为做 addr

conn net.Conn   连接
addr net.Addr	地址,作为池的 key 存放
error           错误

type Dialer

type Dialer interface {
	Dial(network, address string) (net.Conn, error)
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer 是 net.Dialer 接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL