Imroved and added unsubscribe method
@@ -12,14 +12,13 @@ import (
type LayerTimer chan types.LayerID
type Ticker struct {
- subscribes []LayerTimer
nextLayerToTick types.LayerID
m sync.RWMutex
tickInterval time.Duration
startEpoch time.Time
time Clock
stop chan struct{}
- ids map[LayerTimer]int
+ subscribers map[LayerTimer]int // map subscriber channel to its id
started bool
}
@@ -35,13 +34,12 @@ func (RealClock) Now() time.Time {
func NewTicker(time Clock, tickInterval time.Duration, startEpoch time.Time) *Ticker {
t := &Ticker{
- subscribes: make([]LayerTimer, 0, 0),
nextLayerToTick: 1,
tickInterval: tickInterval,
startEpoch: startEpoch,
time: time,
stop: make(chan struct{}),
- ids: make(map[LayerTimer]int),
+ subscribers: make(map[LayerTimer]int),
}
t.init()
return t
@@ -76,9 +74,9 @@ func (t *Ticker) notifyOnTick() {
t.m.Lock()
log.Event().Info("release tick", log.LayerId(uint64(t.nextLayerToTick)))
- for _, ch := range t.subscribes {
+ for ch := range t.subscribers {
ch <- t.nextLayerToTick
- log.Debug("iv'e notified number : %v", t.ids[ch])
+ log.Debug("iv'e notified number : %v", t.subscribers[ch])
}
log.Debug("Ive notified all")
t.nextLayerToTick++
@@ -95,13 +93,18 @@ func (t *Ticker) GetCurrentLayer() types.LayerID {
func (t *Ticker) Subscribe() LayerTimer {
ch := make(LayerTimer)
t.m.Lock()
- t.ids[ch] = len(t.ids)
- t.subscribes = append(t.subscribes, ch)
+ t.subscribers[ch] = len(t.subscribers)
t.m.Unlock()
return ch
}
+func (t *Ticker) Unsubscribe(ch LayerTimer) {
+ t.m.Lock()
+ delete(t.subscribers, ch)
+ t.m.Unlock()
+}
+
func (t *Ticker) updateLayerID() {
t.nextLayerToTick = types.LayerID((t.time.Now().Sub(t.startEpoch) / t.tickInterval) + 2)
}
