https://github.com/joway/pond
Generic Object Pool for Golang.
https://github.com/joway/pond
Last synced: about 1 year ago
JSON representation
Generic Object Pool for Golang.
- Host: GitHub
- URL: https://github.com/joway/pond
- Owner: joway
- License: other
- Created: 2021-01-21T04:58:54.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-12T08:36:31.000Z (almost 5 years ago)
- Last Synced: 2025-03-25T07:51:09.311Z (about 1 year ago)
- Language: Go
- Size: 29.3 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pond

[](https://goreportcard.com/report/github.com/joway/pond)
[](https://codecov.io/gh/joway/pond)
[](https://circleci.com/gh/joway/pond)
Generic Object Pool for Golang.
It has been used in production and serve millions of QPS.
## Adopters
- [Hive](https://github.com/joway/hive): A high-efficiency Goroutine Pool.
## Get Started
```go
type conn struct {
addr string
}
ctx := context.Background()
cfg := pond.NewDefaultConfig()
//required
cfg.ObjectCreateFactory = func (ctx context.Context) (interface{}, error) {
return &conn{addr: "127.0.0.1"}, nil
}
//optional
cfg.ObjectValidateFactory = func (ctx context.Context, object interface{}) bool {
c := object.(*conn)
return c.addr != ""
}
//optional
cfg.ObjectDestroyFactory = func (ctx context.Context, object interface{}) error {
c := object.(*conn)
c.addr = ""
return nil
}
p, err := pond.New(cfg)
if err != nil {
log.Fatal(err)
}
obj, err := p.BorrowObject(ctx)
if err != nil {
log.Fatal(err)
}
defer p.ReturnObject(ctx, obj)
fmt.Printf("get conn: %v\n", obj.(*conn).addr)
```
## Configuration
| Option | Default | Description |
| ------------------------------|:--------------:| :------------|
| MaxSize | 10 |The capacity of the pool. If MaxSize <= 0, no capacity limit.|
| MinIdle | 0 |The minimum size of the idle objects.|
| MaxIdle | 10 |The maximal size of the idle objects. Idle objects exceeding MaxIdle will be evicted.|
| MinIdleTime | 5m |The minimum time that idle object should be reserved.|
| Nonblocking | false |The blocking policy. If true, it will return ErrPoolExhausted when pool is exhausted.|
| AutoEvict | true |Enable auto evict idle objects. When true, pool will create a goroutine to start a evictor.|
| EvictInterval | 30s |The interval between evict.|
| MaxValidateAttempts | 1 |The maximal attempts to validate object.|
| ObjectCreateFactory | **required** |The factory of creating object.|
| ObjectValidateFactory | none |The factory of validating object.|
| ObjectDestroyFactory | none |The factory of destroying object.|
## Benchmark
Compare with:
- [go-commons-pool](https://github.com/jolestar/go-commons-pool):
```text
BenchmarkPool-8 3116902 358 ns/op 71 B/op 2 allocs/op
BenchmarkPoolWithConcurrent-8 3683365 326 ns/op 0 B/op 0 allocs/op
BenchmarkCommonsPool-8 1828080 669 ns/op 103 B/op 3 allocs/op
BenchmarkCommonsPoolWithConcurrent-8 1715344 703 ns/op 32 B/op 1 allocs/op
```