优化lego框架层代码

This commit is contained in:
liwei1dao 2022-06-09 11:44:31 +08:00
parent f9185698e0
commit 5323f63e19

View File

@ -22,12 +22,11 @@ import (
) )
type RPCXService struct { type RPCXService struct {
cbase.ServiceBase cbase.ServiceBase //继承服务基类
opts *Options opts *Options //服务启动的配置信息数据
serverList sync.Map serverList sync.Map //集群服务会话管理列表对象
rpcxService base.IRPCXService rpcxService base.IRPCXService //服务自身 通过接口可以实现上层服务类重构底层接口
IsInClustered bool IsInClustered bool //当前服务是否已加入到集群中
lock sync.RWMutex //服务锁
} }
func (this *RPCXService) GetTag() string { func (this *RPCXService) GetTag() string {
@ -137,7 +136,6 @@ func (this *RPCXService) Destroy() (err error) {
//注册服务会话 当有新的服务加入时 //注册服务会话 当有新的服务加入时
func (this *RPCXService) FindServiceHandlefunc(node registry.ServiceNode) { func (this *RPCXService) FindServiceHandlefunc(node registry.ServiceNode) {
this.lock.Lock()
if _, ok := this.serverList.Load(node.Id); !ok { if _, ok := this.serverList.Load(node.Id); !ok {
if s, err := NewServiceSession(&node); err != nil { if s, err := NewServiceSession(&node); err != nil {
log.Errorf("创建服务会话失败【%s】 err:%v", node.Id, err) log.Errorf("创建服务会话失败【%s】 err:%v", node.Id, err)
@ -145,7 +143,6 @@ func (this *RPCXService) FindServiceHandlefunc(node registry.ServiceNode) {
this.serverList.Store(node.Id, s) this.serverList.Store(node.Id, s)
} }
} }
this.lock.Unlock()
if this.IsInClustered { if this.IsInClustered {
event.TriggerEvent(core.Event_FindNewService, node) //触发发现新的服务事件 event.TriggerEvent(core.Event_FindNewService, node) //触发发现新的服务事件
} else { } else {
@ -181,13 +178,11 @@ func (this *RPCXService) UpDataServiceHandlefunc(node registry.ServiceNode) {
//注销服务会话 //注销服务会话
func (this *RPCXService) LoseServiceHandlefunc(sId string) { func (this *RPCXService) LoseServiceHandlefunc(sId string) {
this.lock.Lock()
session, ok := this.serverList.Load(sId) session, ok := this.serverList.Load(sId)
if ok && session != nil { if ok && session != nil {
session.(base.IRPCXServiceSession).Done() session.(base.IRPCXServiceSession).Done()
this.serverList.Delete(sId) this.serverList.Delete(sId)
} }
this.lock.Unlock()
event.TriggerEvent(core.Event_LoseService, sId) //触发发现新的服务事件 event.TriggerEvent(core.Event_LoseService, sId) //触发发现新的服务事件
} }
@ -288,8 +283,6 @@ func (this *RPCXService) RegisterFunctionName(name string, fn interface{}) (err
//同步 执行目标远程服务方法 //同步 执行目标远程服务方法
func (this *RPCXService) RpcCallById(sId string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (err error) { func (this *RPCXService) RpcCallById(sId string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (err error) {
defer lego.Recover(fmt.Sprintf("RpcCallById sId:%s rkey:%v arg %v", sId, serviceMethod, args)) defer lego.Recover(fmt.Sprintf("RpcCallById sId:%s rkey:%v arg %v", sId, serviceMethod, args))
this.lock.RLock()
defer this.lock.RUnlock()
ss, ok := this.serverList.Load(sId) ss, ok := this.serverList.Load(sId)
if !ok { if !ok {
if node, err := registry.GetServiceById(sId); err != nil { if node, err := registry.GetServiceById(sId); err != nil {
@ -311,8 +304,6 @@ func (this *RPCXService) RpcCallById(sId string, serviceMethod string, ctx conte
//异步 执行目标远程服务方法 //异步 执行目标远程服务方法
func (this *RPCXService) RpcGoById(sId string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (call *client.Call, err error) { func (this *RPCXService) RpcGoById(sId string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (call *client.Call, err error) {
defer lego.Recover(fmt.Sprintf("RpcGoById sId:%s rkey:%v arg %v", sId, serviceMethod, args)) defer lego.Recover(fmt.Sprintf("RpcGoById sId:%s rkey:%v arg %v", sId, serviceMethod, args))
this.lock.RLock()
defer this.lock.RUnlock()
ss, ok := this.serverList.Load(sId) ss, ok := this.serverList.Load(sId)
if !ok { if !ok {
if node, err := registry.GetServiceById(sId); err != nil { if node, err := registry.GetServiceById(sId); err != nil {
@ -333,8 +324,6 @@ func (this *RPCXService) RpcGoById(sId string, serviceMethod string, ctx context
func (this *RPCXService) RpcCallByType(sType string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (err error) { func (this *RPCXService) RpcCallByType(sType string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (err error) {
defer lego.Recover(fmt.Sprintf("RpcCallByType sType:%s rkey:%s arg %v", sType, serviceMethod, args)) defer lego.Recover(fmt.Sprintf("RpcCallByType sType:%s rkey:%s arg %v", sType, serviceMethod, args))
this.lock.RLock()
defer this.lock.RUnlock()
ss, err := this.rpcxService.DefauleRpcRouteRules(sType, core.AutoIp) ss, err := this.rpcxService.DefauleRpcRouteRules(sType, core.AutoIp)
if err != nil { if err != nil {
log.Errorf("未找到目标服务【%s】节点 err:%v", sType, err) log.Errorf("未找到目标服务【%s】节点 err:%v", sType, err)
@ -346,8 +335,6 @@ func (this *RPCXService) RpcCallByType(sType string, serviceMethod string, ctx c
func (this *RPCXService) RpcGoByType(sType string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (call *client.Call, err error) { func (this *RPCXService) RpcGoByType(sType string, serviceMethod string, ctx context.Context, args interface{}, reply interface{}) (call *client.Call, err error) {
defer lego.Recover(fmt.Sprintf("RpcCallByType sType:%s rkey:%s arg %v", sType, serviceMethod, args)) defer lego.Recover(fmt.Sprintf("RpcCallByType sType:%s rkey:%s arg %v", sType, serviceMethod, args))
this.lock.RLock()
defer this.lock.RUnlock()
ss, err := this.rpcxService.DefauleRpcRouteRules(sType, core.AutoIp) ss, err := this.rpcxService.DefauleRpcRouteRules(sType, core.AutoIp)
if err != nil { if err != nil {
log.Errorf("未找到目标服务【%s】节点 err:%v", sType, err) log.Errorf("未找到目标服务【%s】节点 err:%v", sType, err)