1 Star 0 Fork 0

csingo / cRpc

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
Type.go 1.68 KB
Copy Edit Raw Blame History
Joe Chan authored 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

Search

344bd9b3 5694891 D2dac590 5694891