MCPcopy Index your code
hub / github.com/panjf2000/gnet

github.com/panjf2000/gnet @v2.10.0 sqlite

repository ↗ · DeepWiki ↗ · release v2.10.0 ↗
881 symbols 3,252 edges 102 files 375 documented · 43%
README

gnet

panjf2000%2Fgnet | Trendshift

English | 中文

🎉🎉🎉 Feel free to join the channels about gnet on the Discord Server.

📖 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 in 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 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 functionality (via a concise set of APIs) required by a network application and it doesn't plan on becoming 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.

🚀 Features

🦖 Milestones

  • [x] High-performance event-driven looping based on a networking model of multiple threads/goroutines
  • [x] Built-in goroutine pool powered by the library ants
  • [x] Lock-free during the entire runtime
  • [x] Concise and easy-to-use APIs
  • [x] Efficient, reusable, and elastic memory buffer: (Elastic-)Ring-Buffer, Linked-List-Buffer and Elastic-Mixed-Buffer
  • [x] Multiple protocols/IPC mechanisms: TCP, UDP, and Unix Domain Socket
  • [x] Multiple load-balancing algorithms: Round-Robin, Source-Addr-Hash, and Least-Connections
  • [x] Flexible ticker event
  • [x] gnet client
  • [x] Running on Linux, macOS, Windows, and *BSD: Darwin/DragonFlyBSD/FreeBSD/NetBSD/OpenBSD
  • [x] Edge-triggered I/O support
  • [x] Multiple network addresses binding
  • [x] Support registering new connections to event-loops

🕊 Roadmap

  • [ ] TLS support
  • [ ] io_uring support
  • [ ] KCP support

Windows version of gnet should only be used in development for developing and testing, it shouldn't be used in production.

🎬 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 corporations/organizations use gnet as the underlying network service in production.

If you're also using gnet in production, please help us enrich this list by opening a pull request.

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

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

On MacOS (kqueue)

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

Combat with Rust

⚠️ License

The source code of gnet should be distributed under the Apache-2.0 license.

👏 Contributors

Please read the Contributing Guidelines before opening a PR and thank you to all the developers who already made contributions to gnet!

⚓ Relevant Articles

☕️ Buy me a coffee

Please be sure to leave your name, GitHub account, or other social media accounts when you donate by the following means so that I can add it to the list of donors as a token of my appreciation.

By me coffee Patreon OpenCollective

🔑 JetBrains OS licenses

gnet has been being developed with GoLand IDE under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.

JetBrains logo.

🔋 Sponsorship

DigitalOcean Referral Badge

Extension points exported contracts — how you extend this code

EventHandler (Interface)
EventHandler represents the engine events' callbacks for the Run call. Each event has an Action return value that is use [4 …
gnet.go
AsyncTaskQueue (Interface)
AsyncTaskQueue is a queue storing asynchronous tasks. [1 implementers]
pkg/queue/queue.go
Option (FuncType)
Option is a function that will set up option.
options.go
Logger (Interface)
Logger is used for logging formatted messages.
pkg/logging/logger.go
PollEventHandler (FuncType)
PollEventHandler is the callback for I/O events notified by the poller.
pkg/netpoll/defs_poller.go
Reader (Interface)
* type asyncCmdType uint8 const ( asyncCmdClose = iota + 1 asyncCmdWake asyncCmdWrite asyncCmdWritev ) type asyncC [1 …
gnet.go
Func (FuncType)
Func is the callback function executed by poller.
pkg/queue/queue.go
Writer (Interface)
Writer is an interface that consists of a number of methods for writing that Conn must implement. [1 implementers]
gnet.go

Core symbols most depended-on inside this repo

Run
called by 347
gnet.go
Close
called by 74
gnet.go
Buffered
called by 64
pkg/buffer/ring/ring_buffer.go
IsEmpty
called by 60
pkg/queue/queue.go
Available
called by 47
pkg/buffer/ring/ring_buffer.go
Write
called by 46
connection_unix.go
Run
called by 43
gnet.go
Write
called by 43
pkg/buffer/ring/ring_buffer.go

Shape

Method 499
Function 260
Struct 100
Interface 10
TypeAlias 7
FuncType 5

Languages

Go100%

Modules by API surface

gnet_test.go142 symbols
gnet.go75 symbols
connection_unix.go48 symbols
connection_windows.go47 symbols
client_test.go39 symbols
options.go27 symbols
pkg/logging/logger.go23 symbols
eventloop_unix.go23 symbols
pkg/buffer/linkedlist/linked_list_buffer.go22 symbols
pkg/buffer/ring/ring_buffer.go21 symbols
pkg/buffer/elastic/elastic_ring_buffer.go21 symbols
os_unix_test.go19 symbols

Dependencies from manifests, versioned

github.com/pmezard/go-difflibv1.0.0 · 1×
github.com/valyala/bytebufferpoolv1.0.0 · 1×
go.uber.org/multierrv1.11.0 · 1×
go.uber.org/zapv1.28.0 · 1×
golang.org/x/syncv0.11.0 · 1×
golang.org/x/sysv0.30.0 · 1×
gopkg.in/natefinch/lumberjack.v2v2.2.1 · 1×
gopkg.in/yaml.v3v3.0.1 · 1×

For agents

$ claude mcp add gnet \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact