go-common-pool

最后更新于:2022-04-02 02:39:52

[TOC] > [github/go-common-poll](https://github.com/jolestar/go-commons-pool) ## 概述 Go Commons池是通用对象池 几个重要事件: - 一个对象加入idleObjects时需要进行(钝化)操作 - 一个对象在获取时需要进行(激活-->校验)操作 - 一个对象在归还时需要进行(校验-->钝化)操作 - 其中任何一步失败都需要销毁对象 ### 配置 | 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 | ## 实例 ### Use Simple Factory
main.go ``` import ( "context" "fmt" "strconv" "sync/atomic" "github.com/jolestar/go-commons-pool/v2" ) func Example_simple() { type myPoolObject struct { s string } v := uint64(0) factory := pool.NewPooledObjectFactorySimple( func(context.Context) (interface{}, error) { return &myPoolObject{ s: strconv.FormatUint(atomic.AddUint64(&v, 1), 10), }, nil }) ctx := context.Background() p := pool.NewObjectPoolWithDefaultConfig(ctx, factory) obj, err := p.BorrowObject(ctx) if err != nil { panic(err) } o := obj.(*myPoolObject) fmt.Println(o.s) err = p.ReturnObject(ctx, obj) if err != nil { panic(err) } // Output: 1 } ```

### Use Custom Factory
main.go ``` import ( "context" "fmt" "strconv" "sync/atomic" "github.com/jolestar/go-commons-pool/v2" ) type MyPoolObject struct { s string } type MyCustomFactory struct { v uint64 } func (f *MyCustomFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) { return pool.NewPooledObject( &MyPoolObject{ s: strconv.FormatUint(atomic.AddUint64(&f.v, 1), 10), }), nil } func (f *MyCustomFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error { // do destroy return nil } func (f *MyCustomFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool { // do validate return true } func (f *MyCustomFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error { // do activate return nil } func (f *MyCustomFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error { // do passivate return nil } func Example_customFactory() { ctx := context.Background() p := pool.NewObjectPoolWithDefaultConfig(ctx, &MyCustomFactory{}) p.Config.MaxTotal = 100 obj1, err := p.BorrowObject(ctx) if err != nil { panic(err) } o := obj1.(*MyPoolObject) fmt.Println(o.s) err = p.ReturnObject(ctx, obj1) if err != nil { panic(err) } // Output: 1 } ```

';