mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-07 01:04:41 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
166facc959 | ||
|
|
5bb747201b | ||
|
|
1014bc54e4 | ||
|
|
9c26c742fe | ||
|
|
d1935b1bbc | ||
|
|
90d54bf3e2 |
@@ -1181,6 +1181,8 @@ func (slf *TestTcpService) OnRequest (clientid string,msg proto.Message){
|
||||
* log/log.go:日志的封装,可以使用它构建对象记录业务文件日志
|
||||
* util:在该目录下,有常用的uuid,hash,md5,协程封装等工具库
|
||||
* 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"
|
||||
"sync"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var configDir = "./config/"
|
||||
@@ -433,6 +435,25 @@ func (cls *Cluster) GetGlobalCfg() interface{} {
|
||||
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) {
|
||||
cls.locker.RLock()
|
||||
defer cls.locker.RUnlock()
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/util/queue"
|
||||
"context"
|
||||
)
|
||||
|
||||
var idleTimeout = int64(2 * time.Second)
|
||||
@@ -30,6 +31,9 @@ type dispatch struct {
|
||||
|
||||
waitWorker 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)) {
|
||||
@@ -40,7 +44,7 @@ func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan
|
||||
d.workerQueue = make(chan task)
|
||||
d.cbChannel = cbChannel
|
||||
d.queueIdChannel = make(chan int64, cap(tasks))
|
||||
|
||||
d.cancelContext,d.cancel = context.WithCancel(context.Background())
|
||||
d.waitDispatch.Add(1)
|
||||
go d.run()
|
||||
}
|
||||
@@ -64,10 +68,12 @@ func (d *dispatch) run() {
|
||||
d.processqueueEvent(queueId)
|
||||
case <-timeout.C:
|
||||
d.processTimer()
|
||||
if atomic.LoadInt32(&d.minConcurrentNum) == -1 && len(d.tasks) == 0 {
|
||||
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 10))
|
||||
}
|
||||
case <- d.cancelContext.Done():
|
||||
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 5))
|
||||
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() {
|
||||
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
||||
d.cancel()
|
||||
|
||||
|
||||
breakFor:
|
||||
for {
|
||||
|
||||
@@ -45,8 +45,10 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
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) {
|
||||
typeStruct := struct {Type int `json:"typ"`}{}
|
||||
defer jsonProcessor.ReleaseBytes(data)
|
||||
err := json.Unmarshal(data, &typeStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -76,7 +77,7 @@ func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (inte
|
||||
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) {
|
||||
@@ -104,7 +105,8 @@ func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonP
|
||||
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 {
|
||||
log.Debug("Unknown message",log.String("clientId",clientId))
|
||||
return
|
||||
|
||||
@@ -54,8 +54,10 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
|
||||
}
|
||||
|
||||
// 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)
|
||||
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
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
|
||||
func (pbProcessor *PBProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||
defer pbProcessor.ReleaseBytes(data)
|
||||
return pbProcessor.UnmarshalWithOutRelease(clientId, data)
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PBPackInfo{typ: msgType, msg: protoMsg}, nil
|
||||
return &PBPackInfo{typ: msgType, msg: protoMsg,rawMsg:data}, nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
@@ -133,8 +134,9 @@ func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16, msg []byte) *PBPackIn
|
||||
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))
|
||||
recyclerReaderBytes(msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
|
||||
@@ -38,9 +38,11 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
||||
recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -80,7 +82,8 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRaw
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package processor
|
||||
|
||||
type IProcessor interface {
|
||||
// must goroutine safe
|
||||
MsgRoute(clientId string,msg interface{}) error
|
||||
MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error
|
||||
//must goroutine safe
|
||||
UnknownMsgRoute(clientId string,msg interface{})
|
||||
UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte))
|
||||
// connect event
|
||||
ConnectedRoute(clientId string)
|
||||
DisConnectedRoute(clientId string)
|
||||
|
||||
@@ -129,6 +129,13 @@ func (tcpConn *TCPConn) ReadMsg() ([]byte, error) {
|
||||
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){
|
||||
tcpConn.msgParser.ReleaseBytes(byteBuff)
|
||||
}
|
||||
|
||||
@@ -110,7 +110,6 @@ type IRpcHandler interface {
|
||||
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
||||
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
||||
CastGo(serviceMethod string, args interface{}) error
|
||||
IsSingleCoroutine() bool
|
||||
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
||||
GetRpcServer() FuncRpcServer
|
||||
}
|
||||
@@ -539,10 +538,6 @@ func (handler *RpcHandler) GetName() string {
|
||||
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 {
|
||||
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"github.com/duanhf2012/origin/v2/concurrent"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var timerDispatcherLen = 100000
|
||||
@@ -57,6 +58,7 @@ type Service struct {
|
||||
serviceCfg interface{}
|
||||
goroutineNum int32
|
||||
startStatus bool
|
||||
isRelease int32
|
||||
retire int32
|
||||
eventProcessor event.IEventProcessor
|
||||
profiler *profiler.Profiler //性能分析器
|
||||
@@ -147,6 +149,7 @@ func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServe
|
||||
|
||||
func (s *Service) Start() {
|
||||
s.startStatus = true
|
||||
atomic.StoreInt32(&s.isRelease,0)
|
||||
var waitRun sync.WaitGroup
|
||||
|
||||
for i:=int32(0);i< s.goroutineNum;i++{
|
||||
@@ -175,6 +178,7 @@ func (s *Service) Run() {
|
||||
select {
|
||||
case <- s.closeSig:
|
||||
bStop = true
|
||||
s.Release()
|
||||
concurrent.Close()
|
||||
case cb:=<-concurrentCBChannel:
|
||||
concurrent.DoCallback(cb)
|
||||
@@ -247,10 +251,6 @@ func (s *Service) Run() {
|
||||
}
|
||||
|
||||
if bStop == true {
|
||||
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
|
||||
s.startStatus = false
|
||||
s.Release()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -273,8 +273,11 @@ func (s *Service) Release(){
|
||||
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(){
|
||||
@@ -295,6 +298,24 @@ func (s *Service) GetServiceCfg()interface{}{
|
||||
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{
|
||||
return s.profiler
|
||||
}
|
||||
@@ -307,10 +328,6 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
|
||||
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
||||
}
|
||||
|
||||
func (s *Service) IsSingleCoroutine() bool {
|
||||
return s.goroutineNum == 1
|
||||
}
|
||||
|
||||
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
|
||||
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func Init() {
|
||||
for _,s := range setupServiceList {
|
||||
err := s.OnInit()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -86,12 +85,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 {
|
||||
var indexes []mongo.IndexModel
|
||||
for _, keys := range indexKeys {
|
||||
keysDoc := bsonx.Doc{}
|
||||
keysDoc := bson.D{}
|
||||
for _, key := range keys {
|
||||
if asc {
|
||||
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
||||
keysDoc = append(keysDoc, bson.E{Key:key,Value:1})
|
||||
} 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:
|
||||
tcpService.process.DisConnectedRoute(pack.ClientId)
|
||||
case TPT_UnknownPack:
|
||||
tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data)
|
||||
tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data,tcpService.recyclerReaderBytes)
|
||||
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){
|
||||
tcpService.process = process
|
||||
tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp,handler, tcpService.TcpEventHandler)
|
||||
|
||||
@@ -95,9 +95,9 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
||||
case WPT_DisConnected:
|
||||
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
||||
case WPT_UnknownPack:
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data)
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes)
|
||||
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{
|
||||
bytes,err := slf.wsConn.ReadMsg()
|
||||
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
|
||||
}
|
||||
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]
|
||||
if ok == false{
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientid)
|
||||
return fmt.Errorf("client %s is disconnect!",clientid)
|
||||
}
|
||||
|
||||
ws.mapClientLocker.Unlock()
|
||||
@@ -180,3 +180,5 @@ func (ws *WSService) Close(clientid string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ws *WSService) recyclerReaderBytes(data []byte) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user