代码拉取完成,页面将自动刷新
package resolver
import (
"context"
"gitee.com/djienet/kratos/pkg/conf/env"
"gitee.com/djienet/kratos/pkg/naming"
)
type mockDiscoveryBuilder struct {
instances map[string]*naming.Instance
watchch map[string][]*mockDiscoveryResolver
}
func (mb *mockDiscoveryBuilder) Build(id string, opts ...naming.BuildOpt) naming.Resolver {
mr := &mockDiscoveryResolver{
d: mb,
watchch: make(chan struct{}, 1),
}
mb.watchch[id] = append(mb.watchch[id], mr)
mr.watchch <- struct{}{}
return mr
}
func (mb *mockDiscoveryBuilder) Scheme() string {
return "mockdiscovery"
}
type mockDiscoveryResolver struct {
d *mockDiscoveryBuilder
watchch chan struct{}
}
var _ naming.Resolver = &mockDiscoveryResolver{}
func (md *mockDiscoveryResolver) Fetch(ctx context.Context) (*naming.InstancesInfo, bool) {
zones := make(map[string][]*naming.Instance)
for _, v := range md.d.instances {
zones[v.Zone] = append(zones[v.Zone], v)
}
return &naming.InstancesInfo{Instances: zones}, len(zones) > 0
}
func (md *mockDiscoveryResolver) Watch() <-chan struct{} {
return md.watchch
}
func (md *mockDiscoveryResolver) Close() error {
close(md.watchch)
return nil
}
func (md *mockDiscoveryResolver) Scheme() string {
return "mockdiscovery"
}
func (mb *mockDiscoveryBuilder) registry(appID string, hostname, rpc string, metadata map[string]string) {
ins := &naming.Instance{
AppID: appID,
Env: "hello=world",
Hostname: hostname,
Addrs: []string{"grpc://" + rpc},
Version: "1.1",
Zone: env.Zone,
Metadata: metadata,
}
mb.instances[hostname] = ins
if ch, ok := mb.watchch[appID]; ok {
var bullet struct{}
for _, c := range ch {
c.watchch <- bullet
}
}
}
func (mb *mockDiscoveryBuilder) cancel(hostname string) {
ins, ok := mb.instances[hostname]
if !ok {
return
}
delete(mb.instances, hostname)
if ch, ok := mb.watchch[ins.AppID]; ok {
var bullet struct{}
for _, c := range ch {
c.watchch <- bullet
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。