improve package gtimer

This commit is contained in:
John
2020-04-30 22:22:35 +08:00
parent 3bb002796c
commit 3db83e1159
4 changed files with 19 additions and 15 deletions

View File

@ -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)
}

View File

@ -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 {}
}

View File

@ -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.

View File

@ -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 <level> 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)