Skip to content

Latest commit

 

History

History
202 lines (145 loc) · 7.76 KB

README.md

File metadata and controls

202 lines (145 loc) · 7.76 KB

gnet

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, it 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.

gent is derived from project evio while having higher performance.

Features

  • High-performance Event-Loop under multi-threads model
  • Built-in load balancing algorithm: Round-Robin
  • Concise APIs
  • Efficient memory usage: Ring-Buffer
  • Supporting multiple protocols: TCP, UDP, and Unix Sockets
  • Supporting two event-notification mechanisms: epoll in Linux and kqueue in FreeBSD
  • Supporting asynchronous write operation
  • Allowing multiple network binding on the same Event-Loop
  • Flexible ticker event
  • SO_REUSEPORT socket option

Key Designs

Multiple-threads Model

gnet redesigns and implements a new built-in multiple-threads model: 『Multiple Reactors』 which is also the default multiple-threads model of netty, Here's the schematic diagram:

multi_reactor

and it works as the following sequence diagram:

reactor

The subsequent multiple-threads model of gnet: 『Multiple Reactors with thread/goroutine pool』is under development and about to be delivered soon, the architecture diagram of new model is in here:

multi_reactor_thread_pool

and it works as the following sequence diagram:

multi-reactors

Communication Mechanism

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).

Auto-scaling Ring Buffer

gnet leverages Ring-Buffer to cache TCP streams and manage memory cache in networking.

Getting Started

Installation

$ go get -u github.com/panjf2000/gnet

Example

// ======================== Echo Server implemented with gnet ===========================

package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/panjf2000/gnet"
	"github.com/panjf2000/gnet/ringbuffer"
)

func main() {
	var port int
	var loops int
	var udp bool
	var trace bool
	var reuseport bool

	flag.IntVar(&port, "port", 5000, "server port")
	flag.BoolVar(&udp, "udp", false, "listen on udp")
	flag.BoolVar(&reuseport, "reuseport", false, "reuseport (SO_REUSEPORT)")
	flag.BoolVar(&trace, "trace", false, "print packets to console")
	flag.IntVar(&loops, "loops", 0, "num loops")
	flag.Parse()

	var events gnet.Events
	events.NumLoops = loops
	events.OnInitComplete = func(srv gnet.Server) (action gnet.Action) {
		log.Printf("echo server started on port %d (loops: %d)", port, srv.NumLoops)
		if reuseport {
			log.Printf("reuseport")
		}
		return
	}
	events.React = func(c gnet.Conn, inBuf *ringbuffer.RingBuffer) (out []byte, action gnet.Action) {
		top, tail := inBuf.PreReadAll()
		out = append(top, tail...)
		inBuf.Reset()

		if trace {
			log.Printf("%s", strings.TrimSpace(string(top)+string(tail)))
		}
		return
	}
	scheme := "tcp"
	if udp {
		scheme = "udp"
	}
	log.Fatal(gnet.Serve(events, fmt.Sprintf("%s://:%d", scheme, port)))
}

I/O Events

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.
  • OnDetached is activated when a connection has been detached using the Detach return action.
  • React is activated when the server receives new data from a connection.
  • 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.

Performance

On Linux (epoll)

Test Environment

Go Version: go1.12.9 linux/amd64
OS:         Ubuntu 18.04
CPU:        8 Virtual CPUs
Memory:     16.0 GiB

Echo Server

HTTP Server

On FreeBSD (kqueue)

Test Environment

Go Version: go version go1.12.9 darwin/amd64
OS:         macOS Mojave 10.14.6
CPU:        4 CPUs
Memory:     8.0 GiB

Echo Server

HTTP Server

License

Source code in gnet is available under the MIT License.

TODO

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~~