mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 23:24:45 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0489ee3ef4 | ||
|
|
692dacda0c | ||
|
|
7f86b1007d | ||
|
|
ba5e30ae2e | ||
|
|
0b1a1d2283 | ||
|
|
7780947a96 | ||
|
|
4c169cf0bb |
@@ -43,7 +43,7 @@ func main() {
|
|||||||
一个origin进程需要创建一个node对象,Start开始运行。您也可以直接下载origin引擎示例:
|
一个origin进程需要创建一个node对象,Start开始运行。您也可以直接下载origin引擎示例:
|
||||||
|
|
||||||
```
|
```
|
||||||
go get -v -u github.com/duanhf2012/originserver
|
go get -v -u github.com/duanhf2012/originserver_v2
|
||||||
```
|
```
|
||||||
|
|
||||||
本文所有的说明都是基于该示例为主。
|
本文所有的说明都是基于该示例为主。
|
||||||
@@ -55,8 +55,6 @@ origin引擎三大对象关系
|
|||||||
* Service:一个独立的服务可以认为是一个大的功能模块,他是Node的子集,创建完成并安装Node对象中。服务可以支持对外部RPC等功能。
|
* Service:一个独立的服务可以认为是一个大的功能模块,他是Node的子集,创建完成并安装Node对象中。服务可以支持对外部RPC等功能。
|
||||||
* Module: 这是origin最小对象单元,强烈建议所有的业务模块都划分成各个小的Module组合,origin引擎将监控所有服务与Module运行状态,例如可以监控它们的慢处理和死循环函数。Module可以建立树状关系。Service本身也是Module的类型。
|
* Module: 这是origin最小对象单元,强烈建议所有的业务模块都划分成各个小的Module组合,origin引擎将监控所有服务与Module运行状态,例如可以监控它们的慢处理和死循环函数。Module可以建立树状关系。Service本身也是Module的类型。
|
||||||
|
|
||||||
origin集群核心配置文件在config的cluster目录下,如github.com/duanhf2012/originserver的config/cluster目录下有cluster.json与service.json配置:
|
|
||||||
|
|
||||||
## origin配置说明
|
## origin配置说明
|
||||||
|
|
||||||
origin的配置文件以json格式,主要包含Discovery、RpcMode、NodeList、Service部分,具体格式如下:
|
origin的配置文件以json格式,主要包含Discovery、RpcMode、NodeList、Service部分,具体格式如下:
|
||||||
@@ -352,7 +350,7 @@ areaId, ok := mapGlobal["AreaId"]
|
|||||||
第一章:origin基础:
|
第一章:origin基础:
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
查看github.com/duanhf2012/originserver中的simple_service中新建两个服务,分别是TestService1.go与CTestService2.go。
|
查看github.com/duanhf2012/originserver_v2中的simple_service中新建两个服务,分别是TestService1.go与CTestService2.go。
|
||||||
|
|
||||||
simple_service/TestService1.go如下:
|
simple_service/TestService1.go如下:
|
||||||
|
|
||||||
@@ -585,7 +583,7 @@ func (ts *TestService) OnNodeDisconnect(nodeId int){
|
|||||||
Module创建与销毁:
|
Module创建与销毁:
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
可以认为Service就是一种Module,它有Module所有的功能。在示例代码中可以参考originserver/simple_module/TestService3.go。
|
可以认为Service就是一种Module,它有Module所有的功能。在示例代码中可以参考originserver_v2/simple_module/TestService3.go。
|
||||||
|
|
||||||
```
|
```
|
||||||
package simple_module
|
package simple_module
|
||||||
|
|||||||
69
log/log.go
69
log/log.go
@@ -22,7 +22,10 @@ var LogSize int64
|
|||||||
var LogChannelCap int
|
var LogChannelCap int
|
||||||
var LogPath string
|
var LogPath string
|
||||||
var LogLevel slog.Level = LevelTrace
|
var LogLevel slog.Level = LevelTrace
|
||||||
|
|
||||||
|
|
||||||
var gLogger, _ = NewTextLogger(LevelDebug, "", "",true,LogChannelCap)
|
var gLogger, _ = NewTextLogger(LevelDebug, "", "",true,LogChannelCap)
|
||||||
|
var isSetLogger bool
|
||||||
var memPool = bytespool.NewMemAreaPool()
|
var memPool = bytespool.NewMemAreaPool()
|
||||||
|
|
||||||
// levels
|
// levels
|
||||||
@@ -37,6 +40,21 @@ const (
|
|||||||
LevelFatal = slog.Level(20)
|
LevelFatal = slog.Level(20)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ILogger interface {
|
||||||
|
Trace(msg string, args ...any)
|
||||||
|
Debug(msg string, args ...any)
|
||||||
|
Info(msg string, args ...any)
|
||||||
|
Warning(msg string, args ...any)
|
||||||
|
Error(msg string, args ...any)
|
||||||
|
Stack(msg string, args ...any)
|
||||||
|
Dump(msg string, args ...any)
|
||||||
|
Fatal(msg string, args ...any)
|
||||||
|
|
||||||
|
DoSPrintf(level slog.Level,a []interface{})
|
||||||
|
FormatHeader(buf *Buffer,level slog.Level,calldepth int)
|
||||||
|
Close()
|
||||||
|
}
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
Slogger *slog.Logger
|
Slogger *slog.Logger
|
||||||
|
|
||||||
@@ -47,7 +65,6 @@ type Logger struct {
|
|||||||
|
|
||||||
type IoWriter struct {
|
type IoWriter struct {
|
||||||
outFile io.Writer // destination for output
|
outFile io.Writer // destination for output
|
||||||
outConsole io.Writer //os.Stdout
|
|
||||||
writeBytes int64
|
writeBytes int64
|
||||||
logChannel chan []byte
|
logChannel chan []byte
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
@@ -122,8 +139,8 @@ func (iw *IoWriter) Write(p []byte) (n int, err error){
|
|||||||
func (iw *IoWriter) writeIo(p []byte) (n int, err error){
|
func (iw *IoWriter) writeIo(p []byte) (n int, err error){
|
||||||
n,err = iw.writeFile(p)
|
n,err = iw.writeFile(p)
|
||||||
|
|
||||||
if iw.outConsole != nil {
|
if OpenConsole {
|
||||||
n,err = iw.outConsole.Write(p)
|
n,err = os.Stdout.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -217,17 +234,12 @@ func (iw *IoWriter) swichFile() error{
|
|||||||
iw.fileDay = now.Day()
|
iw.fileDay = now.Day()
|
||||||
iw.fileCreateTime = now.Unix()
|
iw.fileCreateTime = now.Unix()
|
||||||
atomic.StoreInt64(&iw.writeBytes,0)
|
atomic.StoreInt64(&iw.writeBytes,0)
|
||||||
if OpenConsole == true {
|
|
||||||
iw.outConsole = os.Stdout
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
iw.outConsole = os.Stdout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (*Logger,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
|
||||||
logger.ioWriter.fileprefix = filePrefix
|
logger.ioWriter.fileprefix = filePrefix
|
||||||
@@ -242,7 +254,7 @@ func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource
|
|||||||
return &logger,nil
|
return &logger,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJsonLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (*Logger,error){
|
func NewJsonLogger(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
|
||||||
logger.ioWriter.fileprefix = filePrefix
|
logger.ioWriter.fileprefix = filePrefix
|
||||||
@@ -296,13 +308,18 @@ func (logger *Logger) Fatal(msg string, args ...any) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// It's dangerous to call the method on logging
|
// It's non-thread-safe
|
||||||
func Export(logger *Logger) {
|
func SetLogger(logger ILogger) {
|
||||||
if logger != nil {
|
if logger != nil && isSetLogger == false {
|
||||||
gLogger = logger
|
gLogger = logger
|
||||||
|
isSetLogger = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLogger() ILogger{
|
||||||
|
return gLogger
|
||||||
|
}
|
||||||
|
|
||||||
func Trace(msg string, args ...any){
|
func Trace(msg string, args ...any){
|
||||||
gLogger.Trace(msg, args...)
|
gLogger.Trace(msg, args...)
|
||||||
}
|
}
|
||||||
@@ -415,7 +432,7 @@ func Group(key string, args ...any) slog.Attr {
|
|||||||
return slog.Group(key, args...)
|
return slog.Group(key, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) doSPrintf(level slog.Level,a []interface{}) {
|
func (logger *Logger) DoSPrintf(level slog.Level,a []interface{}) {
|
||||||
if logger.Slogger.Enabled(context.Background(),level) == false{
|
if logger.Slogger.Enabled(context.Background(),level) == false{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -425,7 +442,7 @@ func (logger *Logger) doSPrintf(level slog.Level,a []interface{}) {
|
|||||||
|
|
||||||
logger.sBuff.Reset()
|
logger.sBuff.Reset()
|
||||||
|
|
||||||
logger.formatHeader(&logger.sBuff,level,3)
|
logger.FormatHeader(&logger.sBuff,level,3)
|
||||||
|
|
||||||
for _,s := range a {
|
for _,s := range a {
|
||||||
logger.sBuff.AppendString(slog.AnyValue(s).String())
|
logger.sBuff.AppendString(slog.AnyValue(s).String())
|
||||||
@@ -435,46 +452,46 @@ func (logger *Logger) doSPrintf(level slog.Level,a []interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) STrace(a ...interface{}) {
|
func (logger *Logger) STrace(a ...interface{}) {
|
||||||
logger.doSPrintf(LevelTrace,a)
|
logger.DoSPrintf(LevelTrace,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SDebug(a ...interface{}) {
|
func (logger *Logger) SDebug(a ...interface{}) {
|
||||||
logger.doSPrintf(LevelDebug,a)
|
logger.DoSPrintf(LevelDebug,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SInfo(a ...interface{}) {
|
func (logger *Logger) SInfo(a ...interface{}) {
|
||||||
logger.doSPrintf(LevelInfo,a)
|
logger.DoSPrintf(LevelInfo,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SWarning(a ...interface{}) {
|
func (logger *Logger) SWarning(a ...interface{}) {
|
||||||
logger.doSPrintf(LevelWarning,a)
|
logger.DoSPrintf(LevelWarning,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SError(a ...interface{}) {
|
func (logger *Logger) SError(a ...interface{}) {
|
||||||
logger.doSPrintf(LevelError,a)
|
logger.DoSPrintf(LevelError,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func STrace(a ...interface{}) {
|
func STrace(a ...interface{}) {
|
||||||
gLogger.doSPrintf(LevelTrace,a)
|
gLogger.DoSPrintf(LevelTrace,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SDebug(a ...interface{}) {
|
func SDebug(a ...interface{}) {
|
||||||
gLogger.doSPrintf(LevelDebug,a)
|
gLogger.DoSPrintf(LevelDebug,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SInfo(a ...interface{}) {
|
func SInfo(a ...interface{}) {
|
||||||
gLogger.doSPrintf(LevelInfo,a)
|
gLogger.DoSPrintf(LevelInfo,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SWarning(a ...interface{}) {
|
func SWarning(a ...interface{}) {
|
||||||
gLogger.doSPrintf(LevelWarning,a)
|
gLogger.DoSPrintf(LevelWarning,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SError(a ...interface{}) {
|
func SError(a ...interface{}) {
|
||||||
gLogger.doSPrintf(LevelError,a)
|
gLogger.DoSPrintf(LevelError,a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) formatHeader(buf *Buffer,level slog.Level,calldepth int) {
|
func (logger *Logger) FormatHeader(buf *Buffer,level slog.Level,calldepth int) {
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
var file string
|
var file string
|
||||||
var line int
|
var line int
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ type RawMessageInfo struct {
|
|||||||
msgHandler RawMessageHandler
|
msgHandler RawMessageHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
type RawMessageHandler func(clientId uint64,packType uint16,msg []byte)
|
type RawMessageHandler func(clientId string,packType uint16,msg []byte)
|
||||||
type RawConnectHandler func(clientId uint64)
|
type RawConnectHandler func(clientId string)
|
||||||
type UnknownRawMessageHandler func(clientId uint64,msg []byte)
|
type UnknownRawMessageHandler func(clientId string,msg []byte)
|
||||||
|
|
||||||
const RawMsgTypeSize = 2
|
const RawMsgTypeSize = 2
|
||||||
type PBRawProcessor struct {
|
type PBRawProcessor struct {
|
||||||
@@ -38,14 +38,14 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId uint64, msg interface{}) error{
|
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId string, msg interface{}) error{
|
||||||
pPackInfo := msg.(*PBRawPackInfo)
|
pPackInfo := msg.(*PBRawPackInfo)
|
||||||
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId uint64,data []byte) (interface{}, error) {
|
func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId string,data []byte) (interface{}, error) {
|
||||||
var msgType uint16
|
var msgType uint16
|
||||||
if pbRawProcessor.LittleEndian == true {
|
if pbRawProcessor.LittleEndian == true {
|
||||||
msgType = binary.LittleEndian.Uint16(data[:2])
|
msgType = binary.LittleEndian.Uint16(data[:2])
|
||||||
@@ -57,7 +57,7 @@ func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId uint64,data []byte) (i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbRawProcessor *PBRawProcessor ) Marshal(clientId uint64,msg interface{}) ([]byte, error){
|
func (pbRawProcessor *PBRawProcessor ) Marshal(clientId string,msg interface{}) ([]byte, error){
|
||||||
pMsg := msg.(*PBRawPackInfo)
|
pMsg := msg.(*PBRawPackInfo)
|
||||||
|
|
||||||
buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize)
|
buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize)
|
||||||
@@ -80,7 +80,7 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRaw
|
|||||||
pbRawPackInfo.rawMsg = msg
|
pbRawPackInfo.rawMsg = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId uint64,msg interface{}){
|
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string,msg interface{}){
|
||||||
if pbRawProcessor.unknownMessageHandler == nil {
|
if pbRawProcessor.unknownMessageHandler == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -88,11 +88,11 @@ func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId uint64,msg interf
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect event
|
// connect event
|
||||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId uint64){
|
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId string){
|
||||||
pbRawProcessor.connectHandler(clientId)
|
pbRawProcessor.connectHandler(clientId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(clientId uint64){
|
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(clientId string){
|
||||||
pbRawProcessor.disconnectHandler(clientId)
|
pbRawProcessor.disconnectHandler(clientId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ func initLog() error {
|
|||||||
fmt.Printf("cannot create log file!\n")
|
fmt.Printf("cannot create log file!\n")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Export(logger)
|
log.SetLogger(logger)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
189
sysmodule/ginmodule/GinModule.go
Normal file
189
sysmodule/ginmodule/GinModule.go
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
package ginmodule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"datacenter/common/processor"
|
||||||
|
"github.com/duanhf2012/origin/v2/event"
|
||||||
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
|
"github.com/duanhf2012/origin/v2/service"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GinModule struct {
|
||||||
|
service.Module
|
||||||
|
|
||||||
|
*GinConf
|
||||||
|
*gin.Engine
|
||||||
|
srv *http.Server
|
||||||
|
|
||||||
|
processor []processor.IGinProcessor
|
||||||
|
}
|
||||||
|
|
||||||
|
type GinConf struct {
|
||||||
|
Addr string
|
||||||
|
}
|
||||||
|
|
||||||
|
const Sys_Event_Gin_Event event.EventType = -11
|
||||||
|
|
||||||
|
func (gm *GinModule) Init(conf *GinConf, engine *gin.Engine) {
|
||||||
|
gm.GinConf = conf
|
||||||
|
gm.Engine = engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SetupDataProcessor(processor ...processor.IGinProcessor) {
|
||||||
|
gm.processor = processor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) AppendDataProcessor(processor ...processor.IGinProcessor) {
|
||||||
|
gm.processor = append(gm.processor, processor...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) OnInit() error {
|
||||||
|
if gm.Engine == nil {
|
||||||
|
gm.Engine = gin.Default()
|
||||||
|
}
|
||||||
|
|
||||||
|
gm.srv = &http.Server{
|
||||||
|
Addr: gm.Addr,
|
||||||
|
Handler: gm.Engine,
|
||||||
|
}
|
||||||
|
|
||||||
|
gm.Engine.Use(Logger())
|
||||||
|
gm.Engine.Use(gin.Recovery())
|
||||||
|
gm.GetEventProcessor().RegEventReceiverFunc(Sys_Event_Gin_Event, gm.GetEventHandler(), gm.eventHandler)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) eventHandler(ev event.IEvent) {
|
||||||
|
ginEvent := ev.(*GinEvent)
|
||||||
|
for _, handler := range ginEvent.handlersChain {
|
||||||
|
handler(ginEvent.c)
|
||||||
|
}
|
||||||
|
|
||||||
|
ginEvent.chanWait <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) Start() {
|
||||||
|
log.Info("http start listen", slog.Any("addr", gm.Addr))
|
||||||
|
go func() {
|
||||||
|
err := gm.srv.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("ListenAndServe error", slog.Any("error", err.Error()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) StartTLS(certFile, keyFile string) {
|
||||||
|
log.Info("http start listen", slog.Any("addr", gm.Addr))
|
||||||
|
go func() {
|
||||||
|
err := gm.srv.ListenAndServeTLS(certFile, keyFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("ListenAndServeTLS error", slog.Any("error", err.Error()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) Stop(ctx context.Context) {
|
||||||
|
if err := gm.srv.Shutdown(ctx); err != nil {
|
||||||
|
log.SError("Server Shutdown", slog.Any("error", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GinEvent struct {
|
||||||
|
handlersChain gin.HandlersChain
|
||||||
|
chanWait chan struct{}
|
||||||
|
c *gin.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ge *GinEvent) GetEventType() event.EventType {
|
||||||
|
return Sys_Event_Gin_Event
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.Engine.Handle(httpMethod, relativePath, func(c *gin.Context) {
|
||||||
|
for _, p := range gm.processor {
|
||||||
|
_, err := p.Process(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ev GinEvent
|
||||||
|
chanWait := make(chan struct{})
|
||||||
|
ev.chanWait = chanWait
|
||||||
|
ev.handlersChain = handlers
|
||||||
|
ev.c = c
|
||||||
|
gm.NotifyEvent(&ev)
|
||||||
|
|
||||||
|
<-chanWait
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SafeGET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.handleMethod(http.MethodGet, relativePath, handlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SafePOST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.handleMethod(http.MethodPost, relativePath, handlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SafeDELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.handleMethod(http.MethodDelete, relativePath, handlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SafePATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.handleMethod(http.MethodPatch, relativePath, handlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gm *GinModule) SafePut(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||||
|
return gm.handleMethod(http.MethodPut, relativePath, handlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIPWithProxyHeaders(c *gin.Context) string {
|
||||||
|
// 尝试从 X-Real-IP 头部获取真实 IP
|
||||||
|
ip := c.GetHeader("X-Real-IP")
|
||||||
|
|
||||||
|
// 如果 X-Real-IP 头部不存在,则尝试从 X-Forwarded-For 头部获取
|
||||||
|
if ip == "" {
|
||||||
|
ip = c.GetHeader("X-Forwarded-For")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果两者都不存在,则使用默认的 ClientIP 方法获取 IP
|
||||||
|
if ip == "" {
|
||||||
|
ip = c.ClientIP()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIPWithValidatedProxyHeaders(c *gin.Context) string {
|
||||||
|
// 获取代理头部
|
||||||
|
proxyHeaders := c.Request.Header.Get("X-Real-IP,X-Forwarded-For")
|
||||||
|
|
||||||
|
// 分割代理头部,取第一个 IP 作为真实 IP
|
||||||
|
ips := strings.Split(proxyHeaders, ",")
|
||||||
|
ip := strings.TrimSpace(ips[0])
|
||||||
|
|
||||||
|
// 如果 IP 格式合法,则使用获取到的 IP,否则使用默认的 ClientIP 方法获取
|
||||||
|
if isValidIP(ip) {
|
||||||
|
return ip
|
||||||
|
} else {
|
||||||
|
ip = c.ClientIP()
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isValidIP 判断 IP 格式是否合法
|
||||||
|
func isValidIP(ip string) bool {
|
||||||
|
// 此处添加自定义的 IP 格式验证逻辑
|
||||||
|
// 例如,使用正则表达式验证 IP 格式
|
||||||
|
// ...
|
||||||
|
if ip == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
79
sysmodule/ginmodule/logger.go
Normal file
79
sysmodule/ginmodule/logger.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package ginmodule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duanhf2012/origin/log"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger 是一个自定义的日志中间件
|
||||||
|
func Logger() gin.HandlerFunc {
|
||||||
|
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// 处理请求前记录日志
|
||||||
|
// 开始时间
|
||||||
|
startTime := time.Now()
|
||||||
|
// 调用该请求的剩余处理程序
|
||||||
|
c.Next()
|
||||||
|
// 结束时间
|
||||||
|
endTime := time.Now()
|
||||||
|
// 执行时间
|
||||||
|
latencyTime := endTime.Sub(startTime)
|
||||||
|
|
||||||
|
// 请求IP
|
||||||
|
clientIP := c.ClientIP()
|
||||||
|
// remoteIP := c.RemoteIP()
|
||||||
|
|
||||||
|
// 请求方式
|
||||||
|
reqMethod := c.Request.Method
|
||||||
|
// 请求路由
|
||||||
|
reqUri := c.Request.RequestURI
|
||||||
|
// 请求协议
|
||||||
|
reqProto := c.Request.Proto
|
||||||
|
// 请求来源
|
||||||
|
repReferer := c.Request.Referer()
|
||||||
|
// 请求UA
|
||||||
|
reqUA := c.Request.UserAgent()
|
||||||
|
|
||||||
|
// 请求响应内容长度
|
||||||
|
resLength := c.Writer.Size()
|
||||||
|
if resLength < 0 {
|
||||||
|
resLength = 0
|
||||||
|
}
|
||||||
|
// 响应状态码
|
||||||
|
statusCode := c.Writer.Status()
|
||||||
|
|
||||||
|
log.SDebug(fmt.Sprintf(
|
||||||
|
"%s | %3d | %s %10s | \033[44;37m%-6s\033[0m %s %s | %10v | \"%s\" \"%s\"",
|
||||||
|
colorForStatus(statusCode),
|
||||||
|
statusCode,
|
||||||
|
colorForStatus(0),
|
||||||
|
clientIP,
|
||||||
|
// remoteIP,
|
||||||
|
reqMethod,
|
||||||
|
reqUri,
|
||||||
|
reqProto,
|
||||||
|
latencyTime,
|
||||||
|
reqUA,
|
||||||
|
repReferer,
|
||||||
|
))
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// colorForStatus 根据 HTTP 状态码返回 ANSI 颜色代码
|
||||||
|
func colorForStatus(code int) string {
|
||||||
|
switch {
|
||||||
|
case code >= 200 && code < 300:
|
||||||
|
return "\033[42;1;37m" // green
|
||||||
|
case code >= 300 && code < 400:
|
||||||
|
return "\033[34m" // blue
|
||||||
|
case code >= 400 && code < 500:
|
||||||
|
return "\033[33m" // yellow
|
||||||
|
case code == 0:
|
||||||
|
return "\033[0m" // cancel
|
||||||
|
default:
|
||||||
|
return "\033[31m" // red
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
package mongomodule
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
"gopkg.in/mgo.v2"
|
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
"container/heap"
|
|
||||||
_ "gopkg.in/mgo.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// session
|
|
||||||
type Session struct {
|
|
||||||
*mgo.Session
|
|
||||||
ref int
|
|
||||||
index int
|
|
||||||
}
|
|
||||||
|
|
||||||
type SessionHeap []*Session
|
|
||||||
|
|
||||||
func (h SessionHeap) Len() int {
|
|
||||||
return len(h)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h SessionHeap) Less(i, j int) bool {
|
|
||||||
return h[i].ref < h[j].ref
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h SessionHeap) Swap(i, j int) {
|
|
||||||
h[i], h[j] = h[j], h[i]
|
|
||||||
h[i].index = i
|
|
||||||
h[j].index = j
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *SessionHeap) Push(s interface{}) {
|
|
||||||
s.(*Session).index = len(*h)
|
|
||||||
*h = append(*h, s.(*Session))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *SessionHeap) Pop() interface{} {
|
|
||||||
l := len(*h)
|
|
||||||
s := (*h)[l-1]
|
|
||||||
s.index = -1
|
|
||||||
*h = (*h)[:l-1]
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
type DialContext struct {
|
|
||||||
sync.Mutex
|
|
||||||
sessions SessionHeap
|
|
||||||
}
|
|
||||||
|
|
||||||
type MongoModule struct {
|
|
||||||
dailContext *DialContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *MongoModule) Init(url string,sessionNum uint32,dialTimeout time.Duration, timeout time.Duration) error {
|
|
||||||
var err error
|
|
||||||
slf.dailContext, err = dialWithTimeout(url, sessionNum, dialTimeout, timeout)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *MongoModule) Ref() *Session{
|
|
||||||
return slf.dailContext.Ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *MongoModule) UnRef(s *Session) {
|
|
||||||
slf.dailContext.UnRef(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func dialWithTimeout(url string, sessionNum uint32, dialTimeout time.Duration, timeout time.Duration) (*DialContext, error) {
|
|
||||||
if sessionNum <= 0 {
|
|
||||||
sessionNum = 100
|
|
||||||
log.Release("invalid sessionNum, reset to %v", sessionNum)
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := mgo.DialWithTimeout(url, dialTimeout)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.SetMode(mgo.Strong,true)
|
|
||||||
s.SetSyncTimeout(timeout)
|
|
||||||
s.SetSocketTimeout(timeout)
|
|
||||||
|
|
||||||
c := new(DialContext)
|
|
||||||
|
|
||||||
// sessions
|
|
||||||
c.sessions = make(SessionHeap, sessionNum)
|
|
||||||
c.sessions[0] = &Session{s, 0, 0}
|
|
||||||
for i := 1; i < int(sessionNum); i++ {
|
|
||||||
c.sessions[i] = &Session{s.New(), 0, i}
|
|
||||||
}
|
|
||||||
heap.Init(&c.sessions)
|
|
||||||
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (c *DialContext) Close() {
|
|
||||||
c.Lock()
|
|
||||||
for _, s := range c.sessions {
|
|
||||||
s.Close()
|
|
||||||
}
|
|
||||||
c.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (c *DialContext) Ref() *Session {
|
|
||||||
c.Lock()
|
|
||||||
s := c.sessions[0]
|
|
||||||
if s.ref == 0 {
|
|
||||||
s.Refresh()
|
|
||||||
}
|
|
||||||
s.ref++
|
|
||||||
heap.Fix(&c.sessions, 0)
|
|
||||||
c.Unlock()
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (c *DialContext) UnRef(s *Session) {
|
|
||||||
if s == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Lock()
|
|
||||||
s.ref--
|
|
||||||
heap.Fix(&c.sessions, s.index)
|
|
||||||
c.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (s *Session) EnsureCounter(db string, collection string, id string) error {
|
|
||||||
err := s.DB(db).C(collection).Insert(bson.M{
|
|
||||||
"_id": id,
|
|
||||||
"seq": 0,
|
|
||||||
})
|
|
||||||
if mgo.IsDup(err) {
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (s *Session) NextSeq(db string, collection string, id string) (int, error) {
|
|
||||||
var res struct {
|
|
||||||
Seq int
|
|
||||||
}
|
|
||||||
_, err := s.DB(db).C(collection).FindId(id).Apply(mgo.Change{
|
|
||||||
Update: bson.M{"$inc": bson.M{"seq": 1}},
|
|
||||||
ReturnNew: true,
|
|
||||||
}, &res)
|
|
||||||
|
|
||||||
return res.Seq, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (s *Session) EnsureIndex(db string, collection string, key []string, bBackground bool) error {
|
|
||||||
return s.DB(db).C(collection).EnsureIndex(mgo.Index{
|
|
||||||
Key: key,
|
|
||||||
Unique: false,
|
|
||||||
Sparse: true,
|
|
||||||
Background: bBackground,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// goroutine safe
|
|
||||||
func (s *Session) EnsureUniqueIndex(db string, collection string, key []string, bBackground bool) error {
|
|
||||||
return s.DB(db).C(collection).EnsureIndex(mgo.Index{
|
|
||||||
Key: key,
|
|
||||||
Unique: true,
|
|
||||||
Sparse: true,
|
|
||||||
Background: bBackground,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package mongomodule
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
_ "gopkg.in/mgo.v2"
|
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type Student struct {
|
|
||||||
ID bson.ObjectId `bson:"_id"`
|
|
||||||
Name string `bson: "name"`
|
|
||||||
Age int `bson: "age"`
|
|
||||||
Sid string `bson: "sid"`
|
|
||||||
Status int `bson: "status"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StudentName struct {
|
|
||||||
Name string `bson: "name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func Test_Example(t *testing.T) {
|
|
||||||
module:=MongoModule{}
|
|
||||||
module.Init("mongodb://admin:123456@192.168.2.119:27017",100, 5*time.Second,5*time.Second)
|
|
||||||
|
|
||||||
// take session
|
|
||||||
s := module.Take()
|
|
||||||
c := s.DB("test2").C("t_student")
|
|
||||||
|
|
||||||
//2.定义对象
|
|
||||||
insertData := Student{
|
|
||||||
ID:bson.NewObjectId(),
|
|
||||||
Name: "seeta11",
|
|
||||||
Age: 35, //*^_^*
|
|
||||||
Sid: "s20180907",
|
|
||||||
Status: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
updateData := Student{
|
|
||||||
Name: "seeta11",
|
|
||||||
Age: 18,
|
|
||||||
Sid: "s20180907",
|
|
||||||
Status: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//3.插入数据
|
|
||||||
err := c.Insert(&insertData)
|
|
||||||
|
|
||||||
//4.查找数据
|
|
||||||
selector := bson.M{"_id":bson.ObjectIdHex("5f25303e999c622d361989b0")}
|
|
||||||
m:=Student{}
|
|
||||||
err = c.Find(selector).One(&m)
|
|
||||||
|
|
||||||
//5.更新数据
|
|
||||||
//selector2 := bson.M{"_id":bson.ObjectIdHex("5f25303e999c622d361989b0")}
|
|
||||||
updateData.ID = bson.ObjectIdHex("5f25303e999c622d361989b0")
|
|
||||||
err = c.UpdateId(bson.ObjectIdHex("5f25303e999c622d361989b0"),&updateData)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//6.删除数据
|
|
||||||
err = c.RemoveId(bson.ObjectIdHex("5f252f09999c622d36198951"))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//7.序号自增
|
|
||||||
s.EnsureCounter("test2","t_student","5f252f09999c622d36198951")
|
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
id, err := s.NextSeq("test2", "t_student", "5f252f09999c622d36198951")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
//8.setoninsert使用
|
|
||||||
info,uErr := c.Upsert(bson.M{"_id":bson.ObjectIdHex("5f252f09999c622d36198951")},bson.M{
|
|
||||||
"$setOnInsert":bson.M{"Name":"setoninsert","Age":55}})
|
|
||||||
|
|
||||||
|
|
||||||
//9.修改部分数字数据
|
|
||||||
selector1 := bson.M{"_id":bson.ObjectIdHex("60473de655f1012e7453b369")}
|
|
||||||
update1 := bson.M{"$set":bson.M{"name":"xxxxx","age":1111}}
|
|
||||||
c.Update(selector1,update1)
|
|
||||||
|
|
||||||
fmt.Println(info,uErr)
|
|
||||||
}
|
|
||||||
@@ -357,7 +357,7 @@ func (mp *MongoPersist) removeRankData(rankId uint64,keys []uint64) bool {
|
|||||||
|
|
||||||
func (mp *MongoPersist) upsertToDB(collectName string,rankData *RankData) error{
|
func (mp *MongoPersist) upsertToDB(collectName string,rankData *RankData) error{
|
||||||
condition := bson.D{{"_id", rankData.Key}}
|
condition := bson.D{{"_id", rankData.Key}}
|
||||||
upsert := bson.M{"_id":rankData.Key,"RefreshTime": rankData.refreshTimestamp, "SortData": rankData.SortData, "Data": rankData.Data,"ExData":rankData.ExData}
|
upsert := bson.M{"_id":rankData.Key,"RefreshTime": rankData.RefreshTimestamp, "SortData": rankData.SortData, "Data": rankData.Data,"ExData":rankData.ExData}
|
||||||
update := bson.M{"$set": upsert}
|
update := bson.M{"$set": upsert}
|
||||||
|
|
||||||
s := mp.mongo.TakeSession()
|
s := mp.mongo.TakeSession()
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type RankData struct {
|
|||||||
Data []byte
|
Data []byte
|
||||||
ExData []int64
|
ExData []int64
|
||||||
|
|
||||||
refreshTimestamp int64 //刷新时间
|
RefreshTimestamp int64 //刷新时间
|
||||||
//bRelease bool
|
//bRelease bool
|
||||||
ref bool
|
ref bool
|
||||||
compareFunc func(other skip.Comparator) int
|
compareFunc func(other skip.Comparator) int
|
||||||
@@ -39,7 +39,7 @@ func NewRankData(isDec bool, data *rpc.RankData,refreshTimestamp int64) *RankDat
|
|||||||
ret.ExData = append(ret.ExData,d.InitValue+d.IncreaseValue)
|
ret.ExData = append(ret.ExData,d.InitValue+d.IncreaseValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.refreshTimestamp = refreshTimestamp
|
ret.RefreshTimestamp = refreshTimestamp
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -250,7 +250,7 @@ func (rs *RankSkip) UpsetRank(upsetData *rpc.RankData, refreshTimestamp int64, f
|
|||||||
//找到的情况对比排名数据是否有变化,无变化进行data更新,有变化则进行删除更新
|
//找到的情况对比排名数据是否有变化,无变化进行data更新,有变化则进行删除更新
|
||||||
if compareIsEqual(rankNode.SortData, upsetData.SortData) {
|
if compareIsEqual(rankNode.SortData, upsetData.SortData) {
|
||||||
rankNode.Data = upsetData.GetData()
|
rankNode.Data = upsetData.GetData()
|
||||||
rankNode.refreshTimestamp = refreshTimestamp
|
rankNode.RefreshTimestamp = refreshTimestamp
|
||||||
|
|
||||||
if fromLoad == false {
|
if fromLoad == false {
|
||||||
rs.rankModule.OnChangeRankData(rs, rankNode)
|
rs.rankModule.OnChangeRankData(rs, rankNode)
|
||||||
|
|||||||
Reference in New Issue
Block a user