-
-
Save polds/36ffcd95760db6fbd8ce 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 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