优化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 {
cbase.ServiceBase
opts *Options
serverList sync.Map
rpcxService base.IRPCXService
IsInClustered bool
lock sync.RWMutex //服务锁
cbase.ServiceBase //继承服务基类
opts *Options //服务启动的配置信息数据
serverList sync.Map //集群服务会话管理列表对象
rpcxService base.IRPCXService //服务自身 通过接口可以实现上层服务类重构底层接口
IsInClustered bool //当前服务是否已加入到集群中
}
func (this *RPCXService) GetTag() string {
@ -137,7 +136,6 @@ func (this *RPCXService) Destroy() (err error) {
//注册服务会话 当有新的服务加入时
func (this *RPCXService) FindServiceHandlefunc(node registry.ServiceNode) {
this.lock.Lock()
if _, ok := this.serverList.Load(node.Id); !ok {
if s, err := NewServiceSession(&node); err != nil {
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.lock.Unlock()
if this.IsInClustered {
event.TriggerEvent(core.Event_FindNewService, node) //触发发现新的服务事件
} else {
@ -181,13 +178,11 @@ func (this *RPCXService) UpDataServiceHandlefunc(node registry.ServiceNode) {
//注销服务会话
func (this *RPCXService) LoseServiceHandlefunc(sId string) {
this.lock.Lock()
session, ok := this.serverList.Load(sId)
if ok && session != nil {
session.(base.IRPCXServiceSession).Done()
this.serverList.Delete(sId)
}
this.lock.Unlock()
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) {
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)
if !ok {
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) {
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)
if !ok {
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) {
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)
if err != nil {
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) {
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)
if err != nil {
log.Errorf("未找到目标服务【%s】节点 err:%v", sType, err)