Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Last active December 26, 2021 06:38
Show Gist options
  • Save jaydonnell/4fd14c69132734aac76b7f5386161696 to your computer and use it in GitHub Desktop.
Save jaydonnell/4fd14c69132734aac76b7f5386161696 to your computer and use it in GitHub Desktop.
Limit http requests to 5 per second in golang
package main
import (
"context"
"fmt"
"log"
"net/http"
"sync"
"time"
"golang.org/x/time/rate"
)
var url = "http://localhost:8888/"
func main() {
start := time.Now()
// Have a max rate of 5/sec; allow bursts of up-to 5.
rlim := rate.NewLimiter(5, 5)
var wg sync.WaitGroup
for i := 0; i < 30; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Wait for the rate limiter.
rlim.Wait(context.Background())
// make up to 3 attemps to fetch URL
for i := 1; i <= 3; i++ {
resp, err := http.Get(url)
if err != nil {
log.Println(err)
continue
}
fmt.Println(" status: ", resp.Status, " attempt: ", i)
if resp.StatusCode == 200 {
break
}
}
}()
}
wg.Wait()
elapsed := time.Since(start)
fmt.Println("Fetched 30 urls in", elapsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment