mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve package gtimer
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
@ -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 {}
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user