panjf2000 / gnet
- воскресенье, 22 сентября 2019 г. в 00:22:00
Go
⚡️ A high-performance, lightweight, nonblocking and event-loop networking library written in pure Go. 🔥
gnet is an Event-Loop networking framework that is fast and small. It makes direct epoll and kqueue syscalls rather than using the standard Go net package, and works in a similar manner as libuv and libevent.
The goal of this project is to create a server framework for Go that performs on par with Redis and Haproxy for packet handling.
gnet sells itself as a high-performance, lightweight, nonblocking network library written in pure Go which works on transport layer with TCP/UDP/Unix-Socket protocols, so it allows developers to implement their own protocols of application layer upon gnet for building diversified network applications, for instance, you get a HTTP Server or Web Framework if you implement HTTP protocol upon gnet while you have a Redis Server done with the implementation of Redis protocol upon gnet and so on.
gnet derives from project evio while having higher performance.
gnet redesigns and implements a new built-in multiple-threads/goroutines model: 『Multiple Reactors』 which is also the default multiple-threads model of netty, Here's the schematic diagram:
and it works as the following sequence diagram:
You may ask me a question: what if my business logic in Event.React() contains some blocking code which leads to a blocking in event-loop of gnet, what is the solution for this kind of situation?
And the solution for that would be found in the subsequent multiple-threads/goroutines model of gnet: 『Multiple Reactors with thread/goroutine pool』which pulls you out from the blocking mire, it will construct a worker-pool with fixed capacity and put those blocking jobs in Event.React() into the worker-pool to unblock the event-loop goroutines.
This new networking model is under development and about to be delivered soon and its architecture diagram of new model is in here:
and it works as the following sequence diagram:
Before you can benefit from this new networking model in handling blocking business logic, there is still a way for you to handle your business logic in networking: you can leverage the open-source goroutine-pool to unblock your blocking code, and I now present you ants: a high-performance goroutine pool in Go that allows you to manage and recycle a massive number of goroutines in your concurrency programs.
You can import ants to your gnet server and put your blocking code to the ants pool in Event.React(), which makes your business code nonblocking.
gnet builds its 『Multiple Reactors』Model under Goroutines in Golang, one Reactor per Goroutine, so there is a critical requirement handling extremely large amounts of messages between Goroutines in this networking model of gnet, which means gnet needs a efficient communication mechanism between Goroutines. I choose a tricky solution of Disruptor(Ring-Buffer) which provides a higher performance of messages dispatching in networking, instead of the recommended pattern: CSP(Channel) under Golang-Best-Practices.
That is why I finally settle on go-disruptor: the Golang port of the LMAX Disruptor(a high performance inter-thread messaging library).
gnet leverages Ring-Buffer to cache TCP streams and manage memory cache in networking.
$ go get -u github.com/panjf2000/gnetIt is easy to create a network server with gnet. All you have to do is just register your events to gnet.Events and pass it to the gnet.Serve function along with the binding address(es). Each connections is represented as an gnet.Conn object that is passed to various events to differentiate the clients. At any point you can close a client or shutdown the server by return a Close or Shutdown action from an event.
The simplest example to get you started playing with gnet would be the echo server. So here you are, a simplest echo server upon gnet that is litsening on port 9000:
package main
import (
"log"
"github.com/panjf2000/gnet"
)
func main() {
var events gnet.Events
events.Multicore = true
events.React = func(c gnet.Conn) (action gnet.Action) {
top, tail := c.Read()
c.Write(append(top, tail...))
c.ResetBuffer()
if trace {
log.Printf("%s", strings.TrimSpace(string(top)+string(tail)))
}
return
}
log.Fatal(gnet.Serve(events, "tcp://:9000"))
}As you can see, this example of echo server only sets up the React function where you commonly write your main business code and it will be invoked once the server receives input data from a client. The output data will be then sent back to that client by assigning the out variable and return it after your business code finish processing data(in this case, it just echo the data back).
Current supported I/O events in gnet:
OnInitComplete is activated when the server is ready to accept new connections.OnOpened is activated when a connection has opened.OnClosed is activated when a connection has closed.React is activated when the server receives new data from a connection, you usually put the business code in this function.Tick is activated immediately after the server starts and will fire again after a specified interval.PreWrite is activated just before any data is written to any client socket.// Binding both TCP and Unix-Socket to one gnet server.
gnet.Serve(events, "tcp://:9000", "unix://socket")The Tick event fires ticks at a specified interval.
The first tick fires immediately after the Serving events.
events.Tick = func() (delay time.Duration, action Action){
log.Printf("tick")
delay = time.Second
return
}The Serve function can bind to UDP addresses.
OnOpened and OnClosed events are not available for UDP sockets, only the React event.The Events.Multicore indicates whether the server will be effectively created with multi-cores, if so, then you must take care with synchronizing memory between all event callbacks, otherwise, it will run the server with single thread. The number of threads in the server will be automatically assigned to the value of runtime.NumCPU().
The current built-in load balancing algorithm in gnet is Round-Robin.
Servers can utilize the SO_REUSEPORT option which allows multiple sockets on the same host to bind to the same port and the OS kernel takes care of the load balancing for you, it wakes one socket per accpet event coming to resolved the thundering herd.
Just provide reuseport=true to an address and you can enjoy this feature:
gnet.Serve(events, "tcp://:9000?reuseport=true"))Go Version : go1.12.9 linux/amd64
OS : Ubuntu 18.04/x86_64
CPU : 8 Virtual CPUs
Memory : 16.0 GiBGo Version : go version go1.12.9 darwin/amd64
OS : macOS Mojave 10.14.6/x86_64
CPU : 4 CPUs
Memory : 8.0 GiBSource code in gnet is available under the MIT License.
gnet is still under active development so the code and documentation will continue to be updated, if you are interested in gnet, please feel free to make your code contributions to it, also if you like gnet, give it a star ~~