-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autorelay: remove ticker in favor of a dynamic timer
- Loading branch information
1 parent
a0cf073
commit d5b6e06
Showing
2 changed files
with
49 additions
and
10 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
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,42 @@ | ||
package autorelay | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
type timer struct { | ||
timer *clock.Timer | ||
running bool | ||
read bool | ||
} | ||
|
||
func newTimer(cl clock.Clock) *timer { | ||
t := cl.Timer(100 * time.Hour) // There's no way to initialize a stopped timer | ||
t.Stop() | ||
return &timer{timer: t} | ||
} | ||
|
||
func (t *timer) Chan() <-chan time.Time { | ||
return t.timer.C | ||
} | ||
|
||
func (t *timer) Stop() { | ||
if !t.running { | ||
return | ||
} | ||
if !t.timer.Stop() && !t.read { | ||
<-t.timer.C | ||
} | ||
t.read = false | ||
} | ||
|
||
func (t *timer) SetRead() { | ||
t.read = true | ||
} | ||
|
||
func (t *timer) Reset(d time.Duration) { | ||
t.Stop() | ||
t.timer.Reset(d) | ||
} |