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.
 
 
 

24 lines
443 B

  1. package ratelimit_test
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/VojtechVitek/ratelimit"
  6. )
  7. func ExampleThrottle() {
  8. middleware := ratelimit.Throttle(1)
  9. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. w.Write([]byte("working hard...\n\n"))
  11. if f, ok := w.(http.Flusher); ok {
  12. f.Flush()
  13. }
  14. time.Sleep(10 * time.Second)
  15. w.Write([]byte("done"))
  16. })
  17. http.ListenAndServe(":3333", middleware(handler))
  18. }