Skip to content

Commit

Permalink
Merge pull request #31 from silenceper/f-readme
Browse files Browse the repository at this point in the history
change readme
  • Loading branch information
silenceper authored Oct 11, 2020
2 parents a659d81 + 54c7829 commit 5571f6c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go:
- 1.11.x
before_script:
- GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/)
- go get github.com/golang/lint/golint
- go get golang.org/x/lint

script:
- go test -v -race ./...
Expand Down
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
# pool
[![GoDoc](http://godoc.org/github.com/silenceper/pool?status.svg)](http://godoc.org/github.com/silenceper/pool)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/silenceper/pool)](https://pkg.go.dev/github.com/silenceper/pool)
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/pool)](https://goreportcard.com/report/github.com/silenceper/pool)

Golang 实现的连接池

[中文文档](./README_ZH_CN.md)

## 功能:
A golang universal network connection pool.

- 连接池中连接类型为`interface{}`,使得更加通用
- 连接的最大空闲时间,超时的连接将关闭丢弃,可避免空闲时连接自动失效问题
- 支持用户设定 ping 方法,检查连接的连通性,无效的连接将丢弃
- 使用channel处理池中的连接,高效
## Feature:

## 基本用法
- The connection type in the connection pool is `interface{}`, making it more versatile
- The connection supports setting the maximum idle time, the timeout connection will be closed and discarded, which can avoid the problem of automatic connection failure when idle

- Support user setting `ping` method, used to check the connectivity of connection, invalid connection will be discarded
- When the connection pool is full, support for connection waiting (like the go db connection pool)

## Basic Usage:

```go

//factory 创建连接的方法
//factory Specify the method to create the connection
factory := func() (interface{}, error) { return net.Dial("tcp", "127.0.0.1:4000") }

//close 关闭连接的方法
//close Specify the method to close the connection
close := func(v interface{}) error { return v.(net.Conn).Close() }

//ping 检测连接的方法
//ping Specify the method to detect whether the connection is invalid
//ping := func(v interface{}) error { return nil }

//创建一个连接池: 初始化5,最大空闲连接是20,最大并发连接30
//Create a connection pool: Initialize the number of connections to 5, the maximum idle connection is 20, and the maximum concurrent connection is 30
poolConfig := &pool.Config{
InitialCap: 5,//资源池初始连接数
MaxIdle: 20,//最大空闲连接数
MaxCap: 30,//最大并发连接数
InitialCap: 5,
MaxIdle: 20,
MaxCap: 30,
Factory: factory,
Close: close,
//Ping: ping,
//连接最大空闲时间,超过该时间的连接 将会关闭,可避免空闲时连接EOF,自动失效的问题
//The maximum idle time of the connection, the connection exceeding this time will be closed, which can avoid the problem of automatic failure when connecting to EOF when idle
IdleTimeout: 15 * time.Second,
}
p, err := pool.NewChannelPool(poolConfig)
if err != nil {
fmt.Println("err=", err)
}

//从连接池中取得一个连接
//Get a connection from the connection pool
v, err := p.Get()

//do something
//conn=v.(net.Conn)

//将连接放回连接池中
//Put the connection back into the connection pool, when the connection is no longer in use
p.Put(v)

//释放连接池中的所有连接
//Release all connections in the connection pool, when resources need to be destroyed
p.Release()

//查看当前连接中的数量
//View the number of connections in the current connection pool
current := p.Len()


```


#### :
该连接池参考 [https://github.com/fatih/pool](https://github.com/fatih/pool) 实现,改变以及增加原有的一些功能。
#### Remarks:
The connection pool implementation refers to pool [https://github.com/fatih/pool](https://github.com/fatih/pool) , thanks.


## License
Expand Down
69 changes: 69 additions & 0 deletions README_ZH_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# pool
[![PkgGoDev](https://pkg.go.dev/badge/github.com/silenceper/pool)](https://pkg.go.dev/github.com/silenceper/pool)
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/pool)](https://goreportcard.com/report/github.com/silenceper/pool)

Golang 实现的连接池


## 功能:

- 连接池中连接类型为`interface{}`,使得更加通用
- 连接的最大空闲时间,超时的连接将关闭丢弃,可避免空闲时连接自动失效问题
- 支持用户设定 ping 方法,检查连接的连通性,无效的连接将丢弃
- 使用channel处理池中的连接,高效

## 基本用法

```go

//factory 创建连接的方法
factory := func() (interface{}, error) { return net.Dial("tcp", "127.0.0.1:4000") }

//close 关闭连接的方法
close := func(v interface{}) error { return v.(net.Conn).Close() }

//ping 检测连接的方法
//ping := func(v interface{}) error { return nil }

//创建一个连接池: 初始化5,最大空闲连接是20,最大并发连接30
poolConfig := &pool.Config{
InitialCap: 5,//资源池初始连接数
MaxIdle: 20,//最大空闲连接数
MaxCap: 30,//最大并发连接数
Factory: factory,
Close: close,
//Ping: ping,
//连接最大空闲时间,超过该时间的连接 将会关闭,可避免空闲时连接EOF,自动失效的问题
IdleTimeout: 15 * time.Second,
}
p, err := pool.NewChannelPool(poolConfig)
if err != nil {
fmt.Println("err=", err)
}

//从连接池中取得一个连接
v, err := p.Get()

//do something
//conn=v.(net.Conn)

//将连接放回连接池中
p.Put(v)

//释放连接池中的所有连接
p.Release()

//查看当前连接中的数量
current := p.Len()


```


#### 注:
该连接池参考 [https://github.com/fatih/pool](https://github.com/fatih/pool) 实现,改变以及增加原有的一些功能。


## License

The MIT License (MIT) - see LICENSE for more details
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit 5571f6c

Please sign in to comment.