Created
February 18, 2016 14:26
event-limiter
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
type EventLimiter struct { | |
ticker time.Ticker | |
trigger chan bool | |
timePassed chan bool | |
} | |
func NewLimiter() *EventLimiter { | |
ticker := time.NewTicker(60 * time.Second) | |
trigger := make(chan bool, 1) | |
timePassed := make(chan bool, 1) | |
return EventLimiter{ticker, trigger, timePassed} | |
} | |
func (l EventLimiter) maintainLimiter() { | |
//goroutine for both fors | |
for { | |
<- l.ticker.C | |
select { | |
case l.timePassed <- true: | |
default: | |
} | |
} | |
} | |
func (l EventLimiter) limit(f func()) { | |
<- l.timePassed | |
<- l.trigger | |
f() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment