首次HTTP请求始终较慢。

huangapple go评论112阅读模式
英文:

First HTTP request always slow

问题

我有以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. func Get(url string, client *http.Client, header []string) (*http.Response, error) {
  8. start := time.Now()
  9. req, err := http.NewRequest("GET", url, nil)
  10. if err != nil {
  11. return nil, err
  12. }
  13. for i := 0; i < len(header)/2; i++ {
  14. req.Header.Add(header[2*i], header[2*i+1])
  15. }
  16. resp, err := client.Do(req)
  17. fmt.Println(time.Until(start))
  18. return resp, nil
  19. }
  20. func main() {
  21. header := []string{"accept-encoding", "gzip, deflate, br"}
  22. cl := &http.Client{}
  23. url1 := "https://example1.com"
  24. url2 := "https://example2.com"
  25. url3 := "https://www.google.com/"
  26. resp, _ := Get(url1, cl, header)
  27. resp, _ = Get(url2, cl, header)
  28. resp, _ = Get(url3, cl, header)
  29. fmt.Println(resp)
  30. }

resp, _ := Get(url1, cl, header) 的时间总是很慢,无论将 url1 更改为 https://example2.com 还是 https://www.google.com/

像这样:

  1. 1 254.156959ms
  2. 2 34.183151ms
  3. 2 87.880252ms
英文:

I have following code

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. &quot;time&quot;
  6. )
  7. func Get(url string, client *http.Client, header []string) (*http.Response, error) {
  8. start := time.Now()
  9. req, err := http.NewRequest(&quot;GET&quot;, url, nil)
  10. if err != nil {
  11. return nil, err
  12. }
  13. for i := 0; i &lt; len(header)/2; i++ {
  14. req.Header.Add(header[2*i], header[2*i+1])
  15. }
  16. resp, err := client.Do(req)
  17. fmt.Println(time.Until(start))
  18. return resp, nil
  19. }
  20. func main() {
  21. header := []string{&quot;accept-encoding&quot;, &quot;gzip, deflate, br&quot;}
  22. cl := &amp;http.Client{}
  23. url1 := &quot;https://example1.com&quot;
  24. url2 := &quot;https://example2.com&quot;
  25. url3 := &quot;https://www.google.com/&quot;
  26. resp, _ := Get(url1, cl, header)
  27. resp, _ = Get(url2, cl, header)
  28. resp, _ = Get(url3, cl, header)
  29. fmt.Println(resp)
  30. }

The time of resp, _ := Get(url1, cl, header) is always slow
Regardless of url1 change to https://example2.com or https://www.google.com/

like this

  1. 1 254.156959ms
  2. 2 34.183151ms
  3. 2 87.880252ms

答案1

得分: 2

这是一篇关于解决Go的DNS问题的精彩文章,你可以在这里找到它。文章简要提到了net包文档中的Name_Resolution部分。

如果你喜欢阅读代码(和注释),你可以在src/net/lookup.go中找到更多信息。

简而言之,Go可以使用不同的DNS解析器,你可以通过以下方式启用或测试:

  1. GODEBUG=netdns=1 go run main.go
  2. > go package net: hostLookupOrder(redis) = cgo
  3. // osx/macosx case
  4. GODEBUG=netdns=go+2 go run main.go
  5. > go package net: GODEBUG setting forcing use of Gos resolver go
  6. > package net: hostLookupOrder(redis) = files,dns
英文:

There is a beautiful article on medium on resolving go's DNS issues. Shortly it refers to the Name_Resolution of the net package documentation.

More info can be found in src/net/lookup.go, if you like to read code (and comments).

TL/DR: go can use different dns resolvers, wich you can enable/or test with

  1. GODEBUG=netdns=1 go run main.go
  2. &gt; go package net: hostLookupOrder(redis) = cgo
  3. // osx/macosx case
  4. GODEBUG=netdns=go+2 go run main.go
  5. &gt; go package net: GODEBUG setting forcing use of Gos resolver go
  6. &gt; package net: hostLookupOrder(redis) = files,dns

huangapple
  • 本文由 发表于 2021年8月4日 16:08:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68647626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定