Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
44
pkg/redis/client_pool.go
Normal file
44
pkg/redis/client_pool.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
func NewClientPool(args ...Pool) ClientPoolInterface {
|
||||
result := &ClientPool{}
|
||||
|
||||
if len(args) > 0 {
|
||||
result.pool = args[0]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
type ClientPoolInterface interface {
|
||||
GetPool() Pool
|
||||
SetPool(pool Pool)
|
||||
GetFromPool() Client
|
||||
}
|
||||
|
||||
type ClientPool struct {
|
||||
oncePool sync.Once
|
||||
pool Pool
|
||||
}
|
||||
|
||||
func (f *ClientPool) GetPool() Pool {
|
||||
f.oncePool.Do(func() {
|
||||
if f.pool == nil {
|
||||
f.pool = NewPool()
|
||||
}
|
||||
})
|
||||
|
||||
return f.pool
|
||||
}
|
||||
|
||||
func (f *ClientPool) SetPool(pool Pool) {
|
||||
f.pool = pool
|
||||
}
|
||||
|
||||
func (f *ClientPool) GetFromPool() Client {
|
||||
return NewClient(f.GetPool().Get())
|
||||
}
|
||||
Reference in New Issue
Block a user