Convert Figma logo to code with AI

panjf2000 logognet

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

9,481
1,024
9,481
31

Top Related Projects

5,880

Fast event-loop networking for Go

21,607

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

2,151

Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.

4,011

A high-performance non-blocking I/O networking framework focusing on RPC scenarios.

handling 1M websockets connections in Go

Quick Overview

gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in Go. It provides a simple and efficient way to build scalable network applications, such as web servers, real-time communication systems, and distributed systems.

Pros

  • High Performance: gnet is designed to be highly efficient, with low overhead and fast response times.
  • Simplicity: The API is straightforward and easy to use, making it accessible for developers of all skill levels.
  • Flexibility: gnet supports a wide range of network protocols and can be easily extended to support new ones.
  • Scalability: The event-driven architecture of gnet allows it to handle a large number of concurrent connections efficiently.

Cons

  • Limited Documentation: The project's documentation could be more comprehensive, which may make it challenging for new users to get started.
  • Lack of Widespread Adoption: While gnet is a well-designed and capable networking framework, it may not have the same level of community support and adoption as some other Go networking libraries.
  • Potential Learning Curve: Developers who are new to event-driven programming or the Go language may need to invest some time to fully understand and utilize the features of gnet.
  • Potential Compatibility Issues: As a relatively new project, gnet may have some compatibility issues with older versions of Go or other dependencies.

Code Examples

Here are a few examples of how to use gnet:

  1. Simple Echo Server:
package main

import (
    "fmt"
    "github.com/panjf2000/gnet"
)

type echoServer struct {
    gnet.EventServer
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    fmt.Printf("Received: %s\n", frame)
    return frame, gnet.None
}

func main() {
    err := gnet.Serve(new(echoServer), "tcp://0.0.0.0:8080", gnet.WithMulticore(true))
    if err != nil {
        panic(err)
    }
}

This code creates a simple echo server that listens on tcp://0.0.0.0:8080 and echoes back any data it receives.

  1. WebSocket Server:
package main

import (
    "fmt"
    "github.com/panjf2000/gnet"
    "github.com/panjf2000/gnet/pool/goroutine"
)

type wsServer struct {
    gnet.EventServer
    pool *goroutine.Pool
}

func (ws *wsServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    fmt.Printf("Received: %s\n", frame)
    ws.pool.Submit(func() {
        // Handle the WebSocket frame in a separate goroutine
        // ...
    })
    return nil, gnet.None
}

func (ws *wsServer) Tick() (delay time.Duration, action gnet.Action) {
    return 0, gnet.None
}

func main() {
    pool := goroutine.Default()
    err := gnet.Serve(&wsServer{pool: pool}, "ws://0.0.0.0:8080", gnet.WithMulticore(true))
    if err != nil {
        panic(err)
    }
}

This code creates a WebSocket server that listens on ws://0.0.0.0:8080 and processes incoming WebSocket frames in a separate goroutine using a goroutine pool.

  1. UDP Server:
package main

import (
    "fmt"
    "github.com/panjf2000/gnet"
)

type udpServer struct {
    gnet.EventServer
}

func (us *udpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    fmt.Printf("Received: %s\n", frame)

Competitor Comparisons

5,880

Fast event-loop networking for Go

Pros of evio

  • Simpler API and easier to use for basic networking tasks
  • Supports both TCP and UDP protocols out of the box
  • Includes built-in load balancing functionality

Cons of evio

  • Lower performance compared to gnet in high-concurrency scenarios
  • Less actively maintained, with fewer recent updates
  • Limited built-in features for advanced networking tasks

Code Comparison

evio example:

func main() {
    var events evio.Events
    events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
        out = in
        return
    }
    if err := evio.Serve(events, "tcp://localhost:5000"); err != nil {
        panic(err)
    }
}

gnet example:

type echoServer struct { gnet.EventHandler }

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    out = frame
    return
}

func main() {
    gnet.Serve(&echoServer{}, "tcp://:9000", gnet.WithMulticore(true))
}

Both libraries offer event-driven networking capabilities, but gnet provides more advanced features and better performance for high-concurrency scenarios. evio has a simpler API and built-in load balancing, making it easier to use for basic tasks. However, gnet is more actively maintained and offers superior performance in demanding environments.

21,607

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

Pros of fasthttp

  • Highly optimized for HTTP/1.1, offering superior performance for web applications
  • Provides a rich set of utilities for handling HTTP requests and responses
  • Well-established and widely used in production environments

Cons of fasthttp

  • Limited to HTTP protocol, not suitable for general-purpose networking
  • Lacks built-in support for HTTP/2 and WebSockets
  • More complex API compared to the standard net/http package

Code Comparison

fasthttp example:

func requestHandler(ctx *fasthttp.RequestCtx) {
    fmt.Fprintf(ctx, "Hello, world!")
}
fasthttp.ListenAndServe(":8080", requestHandler)

gnet example:

type server struct{ gnet.EventHandler }
func (s *server) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    return []byte("Hello, world!"), gnet.None
}
gnet.Serve(new(server), "tcp://:9000", gnet.WithMulticore(true))

Key Differences

  • fasthttp is specifically designed for HTTP, while gnet is a general-purpose networking framework
  • gnet offers event-driven programming model, while fasthttp follows a more traditional request-response pattern
  • fasthttp provides higher-level HTTP-specific abstractions, whereas gnet operates at a lower level, giving more control over the networking stack
2,151

Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.

Pros of nbio

  • More flexible API design, allowing for easier customization
  • Better support for WebSocket connections out of the box
  • Simpler codebase, potentially easier for newcomers to understand

Cons of nbio

  • Less mature project with fewer stars and contributors compared to gnet
  • May have lower performance in some high-concurrency scenarios
  • Less comprehensive documentation and examples

Code Comparison

nbio:

engine, err := nbio.NewEngine(nbio.Config{
    Network: "tcp",
    Addr:    addr,
})
engine.OnData(func(c *nbio.Conn, data []byte) {
    // Handle data
})

gnet:

type echoServer struct {
    *gnet.EventServer
}
func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    // Handle data
    return frame, gnet.None
}

Both libraries provide efficient networking capabilities for Go applications, but they differ in their API design and focus. nbio offers a more flexible approach with better WebSocket support, while gnet may have an edge in raw performance for certain use cases. The choice between them depends on specific project requirements and developer preferences.

4,011

A high-performance non-blocking I/O networking framework focusing on RPC scenarios.

Pros of netpoll

  • Designed specifically for RPC scenarios, optimized for high-performance microservices
  • Supports both Go and C++ implementations, offering flexibility for different tech stacks
  • Provides built-in support for connection multiplexing, enhancing efficiency in handling multiple concurrent requests

Cons of netpoll

  • Less mature and less widely adopted compared to gnet
  • May have a steeper learning curve due to its focus on RPC and microservices
  • Limited documentation and community resources compared to gnet

Code Comparison

netpoll:

listener, _ := netpoll.CreateListener("tcp", ":8888")
eventLoop, _ := netpoll.NewEventLoop(
    handler,
    netpoll.WithOnPrepare(onPrepare),
)
eventLoop.Serve(listener)

gnet:

type echoServer struct {
    *gnet.EventServer
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    out = frame
    return
}

gnet.Serve(&echoServer{}, "tcp://:9000", gnet.WithMulticore(true))

Both libraries offer high-performance networking capabilities, but netpoll is more focused on RPC scenarios, while gnet provides a more general-purpose networking solution. netpoll's connection multiplexing and support for multiple languages may be advantageous in certain use cases, but gnet's maturity and broader adoption could make it a safer choice for many projects.

handling 1M websockets connections in Go

Pros of 1m-go-websockets

  • Focused specifically on WebSocket performance and scalability
  • Demonstrates handling 1 million concurrent WebSocket connections
  • Simple and straightforward implementation for learning purposes

Cons of 1m-go-websockets

  • Limited to WebSocket functionality, not a general-purpose networking framework
  • Less actively maintained compared to gnet
  • Fewer features and optimizations for production use

Code Comparison

1m-go-websockets:

func wsHandler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println(err)
        return
    }
    // Handle WebSocket connection
}

gnet:

type echoServer struct {
    *gnet.EventServer
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    out = frame
    return
}

The 1m-go-websockets example focuses on upgrading HTTP connections to WebSockets, while gnet provides a more general-purpose event-driven networking framework. gnet offers greater flexibility for various network protocols and optimizations, making it suitable for a wider range of applications beyond WebSockets.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

gnet

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

🚀 Features

🦖 Milestone

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

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

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

⚠️ 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

💰 Backers

Support us with a monthly donation and help us continue our activities.

💎 Sponsors

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

☕️ 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.

        

🔑 JetBrains OS licenses

gnet had 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.

🔋 Sponsorship

This project is supported by: