Home  >  Article  >  Backend Development  >  How to use http.Transport to manage and control HTTP long connections in Go?

How to use http.Transport to manage and control HTTP long connections in Go?

王林
王林Original
2023-07-23 18:12:231537browse

How to use http.Transport to manage and control HTTP long connections in Go?

When making HTTP requests, establishing a long connection can significantly improve the performance and efficiency of the request. Go language provides http.Transport to manage and control HTTP long connections.

http.Transport is the core module for HTTP clients in the Go language. It can perform operations such as connection reuse, connection idle time control, and connection closing. The following will introduce how to use http.Transport to manage and control HTTP long connections.

First, we need to create an http.Transport object and set some parameters to control connection management. For example, we can set the maximum number of idle connections, the maximum idle time, the maximum number of concurrent requests, etc. The following is a sample code:

package main

import (
    "log"
    "net/http"
    "time"
)

func main() {
    transport := &http.Transport{
        MaxIdleConns:          10,               // 最大空闲连接数
        IdleConnTimeout:       30 * time.Second, // 最大空闲时间
        MaxIdleConnsPerHost:   2,                // 每个host的最大空闲连接数
        MaxConnsPerHost:       0,                // 每个host的最大连接数,0表示不限制
        MaxResponseHeaderBytes: 1024 * 1024,      // 响应头的最大长度
        TLSHandshakeTimeout:   10 * time.Second, // TLS握手的超时时间
    }
    client := &http.Client{
        Transport: transport,
    }

    resp, err := client.Get("https://www.example.com")
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    // 处理响应
}

In the above code, we create an http.Transport object and set some parameters. Among them, MaxIdleConns represents the maximum number of idle connections, IdleConnTimeout represents the maximum idle time, MaxIdleConnsPerHost represents the maximum number of idle connections per host, MaxConnsPerHost represents the maximum number of connections per host, MaxResponseHeaderBytes represents the maximum length of the response header, and TLSHandshakeTimeout represents the timeout of the TLS handshake. time.

Then, we use the created http.Transport object to create an http.Client object as our client for HTTP requests. In this http.Client, we set the http.Transport object we just created to its Transport property.

Finally, we can use the created http.Client object to send HTTP requests. When sending an HTTP request, the underlying http.Transport will handle the management and control of the connection according to the parameters we set. There is no need to manually manage the establishment and closing of connections, http.Transport will automatically reuse connections and close idle connections.

In addition to the parameters mentioned above, http.Transport also provides some other methods and properties for more fine-grained control of connection management, such as CancelRequest, CloseIdleConnections and ForceAttemptHTTP2.

Using http.Transport to manage and control HTTP long connections can help us improve the performance and efficiency of HTTP requests. However, parameters need to be set appropriately according to the actual situation, and tested and optimized to achieve the best results.

The above is the detailed content of How to use http.Transport to manage and control HTTP long connections in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn