Skip to content

Commit

Permalink
fix Get EOF and Client.Timeout exceeded while awaiting headers
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jun 15, 2021
1 parent 0c2cc1e commit 2390200
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ var client = &http.Client{}
func init() {
client.Timeout = 10 * time.Second
tp := http.DefaultTransport.(*http.Transport).Clone()
tp.MaxIdleConns = 100
tp.MaxIdleConnsPerHost = 100
//TODO
//https://gocn.vip/topics/11970
//DefaultMaxIdleConnsPerHost 设置的太小就会导致一个问题,
//在大量请求的情况下去访问特定的 host 的时候,长连接会退化成短链接.
tp.MaxIdleConns = 0
tp.MaxIdleConnsPerHost = 50
//to avoid "context deadline exceeded (Client.Timeout exceeded while awaiting headers)"
//https://cloud.tencent.com/developer/article/1529840
tp.IdleConnTimeout = 5 * time.Second

client.Transport = tp
}
func NewClient(clientId string, clientSecret string) *Client {
Expand Down Expand Up @@ -73,8 +81,6 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
if err != nil {
return err
}
//prevents the connection from being re-used
////https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
resp, err := client.Do(req)
if err != nil {
return err
Expand Down

0 comments on commit 2390200

Please sign in to comment.