The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rate_limiter.go 577B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "time"
  4. "github.com/gin-gonic/gin"
  5. )
  6. const reqsPerSecond = 2000
  7. const sleepTime = time.Second / reqsPerSecond
  8. var limiter = make(chan struct{}, reqsPerSecond)
  9. func setUpLimiter() {
  10. for i := 0; i < 2000; i++ {
  11. limiter <- struct{}{}
  12. }
  13. go func() {
  14. for {
  15. limiter <- struct{}{}
  16. time.Sleep(sleepTime)
  17. }
  18. }()
  19. }
  20. func rateLimiter(onAnonymousOnly bool) func(c *gin.Context) {
  21. return func(c *gin.Context) {
  22. if onAnonymousOnly {
  23. ctx := getContext(c)
  24. if ctx.User.ID == 0 {
  25. <-limiter
  26. }
  27. } else {
  28. <-limiter
  29. }
  30. c.Next()
  31. }
  32. }