From 2660a713e45ff0790522acd28111d99e139ca432 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 13 Aug 2023 11:37:33 +0800 Subject: [PATCH] 1 --- app.go | 21 +++++++++++------ global.go | 6 ++--- httpserver.go | 2 +- timer.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 timer.go diff --git a/app.go b/app.go index 26cc86b..e7f0f7b 100644 --- a/app.go +++ b/app.go @@ -58,7 +58,8 @@ func (this *app) init(userApp UserApp) { this.userApp = userApp this.nowTime = time.Now() atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano()) - _Timer = &q5.XTimer{} + _Timer = &Timer{} + /* _Timer.Init( func (context interface{}) int64 { return q5.GetTickCount() @@ -66,13 +67,19 @@ func (this *app) init(userApp UserApp) { nil, 1000 * 60, 5000) + */ _SysLog = new(SysLog_) _SysLog.Init() _TgLog = new(TGLog_) _TgLog.Init() - //flag.IntVar(&this.nodeId, "n", 0, "node id") - //flag.IntVar(&this.instanceId, "i", 0, "instance id") - flag.Parse() + { + var tmpNodeId, tmpInstanceId int + flag.IntVar(&tmpNodeId, "n", 0, "node id") + flag.IntVar(&tmpInstanceId, "i", 0, "instance id") + this.nodeId = int32(tmpNodeId) + this.instanceId = int32(tmpInstanceId) + flag.Parse() + } this.loopCond = sync.NewCond(new(sync.Mutex)) this.chGoLoopTimerExit = make(chan int) this.chGoLoopWait = make(chan int64) @@ -88,7 +95,7 @@ func (this *app) init(userApp UserApp) { func (this *app) unInit() { this.chGoLoopTimerExit <- 1 - _Timer.UnInit() + //_Timer.UnInit() _Timer = nil this.userApp.UnInit() } @@ -180,7 +187,7 @@ func (this *app) goLoopTimer() { } func (this *app) schedule() { - this.chGoLoopWait <- Timer().GetIdleTime() + this.chGoLoopWait <- GetTimer().GetIdleTime() this.loopCond.L.Lock() this.loopCond.Wait() this.loopCond.L.Unlock() @@ -222,7 +229,7 @@ func (this *app) dispatchIMMsg() { } func (this *app) installTimer() { - Timer().SetInterval(1000 * 60, + GetTimer().SetInterval(1000 * 60, func (ev int32, params *q5.Args) { if ev == q5.TIMER_EXEC_EVENT { SysLog().Info("max_run_delay:%d max_schedule_time:%d", diff --git a/global.go b/global.go index 877aa00..1c17c5a 100644 --- a/global.go +++ b/global.go @@ -1,13 +1,11 @@ package f5 -import "q5" - var _app *app -var _Timer *q5.XTimer +var _Timer *Timer var _SysLog *SysLog_ var _TgLog *TGLog_ -func Timer() *q5.XTimer { +func GetTimer() *Timer { return _Timer } diff --git a/httpserver.go b/httpserver.go index c0def9e..81d2b7e 100644 --- a/httpserver.go +++ b/httpserver.go @@ -42,7 +42,7 @@ func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServe }) SysLog().Info("HttpServer.Init") if logOutputTime > 0 { - Timer().SetInterval( + GetTimer().SetInterval( logOutputTime, func (ev int32, params *q5.Args) { if ev == q5.TIMER_EXEC_EVENT { diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..2c9895e --- /dev/null +++ b/timer.go @@ -0,0 +1,65 @@ +package f5 + +import ( + "q5" +) + +type TimerWp struct { + *q5.XTimerWp +} + +type TimerAttacher struct { + q5.XTimerAttacher +} + +type Timer struct { + timer *q5.XTimer +} + +func (this *Timer) Update() { + this.timer.Update() +} + +func (this *Timer) SetTimeout(expireTime int32, cb q5.TimerCb) { + this.timer.SetTimeout(expireTime, cb) +} + +func (this *Timer) SetTimeoutEx(expireTime int32, cb q5.TimerCb, ac *TimerAttacher) { + this.timer.SetTimeoutEx(expireTime, cb, &ac.XTimerAttacher) +} + +func (this *Timer) SetTimeoutWp(expireTime int32, cb q5.TimerCb) *TimerWp { + wp := TimerWp{} + wp.XTimerWp = this.timer.SetTimeoutWp(expireTime, cb) + return &wp +} + +func (this *Timer) SetTimeoutExWp(expireTime int32, cb q5.TimerCb, ac *TimerAttacher) *TimerWp { + wp := TimerWp{} + wp.XTimerWp = this.timer.SetTimeoutExWp(expireTime, cb, &ac.XTimerAttacher) + return &wp +} + +func (this *Timer) SetInterval(expireTime int32, cb q5.TimerCb) { + this.timer.SetInterval(expireTime, cb) +} + +func (this *Timer) SetIntervalEx(expireTime int32, cb q5.TimerCb, ac *TimerAttacher) { + this.timer.SetIntervalEx(expireTime, cb, &ac.XTimerAttacher) +} + +func (this *Timer) SetIntervalWp(expireTime int32, cb q5.TimerCb) *TimerWp { + wp := TimerWp{} + wp.XTimerWp = this.timer.SetIntervalWp(expireTime, cb) + return &wp +} + +func (this *Timer) SetIntervalExWp(expireTime int32, cb q5.TimerCb, ac *TimerAttacher) *TimerWp { + wp := TimerWp{} + wp.XTimerWp = this.timer.SetIntervalExWp(expireTime, cb, &ac.XTimerAttacher) + return &wp +} + +func (this *Timer) GetIdleTime() int64 { + return this.GetIdleTime() +}