连接池基类
package generalpool import ( "context" "git.ichub.com/general/webcli120/goconfig/base/baseiface" "git.ichub.com/general/webcli120/goconfig/base/baseutils" "git.ichub.com/general/webcli120/goconfig/base/goutils" pool "github.com/jolestar/go-commons-pool" "reflect" "time" ) const maxtotal = 1000 const maxidle = 100 type GeneralObjectPool[T baseiface.IpoolObj] struct { CommonPool *pool.ObjectPool } // NewGeneralObjectPool 使用传入的上下文初始化连接池 func NewGeneralObjectPool[T baseiface.IpoolObj]() *GeneralObjectPool[T] { var msgPool = &GeneralObjectPool[T]{} return msgPool.init(context.Background()) } func (self *GeneralObjectPool[T]) init(ctx context.Context) *GeneralObjectPool[T] { PoolConfig := pool.NewDefaultPoolConfig() PoolConfig.MaxTotal = maxtotal PoolConfig.MaxIdle = maxidle WithAbandonedConfig := pool.NewDefaultAbandonedConfig() self.CommonPool = pool.NewObjectPoolWithAbandonedConfig(ctx, pool.NewPooledObjectFactorySimple( func(ctx context.Context) (interface{}, error) { return self.Link() }), PoolConfig, WithAbandonedConfig) return self } // Link 初始化连接 ,通过指针初始化结构体 func (self *GeneralObjectPool[T]) Link() (T, error) { var ptr T var objType = reflect.TypeOf(ptr) var value = reflect.New(objType.Elem()) return value.Interface().(T), nil } // BorrowObject 从连接池借用对象,增加了错误处理和重试逻辑 func (self *GeneralObjectPool[T]) BorrowObject(ctx context.Context) (T, error) { var inst T obj, err := self.CommonPool.BorrowObject(ctx) if err != nil { goutils.Error("连接池获取对象失败", err) // 尝试重试逻辑 time.Sleep(time.Second) // 简单示例,实际应用中可能需要更复杂的重试策略 return inst, err } defer func() { if err := self.CommonPool.ReturnObject(ctx, obj); err != nil { goutils.Error("返回对象到连接池失败", err) } }() inst = obj.(T) inst.Init() baseutils.IfProxy(inst) return inst, nil } // ReturnObject 交还对象到连接池 func (self *GeneralObjectPool[T]) ReturnObject(ctx context.Context, object T) { object.Shutdown() self.CommonPool.ReturnObject(ctx, object) }
连接池实例
package generalpool
import "git.ichub.com/general/webcli120/goconfig/base/basedto"
type IchubResultPool struct {
basedto.BaseEntitySingle
*GeneralObjectPool[*basedto.IchubResult]
}
func NewIchubResultPool() *IchubResultPool {
return &IchubResultPool{
GeneralObjectPool: NewGeneralObjectPool[*basedto.IchubResult](),
}
}
ichubresult实现
type IpoolObj interface { Init() Shutdown() }
func (this *IchubResult) Init() { this.Code = 200 this.Msg = "成功" this.Data = nil } func (this *IchubResult) Shutdown() { // this.Code = 200 // this.Msg = "成功" this.Data = nil }
测试用例
INFO[2024-08-826 14:58:06]D:/go-ichub/git.ichub.com/webcli120/goconfig/base/generalpool/ichub_result_pool_test.go:61 git.ichub.com/general/webcli120/goconfig/base/generalpool.(*TestIchubResultPoolSuite).Test002_return()
{
"code": 200,
"msg": "成功"
} <nil>
INFO[2024-08-826 14:58:06]D:/go-ichub/git.ichub.com/webcli120/goconfig/base/generalpool/ichub_result_pool_test.go:67 git.ichub.com/general/webcli120/goconfig/base/generalpool.(*TestIchubResultPoolSuite).Test002_return()
{
"code": 200,
"msg": "成功"
} <nil>
INFO[2024-08-826 14:58:06]D:/go-ichub/git.ichub.com/webcli120/goconfig/base/generalpool/ichub_result_pool_test.go:50 git.ichub.com/general/webcli120/goconfig/base/generalpool.(*TestIchubResultPoolSuite).TearDownTest() teardown test
INFO[2024-08-826 14:58:06]D:/go-ichub/git.ichub.com/webcli120/goconfig/base/generalpool/ichub_result_pool_test.go:40 git.ichub.com/general/webcli120/goconfig/base/generalpool.(*TestIchubResultPoolSuite).TearDownSuite() teardown suite
--- PASS: TestIchubResultPoolSuites (0.00s)
--- PASS: TestIchubResultPoolSuites/Test002_return (0.00s)
PASS