Network layer implementation of Golang server

Source: Internet
Author: User
Tags epoll

With the recent exposure to some long-connected server implementations, the network model has been learned. Comparing the implementation of the network model and based on the Golang, it is found that the Golang of the network model is greatly reduced, which is due to the Golang goroutine , which can create the concurrent "thread" when programming, when the server can open a few for each client. "Threading", programming becomes much simpler.

Network layer processing in traditional language

The service needs to serve n clients at the same time, so the traditional way of programming is to use IO multiplexing, so that the event captures n sockets in one thread, and when the read-write event is generated, it is true read() or write() , in order to improve throughput:

Img

In

    • The green thread is the thread that accepts the client TCP link, uses the blocking call socket.accept() , and adds the object to the socket conn io multiplexing queue when a new connection arrives.
    • The purple thread is a blocking call for IO multiplexing, which is usually epoll implemented using such system calls for IO multiplexing. When any data arrives in the IO multiplexing queue socket , or the write buffer is idle, the epoll return of the call can be triggered, otherwise the call is blocked epoll . The actual sending and receiving of the data is done in the purple thread. Therefore, in order to improve the throughput, the and all of a socket read write should use a non-blocking mode, so as to maximize the system throughput. For example, if you are blocking a socket call write , you will not be able to return until the data is completely sent, write preventing the entire epoll entry into the next loop, and if the other is socket read ready, it will not be able to respond the first time. Therefore, non-blocking reads and writes will be returned at a slower time when the FD reads and writes, rather than waiting until the end of the read and write. This will improve throughput. However, the use of non-resistive read and write will greatly improve the programming difficulty.
    • The purple thread is responsible for decoding the data and putting it into the queue, waiting for the worker thread to process it, and when the worker thread has data to send, it also puts the data into the send queue, and through some mechanism notifies the purple thread that the corresponding socket has data to write, so that the data is written to the socket in the purple thread.

The programming difficulty of this model is mainly reflected in:

    1. There are fewer threads (and not too many), causing a thread to handle multiple descriptors, which can cause maintenance problems with descriptor state. Even business-level conversations need to be carefully maintained
    2. Non-blocking IO calls to make descriptor states more complex
    3. Synchronous processing of queues

Have to say, can use C or C + + to write server is really big God!

Golang's Goroutine

Golangis a relatively new language, is rapidly developing. Golanga 协程 lightweight threading model called is supported from the language level goroutine . When we create a co-process, the operating system's thread is not actually created, and Golang uses the existing thread to schedule the process. That is, from the programmer's point of view, the process is executed concurrently, as if it were a thread, and from the operating system point of view, the program may only have a few threads running. In the same application, the co-process can have thousands of! So there are thousands of concurrent tasks, and these tasks are very lightweight and much lighter than thread scheduling. So from the programmer's point of view, using Golang can open thousands of concurrent tasks in one application at the same time. It's a reverse sky!

Use the keyword in Golang go to open a goroutine :

func main() {    log.Println("Hello, world")    netListen, err := net.Listen("tcp", "localhost:4000")    if err != nil {        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())        os.Exit(1)    }    defer netListen.Close()    log.Println("Waiting for clients")    for {        conn, err := netListen.Accept()        if err != nil {            continue        }        log.Println(conn.RemoteAddr().String(), " tcp connect success")        go handleConnection(conn)    }}func handleConnection(conn net.Conn) {    ...}

Golang's Channel

In addition to concurrency support, there is a channel concurrency synchronization mechanism called in Golang. channelsimilar to the queue, is goroutine secure. So goroutine the combination and channel can easily implement concurrent programming.

Golang How to implement the network layer

By referring to multiple Golang open source programs, the author concludes: Unscrupulous use of goroutine it. So a golang version of the network model is roughly the case:

Img

is the server module structure of a single client connection, and the same color represents a co-process:

    • Green goroutine is still accepting TCP links
    • When the handshake is finished accept returning the conn object, use a goroutine separate 阻塞读 (purple) to use a separate goroutine 阻塞写 (red)
    • The read data is decoded and then put in 读channel , and processed by the blue. goroutine
    • When writing data is required, blue goroutine writes the data 写channel , triggering the red goroutine encoding and writing Conn

As you can see, for a client, there are at least 3 service-side goroutine services for this client alone. It's a waste of time from a threading point of view, but that's the benefit of the process. This model is easy to understand because it is consistent with people's normal way of thinking. and are blocked calls, so there is no need to maintain state.

Take a look at multiple client scenarios:

Img

Among the multiple clients, although the same color goroutine is represented, but in fact they are all independent goroutine , the conceivable goroutine number will be amazing. However, there is no need to worry! Such an application might actually have only a few threads.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.