Home >Backend Development >Golang >Go language Websocket development: how to handle a large number of concurrent connections

Go language Websocket development: how to handle a large number of concurrent connections

WBOY
WBOYOriginal
2023-12-14 13:47:311276browse

Go language Websocket development: how to handle a large number of concurrent connections

Go language Websocket development: how to handle a large number of concurrent connections

Websocket is a full-duplex communication protocol that establishes a persistent connection between the browser and the server , enabling the server to actively send messages to the client, and the client can also send messages to the server through this connection. Due to its real-time and high efficiency, Websocket has been widely used in real-time communication, instant chat and other scenarios.

However, in actual applications, it is often necessary to handle a large number of concurrent connections. During the development process, we need to consider how to optimize the processing power of the server in order to provide stable and reliable services. The following will introduce how to use Go language to develop WebSocket programs, and combine it with specific code examples to demonstrate how to handle a large number of concurrent connections.

First, we need to use the net/http and github.com/gorilla/websocket packages in the Go language’s standard library to handle Websocket connections. Next, we can create a handler function to handle the connection request and implement the message sending and receiving logic in it.

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/websocket"
)

// 声明一个全局的websocket的upgrader
var upgrader = websocket.Upgrader{}

func main() {
    http.HandleFunc("/ws", handleWS)
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

func handleWS(w http.ResponseWriter, r *http.Request) {
    // 将HTTP连接升级为Websocket连接
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println("Upgrade error: ", err)
        return
    }
    defer conn.Close()

    for {
        // 读取客户端发送的消息
        _, msg, err := conn.ReadMessage()
        if err != nil {
            log.Println("Read error: ", err)
            break
        }

        // 处理收到的消息
        handleMessage(msg)

        // 向客户端发送消息
        err = conn.WriteMessage(websocket.TextMessage, []byte("Server received: "+string(msg)))
        if err != nil {
            log.Println("Write error: ", err)
            break
        }
    }
}

func handleMessage(message []byte) {
    log.Println("Received message: ", string(message))
    // TODO: 处理消息逻辑
}

In the above code, we first create a global upgrader object to upgrade the HTTP connection to a Websocket connection. In the handleWS function, we use the upgrader.Upgrade method to upgrade the HTTP connection to a Websocket connection, and read the message sent by the client through conn.ReadMessage, Then call handleMessage to process the message logic and send the message to the client through conn.WriteMessage.

The above code can handle a Websocket connection. Next we need to consider how to handle a large number of concurrent connections. The Go language provides goroutine and channel to implement concurrent communication. We can create a goroutine in the handleWS function to handle each connect. In this way, each connection can run in an independent goroutine without affecting each other.

func main() {
    http.HandleFunc("/ws", handleWS)
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

func handleWS(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println("Upgrade error: ", err)
        return
    }
    defer conn.Close()

    go func() {
        for {
            _, msg, err := conn.ReadMessage()
            if err != nil {
                log.Println("Read error: ", err)
                break
            }

            handleMessage(msg)

            err = conn.WriteMessage(websocket.TextMessage, []byte("Server received: "+string(msg)))
            if err != nil {
                log.Println("Write error: ", err)
                break
            }
        }
    }()
}

Through the modification of the above code, we use go func() to create an anonymous function as goroutine, in which the message reading and sending of each connection are processed logic. In this way, each connection can be run in an independent goroutine to achieve the effect of concurrent processing.

In addition to concurrently processing connections, we can also use the Go language's channel to limit the number of concurrent connections. We can create a channel with a buffer and pass it to the corresponding goroutine when a new connection is accepted in the main thread. When the number of connections reaches a certain threshold, the new connection will be blocked. When a connection is closed, we can remove it from the channel so that new connections can be accepted.

func main() {
    http.HandleFunc("/ws", handleWS)
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

func handleWS(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println("Upgrade error: ", err)
        return
    }
    defer conn.Close()

    // 将连接传递给一个channel处理
    connections <- conn

    go func(conn *websocket.Conn) {
        for {
            _, msg, err := conn.ReadMessage()
            if err != nil {
                log.Println("Read error: ", err)
                break
            }

            handleMessage(msg)

            err = conn.WriteMessage(websocket.TextMessage, []byte("Server received: "+string(msg)))
            if err != nil {
                log.Println("Write error: ", err)
                break
            }
        }
    }(conn)
}

var (
    maxConnections = 100
    connections    = make(chan *websocket.Conn, maxConnections)
)

func main() {
    http.HandleFunc("/ws", handleWS)
    go handleConnections()
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

func handleConnections() {
    for conn := range connections {
        // 当连接数达到maxConnections时,新连接将会被阻塞
        log.Println("New connection accepted!")
        go handleWS(conn)
    }
}

In the above code, we first create a connections channel with a buffer and set its size to maxConnections. In the handleWS function, we pass the connection to the connections channel, and then create a goroutine to handle the message sending and receiving logic of the connection. In the handleConnections function, we use for conn := range connections to receive new connections and create the corresponding goroutine to process them.

Through the above optimization, we can efficiently handle a large number of Websocket connections in the Go language. When the number of connections is too large, we can use goroutine and channel to distribute the connection processing tasks to multiple goroutine for processing to improve the performance of the server. Concurrent processing capabilities.

To summarize, we can use the standard library and third-party packages in the Go language to handle Websocket connections, and achieve efficient concurrent processing by using goroutine and channel , to meet the needs of handling a large number of concurrent connections. Through reasonable code design and appropriate optimization strategies, we are able to provide stable and reliable support for Websocket services.

(Note: The above code is only an example, and further optimization and improvement may be needed in specific application scenarios)

The above is the detailed content of Go language Websocket development: how to handle a large number of concurrent connections. 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