Skip to content

Instantly share code, notes, and snippets.

@pcasaretto
Created September 6, 2015 22:07
Show Gist options
  • Save pcasaretto/a05f5d690c68a5529372 to your computer and use it in GitHub Desktop.
Save pcasaretto/a05f5d690c68a5529372 to your computer and use it in GitHub Desktop.
package ratelimited
import (
"fmt"
"net/http"
"sync/atomic"
)
type RoundTripper struct {
http.RoundTripper
requests int64
}
func NewRoundTripper() *RoundTripper {
return &RoundTripper{RoundTripper: http.DefaultTransport}
}
func (c *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt64(&c.requests, 1)
fmt.Println(c.requests)
fmt.Println(req.URL)
return c.RoundTripper.RoundTrip(req)
}
package ratelimited_test
import (
"fmt"
"net/http"
"runtime"
"sync"
"testing"
"github.com/pcasaretto/ratelimited"
)
func TestSomething(t *testing.T) {
client := &http.Client{}
client.Transport = ratelimited.NewRoundTripper()
runtime.GOMAXPROCS(runtime.NumCPU())
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
i := i
wg.Add(1)
go func() {
client.Get(fmt.Sprintf("http://google.com/%d", i))
wg.Done()
}()
wg.Wait()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment