Skip to content

Commit

Permalink
优化stop和reset
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPlayer committed May 9, 2022
1 parent 771efe7 commit aac4685
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions timewatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,20 @@ func (w *TimeWatch) AfterFunc(t time.Duration, c Watch, f func()) (r *time.Timer
}

func (w *TimeWatch) Stop(field string) bool {
timer, ok := w.Timer[field]
if !ok {
return false
}
_ = w.cache.HDel(w.key, field)
return w.Timer[field].Stop()
return timer.Stop()
}

func (w *TimeWatch) Reset(field string, d time.Duration) bool {
timer, ok := w.Timer[field]
if !ok {
return false
}

get, err := w.cache.HGet(w.key, field)
if err != nil {
return false
Expand All @@ -108,13 +117,13 @@ func (w *TimeWatch) Reset(field string, d time.Duration) bool {
}
c.TouchOffUnix = time.Now().Unix() + int64(d.Seconds())

if !w.Timer[field].Stop() {
if !timer.Stop() {
select {
case <-w.Timer[field].C: // try to drain the channel
case <-timer.C: // try to drain the channel
default:
}
}
return w.Timer[field].Reset(d)
return timer.Reset(d)
}

const LockKey = "CheckLock"
Expand Down

0 comments on commit aac4685

Please sign in to comment.