created "Node"

This commit is contained in:
Jae Kwon
2014-07-09 18:33:44 -07:00
parent 237b3dc7ff
commit 1b59caf950
8 changed files with 121 additions and 18 deletions
+5 -1
View File
@@ -2,7 +2,10 @@ package common
import "time"
/* RepeatTimer */
/*
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
It's good for keeping connections alive.
*/
type RepeatTimer struct {
Ch chan struct{}
quit chan struct{}
@@ -26,6 +29,7 @@ func (t *RepeatTimer) fireHandler() {
}
}
// Wait the duration again before firing.
func (t *RepeatTimer) Reset() {
t.timer.Reset(t.dur)
}
+5 -1
View File
@@ -5,7 +5,10 @@ import (
"time"
)
/* Throttler */
/*
Throttler sends a struct{}{} to .Ch "dur" after the last .Set().
It's good for ensuring that something happens last after a burst of events.
*/
type Throttler struct {
Ch chan struct{}
quit chan struct{}
@@ -19,6 +22,7 @@ func NewThrottler(dur time.Duration) *Throttler {
var quit = make(chan struct{})
var t = &Throttler{Ch: ch, dur: dur, quit: quit}
t.timer = time.AfterFunc(dur, t.fireHandler)
t.timer.Stop()
return t
}