gopool

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 4 Imported by: 3

README

gopool

简单且高效的通用连接池。 任何struct只需要实现IClient接口即可享受本连接池带来的高效率。

性能测试
测试环境
MacBook Pro (13-inch, Mid 2012) 
cpu 2.5 GHz Intel Core i5
内存 8 GB 1600 MHz DDR3

goos: darwin
goarch: amd64
pkg: github.com/seefan/gopool
5000000	       262 ns/op
5000000	       265 ns/op
5000000	       267 ns/op
5000000	       271 ns/op
5000000	       263 ns/op
3000000	       454 ns/op
3000000	       440 ns/op
3000000	       445 ns/op
3000000	       438 ns/op
3000000	       429 ns/op
PASS

Documentation

Index

Constants

View Source
const (
	//连接池状态:创建
	PoolInit = 0
	//连接池状态:运行
	PoolStart = 1
	//连接池状态:关闭
	PoolStop = -1
	//连接池状态:重启
	PoolReStart = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type IClient

type IClient interface {
	//打开连接
	//
	// 返回,error。如果连接到服务器时出错,就返回错误信息,否则返回nil
	Start() error
	//关闭连接
	//
	// 返回,error。如果关闭连接时出错,就返回错误信息,否则返回nil
	Close() error
	//是否打开
	//
	// 返回,bool。如果已连接到服务器,就返回true。
	IsOpen() bool
	//检查连接状态
	//
	// 返回,bool。如果无法访问服务器,就返回false。
	Ping() bool
}

连接接口

连接池内的连接结构只要实现这个接口就可以嵌入池内使用

type Pool

type Pool struct {

	//create new Closed
	NewClient func() IClient
	//状态
	Status int
	//config
	//获取连接超时时间,单位为秒。默认值: 5
	GetClientTimeout int
	//最大连接池个数。默认值: 20
	MaxPoolSize int
	//最小连接池数。默认值: 5
	MinPoolSize int
	//当连接池中的连接耗尽的时候一次同时获取的连接数。默认值: 5
	AcquireIncrement int
	//最大等待数目,当连接池满后,新建连接将等待池中连接释放后才可以继续,本值限制最大等待的数量,超过本值后将抛出异常。默认值: 1000
	MaxWaitSize int
	//连接池内缓存的连接状态检查时间隔,单位为秒。默认值: 5
	HealthSecond int
	//连接空闲时间,超过这个时间可能会被回收,单位为秒。默认值:60
	IdleTime int
	// contains filtered or unexported fields
}

连接池结构

func NewPool

func NewPool() *Pool

创建新的连接池

返回 Pool

func (*Pool) Close

func (p *Pool) Close()

关闭连接池

func (*Pool) Get

func (p *Pool) Get() (client *PooledClient, err error)

在连接池取一个新连接

返回 client,一个新的连接
返回 err,可能的错误,操作成功返回 nil

func (*Pool) Info

func (p *Pool) Info() string

返回连接池的状态信息

返回,string

func (*Pool) Set

func (p *Pool) Set(element *PooledClient)

归还连接到连接池

element 连接

func (*Pool) Start

func (p *Pool) Start() error

启动连接池

返回 err,可能的错误,操作成功返回 nil

type PooledClient

type PooledClient struct {

	//value
	Client IClient
	// contains filtered or unexported fields
}

缓存的连接

内部使用

type Slice

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

使用slice实现的池

func (*Slice) Append

func (s *Slice) Append(ai int) error

附加连接

ai int 附加的连接数 返回 error 可能的错误

func (*Slice) Close

func (s *Slice) Close()

关闭连接池

func (*Slice) Get

func (s *Slice) Get() (*PooledClient, error)

获取一个连接

返回 *PooledClient 缓存的连接 返回 error 可能的错误

func (*Slice) Info

func (s *Slice) Info(v ...interface{})

func (*Slice) Init

func (s *Slice) Init(ai, min, max int, p *Pool)

初始化连接池

ai int acquireIncrement min int minPoolSize max int maxPoolSize p *Pool 主连接池

func (*Slice) Set

func (s *Slice) Set(element *PooledClient)

回收连接

element *PooledClient 要回收的连接

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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