-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
william
committed
Mar 11, 2022
0 parents
commit 4b7cdd9
Showing
8 changed files
with
698 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jursonmo/timer" | ||
) | ||
|
||
func main() { | ||
var testWheel = timer.NewWheel(1 * time.Millisecond) | ||
//n := 3000 | ||
n := 1000 | ||
var mu sync.Mutex | ||
stopTimerMap := make(map[int]*timer.Timer) | ||
timerMap := make(map[int]*timer.Timer) | ||
f := func(t time.Time, args ...interface{}) { | ||
mu.Lock() | ||
index := args[0].(int) | ||
st, ok := stopTimerMap[index] | ||
if ok { | ||
log.Fatalf("index:%d timer:%s has Stop, but still exec", index, st.Info()) | ||
} | ||
delete(timerMap, index) | ||
mu.Unlock() | ||
} | ||
for i := 0; i < n; i++ { | ||
mu.Lock() | ||
t := testWheel.NewTimerFunc(time.Millisecond*500, f, i) | ||
timerMap[i] = t | ||
mu.Unlock() | ||
|
||
if i/1000 > 0 { | ||
time.Sleep(time.Millisecond * time.Duration(i/1000)) | ||
} | ||
} | ||
|
||
//defaultTimerSize = 128 | ||
//大于128后,append t时,会创建新的内存, t.vec指向老的内存,这样t.Stop() 按道理是无法删除 | ||
mu.Lock() | ||
for j := 0; j < 10; j++ { | ||
st, ok := timerMap[j] | ||
if !ok { | ||
log.Fatalf("index:%, not exsit\n", j) | ||
} | ||
b := st.Stop() | ||
if !b { | ||
log.Printf("Stop index:%d,timer fail\n", j) //如果成功Stop 0-9 的timer, 按道理这10 timer是不会被执行的, 如果执行了,就证明Stop实际没有成功。 | ||
continue | ||
} | ||
stopTimerMap[j] = st | ||
} | ||
mu.Unlock() | ||
|
||
time.Sleep(time.Second) | ||
if len(timerMap) > 0 { | ||
log.Printf("len(timerMap):%d\n", len(timerMap)) | ||
for index := range timerMap { | ||
log.Printf("index:%d\n", index) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/jursonmo/timer" | ||
) | ||
|
||
func main() { | ||
var testWheel = timer.NewWheel(1 * time.Millisecond) | ||
|
||
// check timer | ||
t1 := testWheel.NewTimer(500 * time.Millisecond) | ||
|
||
before := time.Now() | ||
<-t1.C | ||
|
||
after := time.Now() | ||
println(after.Sub(before).String()) | ||
|
||
ok := t1.Stop() | ||
if ok { | ||
log.Fatal("should not ok") | ||
} | ||
|
||
//check timer.Stop() | ||
go func() { | ||
t2 := testWheel.NewTimer(10 * time.Millisecond) | ||
ok := t2.Stop() | ||
if !ok { | ||
log.Fatal("should ok") | ||
} | ||
before = time.Now() | ||
<-t1.C //已经Stop 了,会一直阻塞 | ||
log.Fatal("can't be here") | ||
}() | ||
|
||
//check NewTimerFunc timer | ||
arg0 := "arg0" | ||
arg1 := "arg1" | ||
start := time.Now() | ||
var t3 *timer.Timer | ||
f := func(t time.Time, args ...interface{}) { | ||
log.Printf("t3 func exec after %v\n", t.Sub(start)) | ||
if args[0].(string) != arg0 { | ||
log.Fatal("should arg0") | ||
} | ||
if args[1].(string) != arg1 { | ||
log.Fatal("should arg1") | ||
} | ||
ok := t3.Stop() | ||
if ok { | ||
log.Fatal("t3 should not be Stop") | ||
} | ||
} | ||
t3 = testWheel.NewTimerFunc(time.Millisecond*10, f, arg0, arg1) | ||
|
||
time.Sleep(time.Second) | ||
|
||
//----------------- | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/jursonmo/timer | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package timer | ||
|
||
// import ( | ||
// "time" | ||
// ) | ||
|
||
// type Timer struct { | ||
// C <-chan time.Time | ||
// r *timer | ||
// } | ||
|
||
// func After(d time.Duration) <-chan time.Time { | ||
// return defaultWheel.After(d) | ||
// } | ||
|
||
// func Sleep(d time.Duration) { | ||
// defaultWheel.Sleep(d) | ||
// } | ||
|
||
// func AfterFunc(d time.Duration, f func()) *Timer { | ||
// return defaultWheel.AfterFunc(d, f) | ||
// } | ||
|
||
// func NewTimer(d time.Duration) *Timer { | ||
// return defaultWheel.NewTimer(d) | ||
// } | ||
|
||
// func (t *Timer) Reset(d time.Duration) { | ||
// t.r.w.resetTimer(t.r, d, 0) | ||
// } | ||
|
||
// func (t *Timer) Stop() { | ||
// t.r.w.delTimer(t.r) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package timer | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Ticker struct { | ||
C <-chan time.Time | ||
r *timer | ||
} | ||
|
||
func NewTicker(d time.Duration) *Ticker { | ||
return defaultWheel.NewTicker(d) | ||
} | ||
|
||
func TickFunc(d time.Duration, f func()) *Ticker { | ||
return defaultWheel.TickFunc(d, f) | ||
} | ||
|
||
func Tick(d time.Duration) <-chan time.Time { | ||
return defaultWheel.Tick(d) | ||
} | ||
|
||
func (t *Ticker) Stop() { | ||
t.r.w.delTimer(t.r) | ||
} | ||
|
||
func (t *Ticker) Reset(d time.Duration) { | ||
t.r.w.resetTimer(t.r, d, d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package timer | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Timer struct { | ||
C <-chan time.Time | ||
r *timer | ||
} | ||
|
||
func After(d time.Duration) <-chan time.Time { | ||
return defaultWheel.After(d) | ||
} | ||
|
||
func Sleep(d time.Duration) { | ||
defaultWheel.Sleep(d) | ||
} | ||
|
||
func AfterFunc(d time.Duration, f func()) *Timer { | ||
return defaultWheel.AfterFunc(d, f) | ||
} | ||
|
||
func NewTimer(d time.Duration) *Timer { | ||
return defaultWheel.NewTimer(d) | ||
} | ||
|
||
func (t *Timer) Reset(d time.Duration) bool { | ||
return t.r.w.resetTimer(t.r, d, 0) | ||
} | ||
|
||
func (t *Timer) Stop() bool { | ||
return t.r.w.delTimer(t.r) | ||
} | ||
|
||
func NewTimerFunc(d time.Duration, f func(time.Time, ...interface{}), arg ...interface{}) *Timer { | ||
return defaultWheel.NewTimerFunc(d, f, arg...) | ||
} | ||
|
||
func (t *Timer) Info() string { | ||
return fmt.Sprintf("expires:%d, period:%d, args:%v", t.r.expires, t.r.period, t.r.arg) | ||
} |
Oops, something went wrong.