vconnpool

package module
v3.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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 Pool struct {                                                          // 连接池
    Dialer                                                                      // 拨号
    ResolveAddr func(network, address string) (net.Addr, error)                 // 拨号地址变更
    IdeConn     int                                                             // 空闲连接数,0为不复用连接
    MaxConn     int                                                             // 最大连接数,0为无限制连接
    IdeTimeout  time.Duration                                                   // 空闲自动超时,0为不超时
}
    func (T *Pool) Dial(network, address string) (net.Conn, error)         // 拨号,如果 address 参数是host域名,.Get(...)将无法读取到连接。请再次使用 .Dial(...) 来读取。
    func (T *Pool) DialContext(ctx context.Context, network, address string) (net.Conn, error) //拨号(支持上下文),如果 address 参数是host域名,.Get(...)将无法读取到连接。请再次使用 .Dial(...) 来读取。
    func (T *Pool) Add(conn net.Conn) error                                // 增加连接
    func (T *Pool) Put(conn net.Conn, addr net.Addr) error                 // 增加连接,支持 addr
    func (T *Pool) Get(addr net.Addr) (net.Conn, error)                    // 读取连接,读取出来的连接不会自动回收,需要调用 .Add(...) 收入
    func (T *Pool) ConnNum() int                                           // 当前连接数量
    func (T *Pool) ConnNumIde(network, address string) int                 // 当前连接数量(空闲),不是实时的空闲连接数,存在多线程!
    func (T *Pool) CloseIdleConnections()                                  // 关闭空闲连接
    func (T *Pool) Close() error                                           // 关闭连接池

Documentation

Overview

Package vconnpool 提供高性能、可复用的网络连接池

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClose         = errors.New("vconnpool: the connection is closed")
	ErrConnPoolClosed    = errors.New("vconnpool: the connection pool has been closed")
	ErrConnRAWRead       = errors.New("vconnpool: the original connection cannot be read repeatedly or is closed") // 错误信息更明确
	ErrConnNotAvailable  = errors.New("vconnpool: no available connection in the pool")
	ErrConnPoolMax       = errors.New("vconnpool: the number of connections has reached the maximum limit")
	ErrConnIdleMax       = errors.New("vconnpool: the number of idle connections has reached the maximum")
	ErrConnAlreadyExists = errors.New("vconnpool: the connection already exists in the idle pool")
)
View Source
var PriorityContextKey = &contextKey{"priority"}

PriorityContextKey 上下文键,用于标记是否优先创建新连接(不使用连接池)

Functions

func ResolveAddr

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

Types

type Addr

type Addr struct {
	Name string
	Net  string
}

func (*Addr) Network

func (a *Addr) Network() string

func (*Addr) String

func (a *Addr) String() string

type Conn

type Conn interface {
	net.Conn
	Discard()
	IsReuseConn() bool
	RawConn() net.Conn // 修正签名以返回错误
}

Conn 对外暴露的连接接口

type Dialer

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

Dialer 接口定义

type Pool added in v3.0.2

type Pool struct {
	Dialer      Dialer
	ResolveAddr func(network, address string) (net.Addr, error)
	IdleConn    int
	IdleTimeout time.Duration
	MaxConn     int
	// contains filtered or unexported fields
}

func (*Pool) Add added in v3.0.2

func (p *Pool) Add(conn net.Conn) error

func (*Pool) Close added in v3.0.2

func (p *Pool) Close() error

func (*Pool) CloseIdleConnection added in v3.0.3

func (p *Pool) CloseIdleConnection(addr net.Addr)

CloseIdleConnection 关闭指定地址的空闲连接

func (*Pool) CloseIdleConnections added in v3.0.2

func (p *Pool) CloseIdleConnections()

CloseIdleConnections 关闭所有地址的空闲连接

func (*Pool) Dial added in v3.0.2

func (p *Pool) Dial(network, address string) (Conn, error)

func (*Pool) DialContext added in v3.0.2

func (p *Pool) DialContext(ctx context.Context, network, address string) (Conn, error)

func (*Pool) Get added in v3.0.2

func (p *Pool) Get(addr net.Addr) (conn net.Conn, err error)

Get 从连接池获取指定地址的连接(不创建新连接)

func (*Pool) Num added in v3.1.0

func (p *Pool) Num() int

func (*Pool) NumConn added in v3.1.0

func (p *Pool) NumConn(addr net.Addr) int

NumConn 当前地址连接数量

func (*Pool) NumIdle added in v3.1.0

func (p *Pool) NumIdle(addr net.Addr) int

NumIdle 当前空闲连接数量

func (*Pool) Put added in v3.0.2

func (p *Pool) Put(conn net.Conn, addr net.Addr) error

func (*Pool) WaitConnIdleGeq added in v3.2.0

func (p *Pool) WaitConnIdleGeq(addr net.Addr, l int) <-chan bool

func (*Pool) WaitConnIdleLeq added in v3.2.0

func (p *Pool) WaitConnIdleLeq(addr net.Addr, l int) <-chan bool

func (*Pool) WaitConnNumGeq added in v3.2.0

func (p *Pool) WaitConnNumGeq(addr net.Addr, l int) <-chan bool

func (*Pool) WaitConnNumLeq added in v3.2.0

func (p *Pool) WaitConnNumLeq(addr net.Addr, l int) <-chan bool

Jump to

Keyboard shortcuts

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