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.
 
 
 

47 lines
1.1 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/VojtechVitek/ratelimit"
  6. "github.com/VojtechVitek/ratelimit/memory"
  7. "github.com/VojtechVitek/ratelimit/redis"
  8. redigo "github.com/garyburd/redigo/redis"
  9. "github.com/pressly/chi"
  10. "github.com/pressly/chi/middleware"
  11. )
  12. var pool = &redigo.Pool{
  13. MaxIdle: 10,
  14. MaxActive: 50,
  15. IdleTimeout: 300 * time.Second,
  16. Wait: false, // Important
  17. Dial: func() (redigo.Conn, error) {
  18. c, err := redigo.DialTimeout("tcp", "127.0.0.1:6379", 200*time.Millisecond, 100*time.Millisecond, 100*time.Millisecond)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return c, err
  23. },
  24. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  25. _, err := c.Do("PING")
  26. return err
  27. },
  28. }
  29. // wget http://localhost:3333 -q --show-progress
  30. func main() {
  31. r := chi.NewRouter()
  32. r.Use(middleware.Logger)
  33. r.Use(ratelimit.DownloadSpeed(ratelimit.IP).Rate(1024, time.Second).LimitBy(redis.New(pool), memory.New()))
  34. r.Get("/", ServeVideo)
  35. http.ListenAndServe(":3333", r)
  36. }
  37. func ServeVideo(w http.ResponseWriter, r *http.Request) {
  38. http.ServeFile(w, r, "/Users/vojtechvitek/Desktop/govideo.mov")
  39. }