Allow custom Pool without constructor
This commit is contained in:
parent
c6c6c9cac2
commit
d20c3c7bd1
@ -16,15 +16,15 @@ var (
|
|||||||
SelectRandom = selectRandom
|
SelectRandom = selectRandom
|
||||||
)
|
)
|
||||||
|
|
||||||
type poolClient struct {
|
type PoolClient struct {
|
||||||
*Client
|
*Client
|
||||||
Rate int
|
Rate int
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type SelectionHandler func(map[string]*poolClient, string) string
|
type SelectionHandler func(map[string]*PoolClient, string) string
|
||||||
|
|
||||||
func selectWithRate(pool map[string]*poolClient,
|
func selectWithRate(pool map[string]*PoolClient,
|
||||||
last string) (addr string) {
|
last string) (addr string) {
|
||||||
total := 0
|
total := 0
|
||||||
for _, item := range pool {
|
for _, item := range pool {
|
||||||
@ -36,7 +36,7 @@ func selectWithRate(pool map[string]*poolClient,
|
|||||||
return last
|
return last
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectRandom(pool map[string]*poolClient,
|
func selectRandom(pool map[string]*PoolClient,
|
||||||
last string) (addr string) {
|
last string) (addr string) {
|
||||||
r := rand.Intn(len(pool))
|
r := rand.Intn(len(pool))
|
||||||
i := 0
|
i := 0
|
||||||
@ -53,7 +53,7 @@ type Pool struct {
|
|||||||
SelectionHandler SelectionHandler
|
SelectionHandler SelectionHandler
|
||||||
ErrorHandler ErrorHandler
|
ErrorHandler ErrorHandler
|
||||||
|
|
||||||
clients map[string]*poolClient
|
clients map[string]*PoolClient
|
||||||
last string
|
last string
|
||||||
|
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
@ -62,7 +62,7 @@ type Pool struct {
|
|||||||
// Return a new pool.
|
// Return a new pool.
|
||||||
func NewPool() (pool *Pool) {
|
func NewPool() (pool *Pool) {
|
||||||
return &Pool{
|
return &Pool{
|
||||||
clients: make(map[string]*poolClient, poolSize),
|
clients: make(map[string]*PoolClient, poolSize),
|
||||||
SelectionHandler: SelectWithRate,
|
SelectionHandler: SelectWithRate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,7 +71,7 @@ func NewPool() (pool *Pool) {
|
|||||||
func (pool *Pool) Add(net, addr string, rate int) (err error) {
|
func (pool *Pool) Add(net, addr string, rate int) (err error) {
|
||||||
pool.mutex.Lock()
|
pool.mutex.Lock()
|
||||||
defer pool.mutex.Unlock()
|
defer pool.mutex.Unlock()
|
||||||
var item *poolClient
|
var item *PoolClient
|
||||||
var ok bool
|
var ok bool
|
||||||
if item, ok = pool.clients[addr]; ok {
|
if item, ok = pool.clients[addr]; ok {
|
||||||
item.Rate = rate
|
item.Rate = rate
|
||||||
@ -79,7 +79,7 @@ func (pool *Pool) Add(net, addr string, rate int) (err error) {
|
|||||||
var client *Client
|
var client *Client
|
||||||
client, err = New(net, addr)
|
client, err = New(net, addr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
item = &poolClient{Client: client, Rate: rate}
|
item = &PoolClient{Client: client, Rate: rate}
|
||||||
pool.clients[addr] = item
|
pool.clients[addr] = item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,7 +128,7 @@ func (pool *Pool) Status(addr, handle string) (status *Status, err error) {
|
|||||||
|
|
||||||
// Send a something out, get the samething back.
|
// Send a something out, get the samething back.
|
||||||
func (pool *Pool) Echo(addr string, data []byte) (echo []byte, err error) {
|
func (pool *Pool) Echo(addr string, data []byte) (echo []byte, err error) {
|
||||||
var client *poolClient
|
var client *PoolClient
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
client = pool.selectServer()
|
client = pool.selectServer()
|
||||||
} else {
|
} else {
|
||||||
@ -154,7 +154,7 @@ func (pool *Pool) Close() (err map[string]error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// selecting server
|
// selecting server
|
||||||
func (pool *Pool) selectServer() (client *poolClient) {
|
func (pool *Pool) selectServer() (client *PoolClient) {
|
||||||
for client == nil {
|
for client == nil {
|
||||||
addr := pool.SelectionHandler(pool.clients, pool.last)
|
addr := pool.SelectionHandler(pool.clients, pool.last)
|
||||||
var ok bool
|
var ok bool
|
||||||
|
Loading…
Reference in New Issue
Block a user