首頁  >  文章  >  後端開發  >  為什麼 net/http 不考慮超過 30 秒的超時持續時間?

為什麼 net/http 不考慮超過 30 秒的超時持續時間?

WBOY
WBOY轉載
2024-02-11 17:30:08774瀏覽

为什么 net/http 不考虑超过 30 秒的超时持续时间?

php小編香蕉在探討網路請求中的超時持續時間時發現,為什麼net/http在設計上沒有考慮超過30秒的超時時間限制?超時時間是指在發送請求後,如果在指定時間內沒有收到回應,就會認為請求失敗。在網路請求中,逾時時間的設定是非常重要的,過短可能導致請求失敗,過長則會浪費資源。透過分析,主要原因是在設計時考慮了效能和資源的平衡,以及網路環境的不確定性。接下來,我們將詳細解答這個問題。

問題內容

使用 golang 1.20.1。

在golang的net/http和context套件中,我無法設定超過三十秒的超時。設定較短的超時效果很好,例如

程式碼:

log.infof("elasticsearch url is %v", elasticsearchurl)
    client := &http.client{timeout: time.duration(time.second * 60)}
    req, err := http.newrequest("get", listbackupsurl, nil)
    if err != nil {
        internalerror(w, fmt.sprintf("error creating request: %v", err))
        return
    }
    req.setbasicauth(username, password)
    resp, err := client.do(req)
    if err != nil {
        // handle error
        internalerror(w, fmt.sprintf("error accessing elasticsearch: %v", err))
        return
    }

紀錄:

i0503 23:01:55.973821       1 somecode.go:85] url is http://elasticsearch.example.ingest:9200
e0503 23:02:25.976345       1 caller_handler.go:63] 500 internal server error: error accessing elasticsearch: get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": dial tcp 1.2.3.4:9200: i/o timeout

超時時間是三十秒,而不是六十秒。

如果我使用 http.newrequestwithcontext(...) 並且使用設定相同逾時的上下文,我會得到相同的行為:

程式碼:

log.infof("elasticsearch url is %v", elasticsearchurl)
    ctx, cancel := context.withtimeout(context.background(), time.duration(time.second * 60)) 
    defer cancel()
    req, err := http.newrequestwithcontext(ctx, "get", listbackupsurl, nil)
    if err != nil {
        internalerror(w, fmt.sprintf("error creating request: %v", err))
        return
    }
    req.setbasicauth(username, password)
    resp, err := client.do(req)
    if err != nil {
        // handle error
        internalerror(w, fmt.sprintf("error accessing elasticsearch: %v", err))
        return
    }

紀錄:

i0503 23:31:10.941169       1 somecode.go:85] elasticsearch url is http://elasticsearch.example.ingest:9200
e0503 23:31:40.941642       1 caller_handler.go:63] 500 internal server error: error accessing elasticsearch: get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": dial tcp 1.2.3.4:9200: i/o timeout

但是,如果我在任一方法中將超時更改為三秒 (time.duration(time.second * 3)),它將按預期工作:

I0503 23:44:17.622121       1 somecode.go:85] Elasticsearch URL is http://elasticsearch.example.ingest:9200
E0503 23:44:20.624795       1 caller_handler.go:63] 500 Internal Server Error: error accessing elasticsearch: Get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

解決方法

我會隔離問題以消除可能性,從而幫助縮小導致問題發生的原因。如果可能,請使用一小段程式碼,這樣您就可以只處理您想要的內容。

要測試您的基礎設施,httpstat 將協助您模擬遠端逾時。範例:

func main() {

    client := &http.Client{Timeout: time.Duration(time.Second * 200)}
    req, err := http.NewRequest("GET", "https://www.php.cn/link/1a59ef90d1ea801448e1567d0896a99f/504?sleep=120000", nil)
    if err != nil {
        log.Fatal(err)
        return
    }

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
        return
    }

    fmt.Println(resp)
}

如果您此時收到 dial tcp ip:port: i/o timeout timeout,那麼您需要檢查您的作業系統和防火牆。無論您在 go 中設定什麼逾時,都應該覆蓋作業系統預設值,如果您以這種方式超時,則可能是防火牆(本地或遠端)導致了這種情況。

或者,如果您能夠從外部連接,則 es 可能會超時,儘管根據文檔,您應該預期會直接來自 es 的資訊錯誤。您可以直接在 url 中設定 es 的逾時 並對其進行測試。

希望這有幫助。

以上是為什麼 net/http 不考慮超過 30 秒的超時持續時間?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除