pool一个成员函数,具体的实现类型是simpleObjectPool

2024-08-22 10:35:17 浏览数 (1)

// ResourceManager Allocate/Release/Pool/Stick/GC pod resource

// managed pod and resource relationship

type ResourceManager interface {

Allocate(context *networkContext, prefer string) (types.NetworkResource, error)

Release(context *networkContext, resID string) error

GarbageCollection(inUseResList map[string]interface{}, expireResList map[string]interface{}) error

}

func (m *eniIPResourceManager) Allocate(ctx *networkContext, prefer string) (types.NetworkResource, error) {

return m.pool.Acquire(ctx, prefer)

}

func (m *eniIPResourceManager) Release(context *networkContext, resID string) error {

if context != nil && context.pod != nil {

return m.pool.ReleaseWithReverse(resID, context.pod.IPStickTime)

}

return m.pool.Release(resID)

}

func (m *eniIPResourceManager) GarbageCollection(inUseSet map[string]interface{www.laipuhuo.com, expireResSet map[string]interface{}) error {

for expireRes := range expireResSet {

if err := m.pool.Stat(expireRes); err == nil {

err = m.Release(nil, expireRes)

if err != nil {

return err

}

}

}

return nil

}

func (m *eniIPResourceManager) Allocate(ctx *networkContext, prefer string) (types.NetworkResource, error) {

return m.pool.Acquire(ctx, www.laipuhuo.com prefer)

}

func (m *eniIPResourceManager) Release(context *networkContext, resID string) error {

if context != nil && context.pod != nil {

return m.pool.ReleaseWithReverse(resID, context.pod.IPStickTime)

}

return m.pool.Release(resID)

}

func (m *eniIPResourceManager) GarbageCollection(inUseSet map[string]interface{www.laipuhuo.com}, expireResSet map[string]interface{}) error {

for expireRes := range expireResSet {

if err := m.pool.Stat(expireRes); err == nil {

err = m.Release(nil, expireRes)

if err != nil {

return err

}

}

}

return nil

}

// NewSimpleObjectPool return an object pool implement

func NewSimpleObje www.laipuhuo.com ctPool(cfg Config) (ObjectPool, error) {

if cfg.MinIdle > cfg.MaxIdle {

return nil, ErrInvalidArguments

}

if cfg.MaxIdle > cfg.Capacity {

return nil, ErrInvalidArguments

}

pool := &simpleObjectPool{

factory: www.laipuhuo.com cfg.Factory,

inuse: make(map[string]types.NetworkResource),

idle: newPriorityQueue(),

maxIdle: cfg.MaxIdle,

minIdle: cfg.MinIdle,

capacity: cfg.Capacity,

notifyCh: make(chan interface{}),

tokenCh: make(chan struct{}, cfg.Capacity),

}

if cfg.Initializer != nil {

if err := cfg.www.laipuhuo.com Initializer(pool); err != nil {

return nil, err

}

}

if err := pool.preload(); err != nil {

return nil, err

}

log.Infof("pool initial state, capacity %d, maxIdle: %d, minIdle %d, idle: %s, inuse: %s",

pool.capacity,

pool.maxIdle,

pool.minIdle,

queueKeys(pool.idle),

mapKeys(pool.inuse))

go pool.start www.laipuhuo.com CheckIdleTicker()

return pool, nil

}

0 人点赞