Skip to content

Instantly share code, notes, and snippets.

@polds
Forked from ernesto-jimenez/test.go
Last active August 29, 2015 14:14
Show Gist options
  • Save polds/36ffcd95760db6fbd8ce to your computer and use it in GitHub Desktop.
Save polds/36ffcd95760db6fbd8ce to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"sync"
)
type LimitedRoundTripper struct {
inflight chan interface{}
rt http.RoundTripper
}
func NewLimitedRoundTripper(n int, rt http.RoundTripper) *LimitedRoundTripper {
return &LimitedRoundTripper{
inflight: make(chan interface{}, n),
rt: rt,
}
}
func (l *LimitedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
defer func() {
<-l.inflight
}()
fmt.Println("waiting")
l.inflight <- nil
fmt.Println("requesting")
r, err := l.rt.RoundTrip(req)
fmt.Println("done")
return r, err
}
func main() {
c := http.Client{Transport: NewLimitedRoundTripper(3, http.DefaultTransport)}
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r, _ := c.Get("http://localistico.com")
r.Body.Close()
}()
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment