XUtils

gnet

`gnet` is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.


📖 Introduction

gnet is an event-driven networking framework that is ultra-fast and lightweight. It is built from scratch by exploiting epoll and kqueue and it can achieve much higher performance with lower memory consumption than Go net in many specific scenarios.

gnet and net don’t share the same philosophy about network programming. Thus, building network applications with gnet can be significantly different from building them with net, and the philosophies can’t be reconciled. There are other similar products written in other programming languages in the community, such as libevent, libuv, netty, twisted, tornado, etc. which work in a similar pattern as gnet under the hood.

gnet is not designed to displace the Go net, but to create an alternative in the Go ecosystem for building performance-critical network services. As a result of which, gnet is not as comprehensive as Go net, it provides only the core functionalities (in a concise API set) required by a network application and it doesn’t plan on being a coverall networking framework, as I think Go net has done a good enough job in that area.

gnet sells itself as a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go which works on the transport layer with TCP/UDP protocols and Unix Domain Socket. It enables developers to implement their own protocols(HTTP, RPC, WebSocket, Redis, etc.) of application layer upon gnet for building diversified network services. For instance, you get an HTTP Server 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 the project: evio with much higher performance and more features.

🎬 Getting started

gnet is available as a Go module and we highly recommend that you use gnet via Go Modules, with Go 1.11 Modules enabled (Go 1.11+), you can just simply add import "github.com/panjf2000/gnet/v2" to the codebase and run go mod download/go mod tidy or go [build|run|test] to download the necessary dependencies automatically.

With v2

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

With v1

go get -u github.com/panjf2000/gnet

🎡 Use cases

The following companies/organizations use gnet as the underlying network service in production.

If you have gnet integrated into projects, feel free to open a pull request refreshing this list.

📊 Performance

Benchmarks on TechEmpower

# Hardware Environment
* 28 HT Cores Intel(R) Xeon(R) Gold 5120 CPU @ 3.20GHz
* 32GB RAM
* Dedicated Cisco 10-gigabit Ethernet switch
* Debian 12 "bookworm"
* Go1.19.x linux/amd64

This is a leaderboard of the top 50 out of 486 frameworks that encompass various programming languages worldwide, in which gnet is ranked first.

This is the full framework ranking of Go and gnet tops all the other frameworks, which makes gnet the fastest networking framework in Go.

To see the full ranking list, visit TechEmpower Benchmark Round 22.

Note that the HTTP implementation of gnet on TechEmpower is half-baked and fine-tuned for benchmark purposes only and far from production-ready.

Contrasts to the similar networking libraries

On Linux (epoll)

Test Environment

# Machine information
        OS : Ubuntu 20.04/x86_64
       CPU : 8 CPU cores, AMD EPYC 7K62 48-Core Processor
    Memory : 16.0 GiB

# Go version and settings
Go Version : go1.17.2 linux/amd64
GOMAXPROCS : 8

# Benchmark parameters
TCP connections : 1000/2000/5000/10000
Packet size     : 512/1024/2048/4096/8192/16384/32768/65536 bytes
Test duration   : 15s

Echo benchmark

On MacOS (kqueue)

Test Environment

# Machine information
        OS : MacOS Big Sur/x86_64
       CPU : 6 CPU cores, Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory : 16.0 GiB

# Go version and settings
Go Version : go1.16.5 darwin/amd64
GOMAXPROCS : 12

# Benchmark parameters
TCP connections : 300/400/500/600/700
Packet size     : 512/1024/2048/4096/8192 bytes
Test duration   : 15s

Echo benchmark

⚓ Relevant Articles

💎 Sponsors

Become a bronze sponsor with a monthly donation of $10 and get your logo on our README on GitHub.

💴 Patrons

Patrick Othmer Jimmy ChenZhen Mai Yang 王开帅 Unger Alejandro Weng Wei

Articles

  • coming soon...