1 Star 0 Fork 0

csingo/cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Type.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
Joe Chan 提交于 2022-04-25 14:01 . update
package cRpc
import (
"fmt"
"sync"
)
const (
Header_Rpc_App = "Rpc-App"
Header_Rpc_Service = "Rpc-Service"
Header_Rpc_Method = "Rpc-Method"
)
type RpcServiceInterface interface {
RpcServiceName() (app string, service string)
}
type RpcContainer struct {
Lock *sync.Mutex
Instances map[string]interface{}
}
func (i *RpcContainer) Save(instance interface{}) {
i.Lock.Lock()
defer i.Lock.Unlock()
if IsRpcService(instance) {
app, service := instance.(RpcServiceInterface).RpcServiceName()
name := fmt.Sprintf("%s.%s", app, service)
i.Instances[name] = instance
}
}
func (i *RpcContainer) Get(app, service string) interface{} {
i.Lock.Lock()
defer i.Lock.Unlock()
name := fmt.Sprintf("%s.%s", app, service)
if _, ok := i.Instances[name]; ok {
return i.Instances[name]
}
return nil
}
var container = &RpcContainer{
Lock: &sync.Mutex{},
Instances: make(map[string]interface{}),
}
type RpcClientInterface interface {
RpcClientName() (app string, service string)
}
type RpcClientContainer struct {
Lock *sync.Mutex
Hosts map[string]string
}
func (i *RpcClientContainer) Save(client *RpcConf_Client) {
i.Lock.Lock()
defer i.Lock.Unlock()
if client == nil || len(client.Services) == 0 {
return
}
for _, service := range client.Services {
name := fmt.Sprintf("%s.%s", service.App, service.Service)
i.Hosts[name] = client.Host
}
}
func (i *RpcClientContainer) GetHost(app, service string) string {
i.Lock.Lock()
defer i.Lock.Unlock()
name := fmt.Sprintf("%s.%s", app, service)
if _, ok := i.Hosts[name]; ok {
return i.Hosts[name]
}
return ""
}
var clientContainer = &RpcClientContainer{
Lock: &sync.Mutex{},
Hosts: make(map[string]string),
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.0.13

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385