1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
RpcClientContainer.go 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
joe 提交于 2024-02-20 18:03 . update
package cRpc
import (
"fmt"
"reflect"
"sync"
"google.golang.org/grpc"
)
type RpcClientContainer struct {
lock sync.RWMutex
instances map[string]any
hosts map[string]string
grpc map[string]grpc.ClientConnInterface
}
func (i *RpcClientContainer) Save(instance any) bool {
i.lock.Lock()
defer i.lock.Unlock()
pkg, app, service := instance.(RpcClientInterface).RpcClientName()
index := pkg + "." + app + "." + service
i.instances[index] = instance
return true
}
func (i *RpcClientContainer) Get(name string) any {
i.lock.RLock()
defer i.lock.RUnlock()
return i.instances[name]
}
func (i *RpcClientContainer) Remove(name string) bool {
i.lock.Lock()
defer i.lock.Unlock()
delete(i.instances, name)
return true
}
func (i *RpcClientContainer) Is(instance any) bool {
return reflect.TypeOf(instance).Implements(reflect.TypeOf((*RpcServiceInterface)(nil)).Elem())
}
func (i *RpcClientContainer) Range(f func(instance any)) {
i.lock.RLock()
defer i.lock.RUnlock()
for _, item := range i.instances {
f(item)
}
}
func (i *RpcClientContainer) GetHost(pkg, app, name string) (host string) {
index := fmt.Sprintf("%s.%s.%s", pkg, app, name)
return i.hosts[index]
}
func (i *RpcClientContainer) GetGRPCConnection(pkg, app, name string) grpc.ClientConnInterface {
index := fmt.Sprintf("%s.%s.%s", pkg, app, name)
return i.grpc[index]
}
var client_container = &RpcClientContainer{
lock: sync.RWMutex{},
instances: make(map[string]any),
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.4.0

搜索帮助