a generic object pool for golang
The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool.
Configuration option table, more detail description see ObjectPoolConfig
| Option | Default | Description |
|---|---|---|
| LIFO | true | If pool is LIFO (last in, first out) |
| MaxTotal | 8 | The cap of pool |
| MaxIdle | 8 | Max "idle" instances in the pool |
| MinIdle | 0 | Min "idle" instances in the pool |
| TestOnCreate | false | Validate when object is created |
| TestOnBorrow | false | Validate when object is borrowed |
| TestOnReturn | false | Validate when object is returned |
| TestWhileIdle | false | Validate when object is idle, see TimeBetweenEvictionRuns |
| BlockWhenExhausted | true | Whether to block when the pool is exhausted |
| MinEvictableIdleTime | 30m | Eviction configuration,see DefaultEvictionPolicy |
| SoftMinEvictableIdleTime | math.MaxInt64 | Eviction configuration,see DefaultEvictionPolicy |
| NumTestsPerEvictionRun | 3 | The maximum number of objects to examine during each run evictor goroutine |
| TimeBetweenEvictionRuns | 0 | The number of milliseconds to sleep between runs of the evictor goroutine, less than 1 mean not run |
…
…
For more examples please see pool_test.go and example_simple_test.go, example_customFactory_test.go.
PooledObjectFactory.MakeObject must return a pointer, not value. The following code will complain error.
p := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return "hello", nil
}))
obj, _ := p.BorrowObject()
p.ReturnObject(obj)
The right way is:
p := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
s := "hello"
return &s, nil
}))
For more examples please see example_simple_test.go.
The results of running the pool_perf_test is almost equal to the java version PerformanceTest
go test --perf=true
Go Commons Pool is available under the Apache License, Version 2.0.
No open issues yet, or sync has not completed.