Created
May 7, 2018 18:07
-
-
Save nhocki/007f5dd7b86dc57185cd7680304e1274 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"log" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
"foo/bar/splice/ratelimiter" | |
) | |
var ( | |
releaseSpreadTime = 10 * time.Minute | |
interval = 10 * time.Second | |
usersPerInterval = int64(5) | |
limiter *ratelimiter.RateLimiter | |
) | |
func main() { | |
sigs := make(chan os.Signal, 1) | |
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | |
log.Printf("Ratelimiter at %d users every %s for %s\n", usersPerInterval, interval.String(), releaseSpreadTime.String()) | |
limiter = ratelimiter.New(interval, usersPerInterval, releaseSpreadTime) | |
key := "foo" | |
for { | |
select { | |
case <-time.Tick(1 * time.Second): | |
if limiter.Limit(key) { | |
log.Printf("#### Throttled") | |
} else { | |
log.Println("Tick") | |
} | |
case <-sigs: | |
log.Println("Exit!") | |
os.Exit(0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment