代码拉取完成,页面将自动刷新
package client
import (
"sync/atomic"
"gitee.com/trackertrader/rpcx/protocol"
)
// OneClientPool is a oneclient pool with fixed size.
// It uses roundrobin algorithm to call its xclients.
// All oneclients share the same configurations such as ServiceDiscovery and serverMessageChan.
type OneClientPool struct {
count uint64
index uint64
oneclients []*OneClient
auth string
failMode FailMode
selectMode SelectMode
discovery ServiceDiscovery
option Option
serverMessageChan chan<- *protocol.Message
}
// NewOneClientPool creates a fixed size OneClient pool.
func NewOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *OneClientPool {
pool := &OneClientPool{
count: uint64(count),
oneclients: make([]*OneClient, count),
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
}
for i := 0; i < count; i++ {
oneclient := NewOneClient(failMode, selectMode, discovery, option)
pool.oneclients[i] = oneclient
}
return pool
}
// NewBidirectionalOneClientPool creates a BidirectionalOneClient pool with fixed size.
func NewBidirectionalOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *OneClientPool {
pool := &OneClientPool{
count: uint64(count),
oneclients: make([]*OneClient, count),
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
serverMessageChan: serverMessageChan,
}
for i := 0; i < count; i++ {
oneclient := NewBidirectionalOneClient(failMode, selectMode, discovery, option, serverMessageChan)
pool.oneclients[i] = oneclient
}
return pool
}
// Auth sets s token for Authentication.
func (c *OneClientPool) Auth(auth string) {
c.auth = auth
for _, v := range c.oneclients {
v.Auth(auth)
}
}
// Get returns a OneClient.
// It does not remove this OneClient from its cache so you don't need to put it back.
// Don't close this OneClient because maybe other goroutines are using this OneClient.
func (p *OneClientPool) Get() *OneClient {
i := atomic.AddUint64(&p.index, 1)
picked := int(i % p.count)
return p.oneclients[picked]
}
// Close this pool.
// Please make sure it won't be used any more.
func (p OneClientPool) Close() {
for _, c := range p.oneclients {
c.Close()
}
p.oneclients = nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。