1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Type.go 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
joe 提交于 2023-12-14 09:55 . update
package cRpc
import (
"fmt"
"sync"
"github.com/gin-gonic/gin"
)
//const (
// Header_Rpc_App = "Rpc-App"
// Header_Rpc_Service = "Rpc-Service"
// Header_Rpc_Method = "Rpc-Method"
//)
type DecryptFunc func(ctx *gin.Context) bool
type EncryptFunc func(ctx *gin.Context) string
type ParseHostFunc func(ctx *gin.Context, app, service string) (host, uri string)
type RpcServiceInterface interface {
RpcServiceName() (app, service string)
}
type RpcContainer struct {
Lock *sync.Mutex
DecryptFunc DecryptFunc
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
}
func (i *RpcContainer) SetDecrypt(f DecryptFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.DecryptFunc = f
}
var container = &RpcContainer{
Lock: &sync.Mutex{},
Instances: make(map[string]interface{}),
}
type RpcClientInterface interface {
RpcClientName() (pkg, app, service string)
}
type RpcClientContainer struct {
Lock *sync.Mutex
Signature string
Hosts map[string]string
HostFunc ParseHostFunc
EncryptFunc EncryptFunc
}
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.%s", client.Package, service.App, service.Service)
i.Hosts[name] = client.Host
}
}
func (i *RpcClientContainer) GetHost(pkg, app, service string) string {
i.Lock.Lock()
defer i.Lock.Unlock()
name := fmt.Sprintf("%s.%s.%s", pkg, app, service)
if _, ok := i.Hosts[name]; ok {
return i.Hosts[name]
}
return ""
}
func (i *RpcClientContainer) SetHostFunc(hostFunc ParseHostFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.HostFunc = hostFunc
}
func (i *RpcClientContainer) SetSignature(key string) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.Signature = key
}
func (i *RpcClientContainer) SetEncrypt(f EncryptFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.EncryptFunc = f
}
var clientContainer = &RpcClientContainer{
Lock: &sync.Mutex{},
Signature: "",
Hosts: make(map[string]string),
HostFunc: nil,
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.2.52

搜索帮助