pool

package
v0.0.0-...-66ba032 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2018 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

一個 tcp 連接池

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	//tcp連接
	net.Conn
	// contains filtered or unexported fields
}

包裝的 net.Conn

func (*Conn) Close

func (c *Conn) Close() error

close 之後 Put 會 連接池 連接池 將 釋放 此連接的資源

func (*Conn) Get

func (c *Conn) Get() net.Conn

返回 連接池 模板 Conect 創建的原始 net.Conn

type IPool

type IPool interface {
	//從 連接池 中 獲取一個連接
	Get() (*Conn, error)
	//將 連接 返回給 連接池
	Put(*Conn)

	//釋放 連接池 所有資源 此後 連接池不能再被使用
	Close()
}

連接池 接口

func NewPool

func NewPool(t IPoolTemplate, sum int) (IPool, error)

創建一個 連接池 sum 初始連接 數量

type IPoolTemplate

type IPoolTemplate interface {
	//連接池 將使用此 函數 連接 服務器
	Conect() (net.Conn, error)
	//連接池 將使用此 函數 斷開 連接
	Close(net.Conn) error
	//連接池 將使用此 函數 ping 服務器
	Ping(*Conn) error

	//連接池 中的連接 多久自動執行一次 ping
	//返回 0 將不會執行ping
	//返回值 必須 恆定
	PingInterval() time.Duration

	//多久 未活動的 連接 會被 釋放
	//返回 0 永不超時
	//返回值 必須 恆定
	Timeout() time.Duration

	//連接池 執行縮擴容 操作 的最小 執行週期
	MinInterval() time.Duration
	//連接池 多久執行一次自動 縮擴容
	//返回 0 不執行
	//返回值 必須 恆定
	Interval() time.Duration

	//是否需要 縮擴容
	//返回 0 不需要擴容
	//返回 >0 擴容 |n|
	//返回 <0 縮容 |n|
	Resize(use, free int) int
}

連接池 模板 定義了 連接池 工作細節

type PoolTemplate

type PoolTemplate struct {
	// contains filtered or unexported fields
}

默認的 連接池 模板

func NewPoolTemplate

func NewPoolTemplate(addr string,
	timeout time.Duration,
	min int,
	max int,
) *PoolTemplate

創建一個 默認的 連接池 模板

func (*PoolTemplate) Close

func (p *PoolTemplate) Close(c net.Conn) error

func (*PoolTemplate) Conect

func (p *PoolTemplate) Conect() (net.Conn, error)

func (*PoolTemplate) Interval

func (p *PoolTemplate) Interval() time.Duration

func (*PoolTemplate) MinInterval

func (p *PoolTemplate) MinInterval() time.Duration

func (*PoolTemplate) Ping

func (p *PoolTemplate) Ping(*Conn) error

func (*PoolTemplate) PingInterval

func (p *PoolTemplate) PingInterval() time.Duration

func (*PoolTemplate) Resize

func (p *PoolTemplate) Resize(use, free int) int

func (*PoolTemplate) Timeout

func (p *PoolTemplate) Timeout() time.Duration

Jump to

Keyboard shortcuts

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