resty
resty copied to clipboard
Limit the maximum number of requests
For example, how to limit the maximum number of requests during concurrent requests?
@leewithyuan I'm sorry, I didn't get it. Do you mean throttling?
request pool?
Is this a duplicate of https://github.com/go-resty/resty/issues/256?
@leewithyuan I'm sorry, I didn't get it. Do you mean throttling?
Sorry, I have solved it, the maximum number of requests is limited to 100; But it would be better if there is a more convenient api;
var reqChan = make(chan time.Time, 100)
cli := resty.New()
cli.OnBeforeRequest(func(client *resty.Client, request *resty.Request) error {
reqChan <- request.Time
return nil
})
cli.OnAfterResponse(func(client *resty.Client, response *resty.Response) error {
<-reqChan
return nil
})
for i := 0; i <= 1000; i++ {
go cli.R().Get("http://xxxxxxx")
}
I believe the correct way to do this is construct a new http.Transport
and give it a MaxConnsPerHost
.
Something like this:
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
restyClient.SetTransport(&http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
MaxConnsPerHost: 100,
})