search
HomeBackend DevelopmentGolanggolang http close connection

When using Golang for HTTP programming, we need to often consider how to close the connection. Closing connections can effectively avoid resource waste, improve performance, and reduce unnecessary trouble caused by network problems. This article will introduce in detail how to close HTTP connections in Golang and analyze some of the details.

1. How to close HTTP connections

The HTTP client and server in the Go language implement a series of underlying processes to manage HTTP connections. These low-level processing are usually not exposed to the user, but are hidden in the internal implementation by HTTP clients and servers. So how to close the connection in HTTP client and server?

  1. HTTP client

In the HTTP client, we have several ways to close the connection:

  • Use the defer keyword to Manually close the connection: After the client calls the interface, the connection will generally be closed automatically. However, if the current client needs to request the interface multiple times, you can consider manually controlling the connection closing and using defer to delay closing the connection. The code is as follows:
package main
import (
    "net/http"
    "io/ioutil"
    "fmt"
    "log"
)
func main() {
    client := &http.Client{}
    req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
    if err != nil {
        log.Fatal(err)
    }
    defer client.CloseIdleConnections() // 当函数返回时释放连接
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close() // 当函数返回时关闭body
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(body)[:50])
}
  • Use MaxIdleConns or MaxIdleConnsPerHost of the Transport class to control the connection The upper limit of connections in the pool: http.Transport of Go language contains a default connection pool for managing HTTP connections. We can use its two parameters MaxIdleConns and MaxIdleConnsPerHost to specify the maximum number of idle connections and the maximum number of idle host connections. When this idle number is reached, Transport will automatically close excess connections. The code is as follows:
package main
import (
    "net/http"
    "io/ioutil"
    "fmt"
    "log"
)
func main() {
    transport := &http.Transport{
        MaxIdleConns:          10, // 最大空闲连接数
        MaxIdleConnsPerHost:   3,  // 每个域名地址最大空闲连接数
    }
    client := &http.Client{Transport: transport}
    req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
    if err != nil {
        log.Fatal(err)
    }
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(body)[:50])
}
  1. HTTP server

In the HTTP server, connection management is usually automatically managed by the server. We just need to create the server and close it when the connection is no longer needed. Normally, the server automatically closes the connection after processing a request. But there are several special situations that require us to manually control the closing of the connection:

  • Use conn.Close() in an HTTP processor to close the connection: When the processor processes the HTTP request, if If the server needs to close the connection, it can use the conn.Close() method to close the connection. The code is as follows:
package main
import (
    "fmt"
    "net/http"
)
func handler(w http.ResponseWriter, req *http.Request) {
    // 需要关闭连接时,使用conn.Close()
    conn, _, _ := w.(http.Hijacker).Hijack()
    conn.Close()
}
func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
  • Using Keep-Alive: When using Keep-Alive, the life cycle of the connection is managed by the server. The server can define a keep-alive time to control how long the connection should be closed after being idle. This time is usually set in the HTTP header. The code is as follows:
package main
import (
    "log"
    "net/http"
)
func handler(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Connection", "keep-alive") // 定义keep-alive时间
    w.Write([]byte("hello world"))
}
func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

2. HTTP connection recycling mechanism

When the connection is used up, we need to recycle resources for the connection to prevent resource leakage, performance degradation and other problems. The recycling mechanism of HTTP connection resources usually needs to consider the following issues:

  • Maximum lifetime of HTTP long connections: After the connection has been idle for a period of time, the server will immediately recycle the connection. This time can be determined by the server. Just set the connection timeout when creating the server object.
  • Maximum number of HTTP connections: The maximum number of connections allowed in the connection pool within a period of time. When the connection pool exceeds the connection pool size, they will be automatically closed.
  • HTTP connection reuse mechanism: Connection reuse means multiple requests share a connection at the same time. This can reduce unnecessary resource usage during long waiting states. However, it should be noted that if the connection timeout is short, it may cause a request with transient network fluctuations to fail, thus affecting the stability of the system.

3. Management of HTTP connection pool

In large-scale and highly concurrent systems, we need to use connection pools to manage HTTP connections to improve system performance and stability. When calling an HTTP request, the connection pool can provide an idle network connection to avoid the overhead of frequently creating and destroying connections. Connection pools are usually managed using regular queues to ensure that the elements that enter the queue first are used first, that is, a FIFO queue. The following is the code to implement HTTP connection pool using Go language:

package main
import (
    "fmt"
    "log"
    "net/http"
    "sync"
    "time"
)
type MyHttpClient struct {
    client    *http.Client     // http客户端对象
    Transport *http.Transport  // transport对象,用于管理http连接池
}
var once sync.Once
var myClient *MyHttpClient
func GetMyHttpClient() *MyHttpClient {
    once.Do(func() {
        myClient = &MyHttpClient{
            Transport: &http.Transport{
                Dial: func(network, addr string) (net.Conn, error) {
                    conn, err := net.DialTimeout(network, addr, 10*time.Second)
                    if err != nil {
                        return nil, err
                    }
                    return conn, nil
                },
                MaxIdleConns:          100,               // 连接池中最多拥有的连接数
                MaxIdleConnsPerHost:   2,                 // 每个域名最多拥有的连接数
                IdleConnTimeout:       60 * time.Second,  // 连接在闲置多久后被关闭
                TLSHandshakeTimeout:   10 * time.Second,  // TLS握手时限
                ExpectContinueTimeout: 1 * time.Second,   // 100-continue超时时限
            },
            client: &http.Client{},
        }
    })
    return myClient
}
func main() {
    client := GetMyHttpClient().client
    req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
    if err != nil {
        log.Fatal(err)
    }
    defer client.CloseIdleConnections()
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(body)[:50])
}

4. Summary

The control and recycling of HTTP connections is a key performance optimization point. In Golang, we can manage HTTP connections through manual control, parameter settings using the Transport class, and connection pooling to improve system performance and stability. In addition, we also need to consider issues such as the connection recycling mechanism and HTTP connection pool management, and maintain and optimize the system.

The above is the detailed content of golang http close connection. 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
Golang vs. Python: The Pros and ConsGolang vs. Python: The Pros and ConsApr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang and C  : Concurrency vs. Raw SpeedGolang and C : Concurrency vs. Raw SpeedApr 21, 2025 am 12:16 AM

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Why Use Golang? Benefits and Advantages ExplainedWhy Use Golang? Benefits and Advantages ExplainedApr 21, 2025 am 12:15 AM

Reasons for choosing Golang include: 1) high concurrency performance, 2) static type system, 3) garbage collection mechanism, 4) rich standard libraries and ecosystems, which make it an ideal choice for developing efficient and reliable software.

Golang vs. C  : Performance and Speed ComparisonGolang vs. C : Performance and Speed ComparisonApr 21, 2025 am 12:13 AM

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Is Golang Faster Than C  ? Exploring the LimitsIs Golang Faster Than C ? Exploring the LimitsApr 20, 2025 am 12:19 AM

Golang performs better in compilation time and concurrent processing, while C has more advantages in running speed and memory management. 1.Golang has fast compilation speed and is suitable for rapid development. 2.C runs fast and is suitable for performance-critical applications. 3. Golang is simple and efficient in concurrent processing, suitable for concurrent programming. 4.C Manual memory management provides higher performance, but increases development complexity.

Golang: From Web Services to System ProgrammingGolang: From Web Services to System ProgrammingApr 20, 2025 am 12:18 AM

Golang's application in web services and system programming is mainly reflected in its simplicity, efficiency and concurrency. 1) In web services, Golang supports the creation of high-performance web applications and APIs through powerful HTTP libraries and concurrent processing capabilities. 2) In system programming, Golang uses features close to hardware and compatibility with C language to be suitable for operating system development and embedded systems.

Golang vs. C  : Benchmarks and Real-World PerformanceGolang vs. C : Benchmarks and Real-World PerformanceApr 20, 2025 am 12:18 AM

Golang and C have their own advantages and disadvantages in performance comparison: 1. Golang is suitable for high concurrency and rapid development, but garbage collection may affect performance; 2.C provides higher performance and hardware control, but has high development complexity. When making a choice, you need to consider project requirements and team skills in a comprehensive way.

Golang vs. Python: A Comparative AnalysisGolang vs. Python: A Comparative AnalysisApr 20, 2025 am 12:17 AM

Golang is suitable for high-performance and concurrent programming scenarios, while Python is suitable for rapid development and data processing. 1.Golang emphasizes simplicity and efficiency, and is suitable for back-end services and microservices. 2. Python is known for its concise syntax and rich libraries, suitable for data science and machine learning.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor