Created
September 6, 2015 22:07
-
-
Save pcasaretto/a05f5d690c68a5529372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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