Compare commits

..

2 Commits

Author SHA1 Message Date
boyce
d1935b1bbc 优化日志 2024-05-27 18:09:37 +08:00
boyce
90d54bf3e2 1.新增服务和Global配置接口,支持通过结构体解析
2.优化日志
2024-05-15 10:06:50 +08:00
4 changed files with 43 additions and 3 deletions

View File

@@ -8,6 +8,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/duanhf2012/origin/v2/event" "github.com/duanhf2012/origin/v2/event"
"errors"
"reflect"
) )
var configDir = "./config/" var configDir = "./config/"
@@ -433,6 +435,25 @@ func (cls *Cluster) GetGlobalCfg() interface{} {
return cls.globalCfg return cls.globalCfg
} }
func (cls *Cluster) ParseGlobalCfg(cfg interface{}) error{
if cls.globalCfg == nil {
return errors.New("no service configuration found")
}
rv := reflect.ValueOf(cls.globalCfg)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return errors.New("no service configuration found")
}
bytes,err := json.Marshal(cls.globalCfg)
if err != nil {
return err
}
return json.Unmarshal(bytes,cfg)
}
func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo,bool) { func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo,bool) {
cls.locker.RLock() cls.locker.RLock()
defer cls.locker.RUnlock() defer cls.locker.RUnlock()

View File

@@ -14,6 +14,7 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/duanhf2012/origin/v2/concurrent" "github.com/duanhf2012/origin/v2/concurrent"
"encoding/json"
) )
var timerDispatcherLen = 100000 var timerDispatcherLen = 100000
@@ -295,6 +296,24 @@ func (s *Service) GetServiceCfg()interface{}{
return s.serviceCfg return s.serviceCfg
} }
func (s *Service) ParseServiceCfg(cfg interface{}) error{
if s.serviceCfg == nil {
return errors.New("no service configuration found")
}
rv := reflect.ValueOf(s.serviceCfg)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return errors.New("no service configuration found")
}
bytes,err := json.Marshal(s.serviceCfg)
if err != nil {
return err
}
return json.Unmarshal(bytes,cfg)
}
func (s *Service) GetProfiler() *profiler.Profiler{ func (s *Service) GetProfiler() *profiler.Profiler{
return s.profiler return s.profiler
} }

View File

@@ -23,7 +23,7 @@ func Init() {
for _,s := range setupServiceList { for _,s := range setupServiceList {
err := s.OnInit() err := s.OnInit()
if err != nil { if err != nil {
log.SError("Failed to initialize "+s.GetName()+" service:"+err.Error()) log.Error("Failed to initialize "+s.GetName()+" service",log.ErrorAttr("err",err))
os.Exit(1) os.Exit(1)
} }
} }

View File

@@ -129,7 +129,7 @@ func (slf *WSClient) Run() {
for{ for{
bytes,err := slf.wsConn.ReadMsg() bytes,err := slf.wsConn.ReadMsg()
if err != nil { if err != nil {
log.Debug("read client id %d is error:%+v",slf.id,err) log.Debug("read client id %s is error:%+v",slf.id,err)
break break
} }
data,err:=slf.wsService.process.Unmarshal(slf.id,bytes) data,err:=slf.wsService.process.Unmarshal(slf.id,bytes)
@@ -153,7 +153,7 @@ func (ws *WSService) SendMsg(clientid string,msg interface{}) error{
client,ok := ws.mapClient[clientid] client,ok := ws.mapClient[clientid]
if ok == false{ if ok == false{
ws.mapClientLocker.Unlock() ws.mapClientLocker.Unlock()
return fmt.Errorf("client %d is disconnect!",clientid) return fmt.Errorf("client %s is disconnect!",clientid)
} }
ws.mapClientLocker.Unlock() ws.mapClientLocker.Unlock()