代码拉取完成,页面将自动刷新
// Package roundrobin implements a roundrobin call strategy
package roundrobin
import (
"sync"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"context"
)
type roundrobin struct {
sync.Mutex
rr map[string]int
client.Client
}
func (s *roundrobin) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
nOpts := append(opts, client.WithSelectOption(
// create a selector strategy
selector.WithStrategy(func(services []*registry.Service) selector.Next {
// flatten
var nodes []*registry.Node
for _, service := range services {
nodes = append(nodes, service.Nodes...)
}
// create the next func that always returns our node
return func() (*registry.Node, error) {
if len(nodes) == 0 {
return nil, selector.ErrNoneAvailable
}
s.Lock()
// get counter
rr := s.rr[req.Service()]
// get node
node := nodes[rr%len(nodes)]
// increment
rr++
// save
s.rr[req.Service()] = rr
s.Unlock()
return node, nil
}
}),
))
return s.Client.Call(ctx, req, rsp, nOpts...)
}
// NewClientWrapper is a wrapper which roundrobins requests
func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
return &roundrobin{
rr: make(map[string]int),
Client: c,
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。