From 3db83e115988e0a8c65a6fa290c72a33e87f4016 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 30 Apr 2020 22:22:35 +0800 Subject: [PATCH] improve package gtimer --- .example/encoding/gcompress/zip.go | 5 ++--- .example/other/test.go | 16 ++++++---------- os/gtimer/gtimer_entry.go | 6 ++++-- os/gtimer/gtimer_timer.go | 7 +++++++ 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.example/encoding/gcompress/zip.go b/.example/encoding/gcompress/zip.go index 8ac750ff6..1d5e0d41e 100644 --- a/.example/encoding/gcompress/zip.go +++ b/.example/encoding/gcompress/zip.go @@ -8,9 +8,8 @@ import ( func main() { err := gcompress.ZipPath( - `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg`, - `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg\encoding\gcompress\data.zip`, - "my-dir", + `/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/test`, + `/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/test.zip`, ) fmt.Println(err) } diff --git a/.example/other/test.go b/.example/other/test.go index 151284a4c..cd5867790 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,17 +1,13 @@ package main import ( - "github.com/gogf/gf/frame/g" - "github.com/gogf/gf/net/ghttp" + "fmt" + "github.com/gogf/gf/os/gtimer" + "time" ) func main() { - s := g.Server() - s.Group("/", func(group *ghttp.RouterGroup) { - group.ALL("/", func(r *ghttp.Request) { - r.Response.Write(r.GetBodyString()) - }) - }) - s.SetPort(8199) - s.Run() + tr := gtimer.New(100, 1*time.Second, 10) + tr.Add(1*time.Second, func() { fmt.Println("hello") }) + select {} } diff --git a/os/gtimer/gtimer_entry.go b/os/gtimer/gtimer_entry.go index ff128da45..3d1cfe763 100644 --- a/os/gtimer/gtimer_entry.go +++ b/os/gtimer/gtimer_entry.go @@ -34,8 +34,10 @@ func (w *wheel) addEntry(interval time.Duration, job JobFunc, singleton bool, ti if times <= 0 { times = gDEFAULT_TIMES } - ms := interval.Nanoseconds() / 1e6 - num := ms / w.intervalMs + var ( + ms = interval.Nanoseconds() / 1e6 + num = ms / w.intervalMs + ) if num == 0 { // If the given interval is lesser than the one of the wheel, // then sets it to one tick, which means it will be run in one interval. diff --git a/os/gtimer/gtimer_timer.go b/os/gtimer/gtimer_timer.go index 9eb2edfc4..f03a5118c 100644 --- a/os/gtimer/gtimer_timer.go +++ b/os/gtimer/gtimer_timer.go @@ -7,6 +7,7 @@ package gtimer import ( + "fmt" "time" "github.com/gogf/gf/container/glist" @@ -39,6 +40,9 @@ type wheel struct { // The optional parameter specifies the wheels count of the timer, // which is gDEFAULT_WHEEL_LEVEL in default. func New(slot int, interval time.Duration, level ...int) *Timer { + if slot <= 0 { + panic(fmt.Sprintf("invalid slot number: %d", slot)) + } length := gDEFAULT_WHEEL_LEVEL if len(level) > 0 { length = level[0] @@ -53,6 +57,9 @@ func New(slot int, interval time.Duration, level ...int) *Timer { for i := 0; i < length; i++ { if i > 0 { n := time.Duration(t.wheels[i-1].totalMs) * time.Millisecond + if n <= 0 { + panic(fmt.Sprintf(`inteval is too large with level: %dms x %d`, interval, length)) + } w := t.newWheel(i, slot, n) t.wheels[i] = w t.wheels[i-1].addEntry(n, w.proceed, false, gDEFAULT_TIMES, STATUS_READY)