2 Star 0 Fork 0

djienet/kratos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mockdiscovery.go 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
chenli 提交于 2020-11-02 15:38 . update
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
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/djienet/kratos.git
git@gitee.com:djienet/kratos.git
djienet
kratos
kratos
v1.1.6

搜索帮助