-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from silenceper/f-readme
change readme
- Loading branch information
Showing
4 changed files
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |