vconnpool

package module
v3.2.11 Latest Latest
Warning

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

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

README

vconnpool

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

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

Add 适合存放客户端发起的连接

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) (net.Conn, error)

Get 从连接池获取指定地址的连接

func (*Pool) GetFull added in v3.2.5

func (p *Pool) GetFull(addr net.Addr) (conn net.Conn, b []byte, err error)

GetFull 从连接池获取指定地址的连接,如果在空闲时收到对端发来的数据,会一并返回给调用者

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

Put 适合存放客户端发起的连接,支持指定的地址

func (*Pool) PutFull added in v3.2.5

func (p *Pool) PutFull(conn net.Conn, addr net.Addr, timeout time.Duration, bs int) error

PutFull 适合存放客户端/服务端的连接,支持指定地址、超时和缓存大小

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