mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-12 22:54:43 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d225bb4bd2 | ||
|
|
ea37fb5081 | ||
|
|
0a92f48d0b | ||
|
|
f5e86fee02 | ||
|
|
166facc959 | ||
|
|
5bb747201b | ||
|
|
1014bc54e4 | ||
|
|
9c26c742fe | ||
|
|
d1935b1bbc | ||
|
|
90d54bf3e2 |
@@ -1181,6 +1181,8 @@ func (slf *TestTcpService) OnRequest (clientid string,msg proto.Message){
|
|||||||
* log/log.go:日志的封装,可以使用它构建对象记录业务文件日志
|
* log/log.go:日志的封装,可以使用它构建对象记录业务文件日志
|
||||||
* util:在该目录下,有常用的uuid,hash,md5,协程封装等工具库
|
* util:在该目录下,有常用的uuid,hash,md5,协程封装等工具库
|
||||||
* https://github.com/duanhf2012/originservice: 其他扩展支持的服务可以在该工程上看到,目前支持firebase推送的封装。
|
* https://github.com/duanhf2012/originservice: 其他扩展支持的服务可以在该工程上看到,目前支持firebase推送的封装。
|
||||||
|
* https://github.com/duanhf2012/origingame: 基础游戏服务器的框架
|
||||||
|
* etcd与nats开发环境搭建可以从https://github.com/duanhf2012/originserver_v2下的docker-compose获取
|
||||||
|
|
||||||
备注:
|
备注:
|
||||||
-----
|
-----
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/util/queue"
|
"github.com/duanhf2012/origin/v2/util/queue"
|
||||||
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var idleTimeout = int64(2 * time.Second)
|
var idleTimeout = int64(2 * time.Second)
|
||||||
@@ -30,6 +31,9 @@ type dispatch struct {
|
|||||||
|
|
||||||
waitWorker sync.WaitGroup
|
waitWorker sync.WaitGroup
|
||||||
waitDispatch sync.WaitGroup
|
waitDispatch sync.WaitGroup
|
||||||
|
|
||||||
|
cancelContext context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan task, cbChannel chan func(error)) {
|
func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan task, cbChannel chan func(error)) {
|
||||||
@@ -40,7 +44,7 @@ func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan
|
|||||||
d.workerQueue = make(chan task)
|
d.workerQueue = make(chan task)
|
||||||
d.cbChannel = cbChannel
|
d.cbChannel = cbChannel
|
||||||
d.queueIdChannel = make(chan int64, cap(tasks))
|
d.queueIdChannel = make(chan int64, cap(tasks))
|
||||||
|
d.cancelContext,d.cancel = context.WithCancel(context.Background())
|
||||||
d.waitDispatch.Add(1)
|
d.waitDispatch.Add(1)
|
||||||
go d.run()
|
go d.run()
|
||||||
}
|
}
|
||||||
@@ -64,10 +68,12 @@ func (d *dispatch) run() {
|
|||||||
d.processqueueEvent(queueId)
|
d.processqueueEvent(queueId)
|
||||||
case <-timeout.C:
|
case <-timeout.C:
|
||||||
d.processTimer()
|
d.processTimer()
|
||||||
if atomic.LoadInt32(&d.minConcurrentNum) == -1 && len(d.tasks) == 0 {
|
case <- d.cancelContext.Done():
|
||||||
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 10))
|
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 5))
|
||||||
}
|
|
||||||
timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout)))
|
timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout)))
|
||||||
|
for i:=int32(0);i<d.workerNum;i++{
|
||||||
|
d.processIdle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +172,8 @@ func (c *dispatch) pushAsyncDoCallbackEvent(cb func(err error)) {
|
|||||||
|
|
||||||
func (d *dispatch) close() {
|
func (d *dispatch) close() {
|
||||||
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
||||||
|
d.cancel()
|
||||||
|
|
||||||
|
|
||||||
breakFor:
|
breakFor:
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -10,16 +10,20 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultSkip = 7
|
||||||
type IOriginHandler interface {
|
type IOriginHandler interface {
|
||||||
slog.Handler
|
slog.Handler
|
||||||
Lock()
|
Lock()
|
||||||
UnLock()
|
UnLock()
|
||||||
|
SetSkip(skip int)
|
||||||
|
GetSkip() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseHandler struct {
|
type BaseHandler struct {
|
||||||
addSource bool
|
addSource bool
|
||||||
w io.Writer
|
w io.Writer
|
||||||
locker sync.Mutex
|
locker sync.Mutex
|
||||||
|
skip int
|
||||||
}
|
}
|
||||||
|
|
||||||
type OriginTextHandler struct {
|
type OriginTextHandler struct {
|
||||||
@@ -32,6 +36,14 @@ type OriginJsonHandler struct {
|
|||||||
*slog.JSONHandler
|
*slog.JSONHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bh *BaseHandler) SetSkip(skip int){
|
||||||
|
bh.skip = skip
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bh *BaseHandler) GetSkip() int{
|
||||||
|
return bh.skip
|
||||||
|
}
|
||||||
|
|
||||||
func getStrLevel(level slog.Level) string{
|
func getStrLevel(level slog.Level) string{
|
||||||
switch level {
|
switch level {
|
||||||
case LevelTrace:
|
case LevelTrace:
|
||||||
@@ -78,6 +90,7 @@ func NewOriginTextHandler(level slog.Level,w io.Writer,addSource bool,replaceAtt
|
|||||||
ReplaceAttr: replaceAttr,
|
ReplaceAttr: replaceAttr,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
textHandler.skip = defaultSkip
|
||||||
return &textHandler
|
return &textHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +137,7 @@ func NewOriginJsonHandler(level slog.Level,w io.Writer,addSource bool,replaceAtt
|
|||||||
ReplaceAttr: replaceAttr,
|
ReplaceAttr: replaceAttr,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
jsonHandler.skip = defaultSkip
|
||||||
return &jsonHandler
|
return &jsonHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +155,7 @@ func (oh *OriginJsonHandler) Handle(context context.Context, record slog.Record)
|
|||||||
func (b *BaseHandler) Fill(context context.Context, record *slog.Record) {
|
func (b *BaseHandler) Fill(context context.Context, record *slog.Record) {
|
||||||
if b.addSource {
|
if b.addSource {
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(7, pcs[:])
|
runtime.Callers(b.skip, pcs[:])
|
||||||
record.PC = pcs[0]
|
record.PC = pcs[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -239,6 +239,10 @@ func (iw *IoWriter) swichFile() error{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDefaultHandler() IOriginHandler{
|
||||||
|
return gLogger.(*Logger).Slogger.Handler().(IOriginHandler)
|
||||||
|
}
|
||||||
|
|
||||||
func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (ILogger,error){
|
func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (ILogger,error){
|
||||||
var logger Logger
|
var logger Logger
|
||||||
logger.ioWriter.filePath = pathName
|
logger.ioWriter.filePath = pathName
|
||||||
|
|||||||
@@ -45,8 +45,10 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{}) error{
|
func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error{
|
||||||
pPackInfo := msg.(*JsonPackInfo)
|
pPackInfo := msg.(*JsonPackInfo)
|
||||||
|
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||||
|
|
||||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||||
@@ -58,7 +60,6 @@ func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{}) e
|
|||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (interface{}, error) {
|
func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (interface{}, error) {
|
||||||
typeStruct := struct {Type int `json:"typ"`}{}
|
typeStruct := struct {Type int `json:"typ"`}{}
|
||||||
defer jsonProcessor.ReleaseBytes(data)
|
|
||||||
err := json.Unmarshal(data, &typeStruct)
|
err := json.Unmarshal(data, &typeStruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -76,7 +77,7 @@ func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (inte
|
|||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &JsonPackInfo{typ:msgType,msg:msgData},nil
|
return &JsonPackInfo{typ:msgType,msg:msgData,rawMsg: data},nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) Marshal(clientId string,msg interface{}) ([]byte, error) {
|
func (jsonProcessor *JsonProcessor) Marshal(clientId string,msg interface{}) ([]byte, error) {
|
||||||
@@ -104,7 +105,8 @@ func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonP
|
|||||||
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string,msg interface{}){
|
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)){
|
||||||
|
defer recyclerReaderBytes(msg.([]byte))
|
||||||
if jsonProcessor.unknownMessageHandler==nil {
|
if jsonProcessor.unknownMessageHandler==nil {
|
||||||
log.Debug("Unknown message",log.String("clientId",clientId))
|
log.Debug("Unknown message",log.String("clientId",clientId))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -54,8 +54,10 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}) error {
|
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{},recyclerReaderBytes func(data []byte)) error {
|
||||||
pPackInfo := msg.(*PBPackInfo)
|
pPackInfo := msg.(*PBPackInfo)
|
||||||
|
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||||
|
|
||||||
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
return fmt.Errorf("Cannot find msgtype %d is register!", pPackInfo.typ)
|
return fmt.Errorf("Cannot find msgtype %d is register!", pPackInfo.typ)
|
||||||
@@ -67,7 +69,6 @@ func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}) error
|
|||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbProcessor *PBProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
func (pbProcessor *PBProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||||
defer pbProcessor.ReleaseBytes(data)
|
|
||||||
return pbProcessor.UnmarshalWithOutRelease(clientId, data)
|
return pbProcessor.UnmarshalWithOutRelease(clientId, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +92,7 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &PBPackInfo{typ: msgType, msg: protoMsg}, nil
|
return &PBPackInfo{typ: msgType, msg: protoMsg,rawMsg:data}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
@@ -133,8 +134,9 @@ func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16, msg []byte) *PBPackIn
|
|||||||
return &PBPackInfo{typ: msgType, rawMsg: msg}
|
return &PBPackInfo{typ: msgType, rawMsg: msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{}) {
|
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{},recyclerReaderBytes func(data []byte)) {
|
||||||
pbProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
pbProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
||||||
|
recyclerReaderBytes(msg.([]byte))
|
||||||
}
|
}
|
||||||
|
|
||||||
// connect event
|
// connect event
|
||||||
|
|||||||
@@ -38,9 +38,11 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId string, msg interface{}) error{
|
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId string, msg interface{},recyclerReaderBytes func(data []byte)) error{
|
||||||
pPackInfo := msg.(*PBRawPackInfo)
|
pPackInfo := msg.(*PBRawPackInfo)
|
||||||
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
||||||
|
recyclerReaderBytes(pPackInfo.rawMsg)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +82,8 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRaw
|
|||||||
pbRawPackInfo.rawMsg = msg
|
pbRawPackInfo.rawMsg = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string,msg interface{}){
|
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)){
|
||||||
|
defer recyclerReaderBytes(msg.([]byte))
|
||||||
if pbRawProcessor.unknownMessageHandler == nil {
|
if pbRawProcessor.unknownMessageHandler == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package processor
|
|||||||
|
|
||||||
type IProcessor interface {
|
type IProcessor interface {
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
MsgRoute(clientId string,msg interface{}) error
|
MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error
|
||||||
//must goroutine safe
|
//must goroutine safe
|
||||||
UnknownMsgRoute(clientId string,msg interface{})
|
UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte))
|
||||||
// connect event
|
// connect event
|
||||||
ConnectedRoute(clientId string)
|
ConnectedRoute(clientId string)
|
||||||
DisConnectedRoute(clientId string)
|
DisConnectedRoute(clientId string)
|
||||||
|
|||||||
@@ -129,6 +129,13 @@ func (tcpConn *TCPConn) ReadMsg() ([]byte, error) {
|
|||||||
return tcpConn.msgParser.Read(tcpConn)
|
return tcpConn.msgParser.Read(tcpConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tcpConn *TCPConn) GetRecyclerReaderBytes() func (data []byte) {
|
||||||
|
bytePool := tcpConn.msgParser.IBytesMempool
|
||||||
|
return func(data []byte) {
|
||||||
|
bytePool.ReleaseBytes(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
|
func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
|
||||||
tcpConn.msgParser.ReleaseBytes(byteBuff)
|
tcpConn.msgParser.ReleaseBytes(byteBuff)
|
||||||
}
|
}
|
||||||
|
|||||||
14
node/node.go
14
node/node.go
@@ -29,6 +29,7 @@ var preSetupTemplateService []func()service.IService
|
|||||||
var profilerInterval time.Duration
|
var profilerInterval time.Duration
|
||||||
var bValid bool
|
var bValid bool
|
||||||
var configDir = "./config/"
|
var configDir = "./config/"
|
||||||
|
var NodeIsRun = false
|
||||||
|
|
||||||
const(
|
const(
|
||||||
SingleStop syscall.Signal = 10
|
SingleStop syscall.Signal = 10
|
||||||
@@ -57,7 +58,7 @@ func init() {
|
|||||||
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
|
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
|
||||||
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
|
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
|
||||||
console.RegisterCommandInt("logsize", 0, "<-logsize size> Set log size(MB).", setLogSize)
|
console.RegisterCommandInt("logsize", 0, "<-logsize size> Set log size(MB).", setLogSize)
|
||||||
console.RegisterCommandInt("logchannelcap", 0, "<-logchannelcap num> Set log channel cap.", setLogChannelCapNum)
|
console.RegisterCommandInt("logchannelcap", -1, "<-logchannelcap num> Set log channel cap.", setLogChannelCapNum)
|
||||||
console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof)
|
console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,13 +355,14 @@ func startNode(args interface{}) error {
|
|||||||
cluster.GetCluster().Start()
|
cluster.GetCluster().Start()
|
||||||
|
|
||||||
//6.监听程序退出信号&性能报告
|
//6.监听程序退出信号&性能报告
|
||||||
bRun := true
|
|
||||||
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
||||||
if profilerInterval > 0 {
|
if profilerInterval > 0 {
|
||||||
pProfilerTicker = time.NewTicker(profilerInterval)
|
pProfilerTicker = time.NewTicker(profilerInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
for bRun {
|
NodeIsRun = true
|
||||||
|
for NodeIsRun {
|
||||||
select {
|
select {
|
||||||
case s := <-sig:
|
case s := <-sig:
|
||||||
signal := s.(syscall.Signal)
|
signal := s.(syscall.Signal)
|
||||||
@@ -368,7 +370,7 @@ func startNode(args interface{}) error {
|
|||||||
log.Info("receipt retire signal.")
|
log.Info("receipt retire signal.")
|
||||||
notifyAllServiceRetire()
|
notifyAllServiceRetire()
|
||||||
}else {
|
}else {
|
||||||
bRun = false
|
NodeIsRun = false
|
||||||
log.Info("receipt stop signal.")
|
log.Info("receipt stop signal.")
|
||||||
}
|
}
|
||||||
case <-pProfilerTicker.C:
|
case <-pProfilerTicker.C:
|
||||||
@@ -504,6 +506,10 @@ func setLogChannelCapNum(args interface{}) error {
|
|||||||
return errors.New("param logsize is error")
|
return errors.New("param logsize is error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logChannelCap == -1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
log.LogChannelCap = logChannelCap
|
log.LogChannelCap = logChannelCap
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ type IRpcHandler interface {
|
|||||||
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
||||||
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
||||||
CastGo(serviceMethod string, args interface{}) error
|
CastGo(serviceMethod string, args interface{}) error
|
||||||
IsSingleCoroutine() bool
|
|
||||||
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
||||||
GetRpcServer() FuncRpcServer
|
GetRpcServer() FuncRpcServer
|
||||||
}
|
}
|
||||||
@@ -539,10 +538,6 @@ func (handler *RpcHandler) GetName() string {
|
|||||||
return handler.rpcHandler.GetName()
|
return handler.rpcHandler.GetName()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *RpcHandler) IsSingleCoroutine() bool {
|
|
||||||
return handler.rpcHandler.IsSingleCoroutine()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler *RpcHandler) CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error {
|
func (handler *RpcHandler) CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error {
|
||||||
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
|
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -57,6 +58,7 @@ type Service struct {
|
|||||||
serviceCfg interface{}
|
serviceCfg interface{}
|
||||||
goroutineNum int32
|
goroutineNum int32
|
||||||
startStatus bool
|
startStatus bool
|
||||||
|
isRelease int32
|
||||||
retire int32
|
retire int32
|
||||||
eventProcessor event.IEventProcessor
|
eventProcessor event.IEventProcessor
|
||||||
profiler *profiler.Profiler //性能分析器
|
profiler *profiler.Profiler //性能分析器
|
||||||
@@ -147,6 +149,7 @@ func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServe
|
|||||||
|
|
||||||
func (s *Service) Start() {
|
func (s *Service) Start() {
|
||||||
s.startStatus = true
|
s.startStatus = true
|
||||||
|
atomic.StoreInt32(&s.isRelease,0)
|
||||||
var waitRun sync.WaitGroup
|
var waitRun sync.WaitGroup
|
||||||
|
|
||||||
for i:=int32(0);i< s.goroutineNum;i++{
|
for i:=int32(0);i< s.goroutineNum;i++{
|
||||||
@@ -175,6 +178,7 @@ func (s *Service) Run() {
|
|||||||
select {
|
select {
|
||||||
case <- s.closeSig:
|
case <- s.closeSig:
|
||||||
bStop = true
|
bStop = true
|
||||||
|
s.Release()
|
||||||
concurrent.Close()
|
concurrent.Close()
|
||||||
case cb:=<-concurrentCBChannel:
|
case cb:=<-concurrentCBChannel:
|
||||||
concurrent.DoCallback(cb)
|
concurrent.DoCallback(cb)
|
||||||
@@ -247,10 +251,6 @@ func (s *Service) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if bStop == true {
|
if bStop == true {
|
||||||
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
|
|
||||||
s.startStatus = false
|
|
||||||
s.Release()
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,8 +273,11 @@ func (s *Service) Release(){
|
|||||||
log.Dump(string(buf[:l]),log.String("error",errString))
|
log.Dump(string(buf[:l]),log.String("error",errString))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.self.OnRelease()
|
if atomic.AddInt32(&s.isRelease,-1) == -1{
|
||||||
|
s.self.OnRelease()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) OnRelease(){
|
func (s *Service) OnRelease(){
|
||||||
@@ -295,6 +298,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
|
||||||
}
|
}
|
||||||
@@ -307,10 +328,6 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
|
|||||||
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) IsSingleCoroutine() bool {
|
|
||||||
return s.goroutineNum == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
|
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
|
||||||
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
|
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,6 +47,10 @@ func (mm *MongoModule) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mm *MongoModule) Stop() error {
|
||||||
|
return mm.client.Disconnect(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
func (mm *MongoModule) TakeSession() Session {
|
func (mm *MongoModule) TakeSession() Session {
|
||||||
return Session{Client: mm.client, maxOperatorTimeOut: mm.maxOperatorTimeOut}
|
return Session{Client: mm.client, maxOperatorTimeOut: mm.maxOperatorTimeOut}
|
||||||
}
|
}
|
||||||
@@ -86,12 +89,12 @@ func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]
|
|||||||
func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool, sparse bool, asc bool) error {
|
func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool, sparse bool, asc bool) error {
|
||||||
var indexes []mongo.IndexModel
|
var indexes []mongo.IndexModel
|
||||||
for _, keys := range indexKeys {
|
for _, keys := range indexKeys {
|
||||||
keysDoc := bsonx.Doc{}
|
keysDoc := bson.D{}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if asc {
|
if asc {
|
||||||
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
keysDoc = append(keysDoc, bson.E{Key:key,Value:1})
|
||||||
} else {
|
} else {
|
||||||
keysDoc = keysDoc.Append(key, bsonx.Int32(-1))
|
keysDoc = append(keysDoc, bson.E{Key:key,Value:-1})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,12 +107,16 @@ func (tcpService *TcpService) TcpEventHandler(ev event.IEvent) {
|
|||||||
case TPT_DisConnected:
|
case TPT_DisConnected:
|
||||||
tcpService.process.DisConnectedRoute(pack.ClientId)
|
tcpService.process.DisConnectedRoute(pack.ClientId)
|
||||||
case TPT_UnknownPack:
|
case TPT_UnknownPack:
|
||||||
tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data)
|
tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data,tcpService.recyclerReaderBytes)
|
||||||
case TPT_Pack:
|
case TPT_Pack:
|
||||||
tcpService.process.MsgRoute(pack.ClientId,pack.Data)
|
tcpService.process.MsgRoute(pack.ClientId,pack.Data,tcpService.recyclerReaderBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tcpService *TcpService) recyclerReaderBytes(data []byte) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (tcpService *TcpService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
func (tcpService *TcpService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
||||||
tcpService.process = process
|
tcpService.process = process
|
||||||
tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp,handler, tcpService.TcpEventHandler)
|
tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp,handler, tcpService.TcpEventHandler)
|
||||||
|
|||||||
@@ -95,9 +95,9 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
|||||||
case WPT_DisConnected:
|
case WPT_DisConnected:
|
||||||
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
||||||
case WPT_UnknownPack:
|
case WPT_UnknownPack:
|
||||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data)
|
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes)
|
||||||
case WPT_Pack:
|
case WPT_Pack:
|
||||||
pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data)
|
pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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()
|
||||||
@@ -180,3 +180,5 @@ func (ws *WSService) Close(clientid string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ws *WSService) recyclerReaderBytes(data []byte) {
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user