Convert Figma logo to code with AI

tidwall logoevio

Fast event-loop networking for Go

5,880
491
5,880
27

Top Related Projects

9,481

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure 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.

handling 1M websockets connections in Go

Quick Overview

evio is a fast, lightweight event-loop networking framework for Go. It provides an efficient and easy-to-use API for building high-performance network applications, supporting both TCP and UDP protocols. evio is designed to handle a large number of concurrent connections with minimal resource usage.

Pros

  • High performance and low resource usage
  • Simple and intuitive API
  • Supports both TCP and UDP protocols
  • Suitable for building scalable network applications

Cons

  • Limited documentation and examples
  • May require additional libraries for more complex networking tasks
  • Not as feature-rich as some other networking frameworks
  • Relatively small community compared to more established libraries

Code Examples

Example 1: Basic TCP server

package main

import (
    "fmt"
    "github.com/tidwall/evio"
)

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

This example creates a simple TCP server that responds with "Hello, " followed by the received message.

Example 2: UDP server

package main

import (
    "fmt"
    "github.com/tidwall/evio"
)

func main() {
    var events evio.Events
    events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
        fmt.Printf("Received: %s\n", string(in))
        return
    }
    if err := evio.Serve(events, "udp://localhost:5000"); err != nil {
        panic(err)
    }
}

This example creates a UDP server that prints received messages to the console.

Example 3: Multiple listeners

package main

import (
    "fmt"
    "github.com/tidwall/evio"
)

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",
        "udp://localhost:5001",
        "unix://socket",
    ); err != nil {
        panic(err)
    }
}

This example demonstrates how to create a server that listens on multiple addresses and protocols simultaneously.

Getting Started

To use evio in your Go project, follow these steps:

  1. Install evio:

    go get -u github.com/tidwall/evio
    
  2. Import the package in your Go code:

    import "github.com/tidwall/evio"
    
  3. Create an evio.Events struct and define event handlers:

    var events evio.Events
    events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
        // Handle incoming data
        return
    }
    
  4. Start the server using evio.Serve:

    if err := evio.Serve(events, "tcp://localhost:5000"); err != nil {
        panic(err)
    }
    

Competitor Comparisons

9,481

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

Pros of gnet

  • Higher performance in benchmarks, especially for high-concurrency scenarios
  • More comprehensive feature set, including load balancing and custom protocols
  • Active development with frequent updates and community contributions

Cons of gnet

  • Steeper learning curve due to more complex API
  • Less flexibility for custom event loops compared to evio
  • Larger codebase and dependencies, potentially increasing binary size

Code Comparison

gnet example:

type echoServer struct {
    *gnet.EventServer
}

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

gnet.Serve(new(echoServer), "tcp://:9000", gnet.WithMulticore(true))

evio example:

var events evio.Events
events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
    out = in
    return
}

evio.Serve(events, "tcp://:9000")

Both libraries provide efficient networking capabilities for Go applications, but gnet offers higher performance and more features at the cost of increased complexity. evio, on the other hand, provides a simpler API and greater flexibility for custom event loops.

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

  • Optimized for high-performance HTTP operations, particularly suited for microservices and API servers
  • Provides a complete HTTP client and server implementation with minimal memory allocations
  • Offers a rich set of utilities for handling HTTP requests and responses efficiently

Cons of fasthttp

  • Limited to HTTP/1.x protocol, lacking support for HTTP/2 or newer protocols
  • May require more manual handling of certain HTTP features compared to the standard net/http package
  • Less suitable for general-purpose networking tasks outside of HTTP

Code Comparison

fasthttp example:

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

evio example:

var events evio.Events
events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
    out = []byte("Hello, world!")
    return
}
evio.Serve(events, "tcp://:8080")

Key Differences

  • fasthttp is specifically designed for HTTP, while evio is a general-purpose networking framework
  • evio provides lower-level control over network events, suitable for custom protocols
  • fasthttp offers higher-level HTTP-specific functionality out of the box
  • evio uses an event-driven model, while fasthttp follows a more traditional request-response pattern
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

  • Higher performance and lower latency in high-concurrency scenarios
  • More comprehensive feature set, including WebSocket support and TLS
  • Active development with frequent updates and improvements

Cons of nbio

  • Steeper learning curve due to more complex API
  • Less documentation and examples compared to evio
  • May be overkill for simpler networking tasks

Code Comparison

nbio:

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

evio:

var events evio.Events
events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
    out = in
    return
}
evio.Serve(events, "tcp://:8888")

Both libraries provide efficient network I/O handling in Go, but nbio offers more features and potentially better performance in high-concurrency scenarios. evio has a simpler API and may be easier to use for basic networking tasks. The choice between them depends on the specific requirements of your project, such as performance needs, feature requirements, and development complexity.

handling 1M websockets connections in Go

Pros of 1m-go-websockets

  • Specifically designed for WebSocket connections, offering optimized performance for this use case
  • Includes a benchmark tool for testing WebSocket server performance
  • Demonstrates scalability for handling a large number of concurrent WebSocket connections

Cons of 1m-go-websockets

  • Less versatile than evio, as it focuses solely on WebSocket connections
  • May require more manual configuration for complex networking scenarios
  • Limited documentation compared to evio

Code Comparison

evio:

events.Serve(events.Events{
    Addr:   ":8080",
    NumLoops: -1,
    Serving: func(srv events.Server) (action events.Action) {
        // Server is ready to accept connections
    },
})

1m-go-websockets:

http.HandleFunc("/ws", wsHandler)
if err := http.ListenAndServe(*addr, nil); err != nil {
    log.Fatal("ListenAndServe: ", err)
}

Summary

evio is a more general-purpose event loop networking framework, offering flexibility for various network protocols. 1m-go-websockets, on the other hand, is tailored specifically for WebSocket connections, providing optimized performance and scalability for this particular use case. While evio may be more suitable for diverse networking needs, 1m-go-websockets excels in WebSocket-specific scenarios, especially when handling a large number of concurrent connections.

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

evio
GoDoc

evio 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. It was built to be the foundation for Tile38 and a future L7 proxy for Go.

Please note: Evio should not be considered as a drop-in replacement for the standard Go net or net/http packages.

Features

Getting Started

Installing

To start using evio, install Go and run go get:

$ go get -u github.com/tidwall/evio

This will retrieve the library.

Usage

Starting a server is easy with evio. Just set up your events and pass them to the Serve function along with the binding address(es). Each connections is represented as an evio.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.

Example echo server that binds to port 5000:

package main

import "github.com/tidwall/evio"

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.Error())
	}
}

Here the only event being used is Data, which fires when the server receives input data from a client. The exact same input data is then passed through the output return value, which is then sent back to the client.

Connect to the echo server:

$ telnet localhost 5000

Events

The event type has a bunch of handy events:

  • Serving fires when the server is ready to accept new connections.
  • Opened fires when a connection has opened.
  • Closed fires when a connection has closed.
  • Detach fires when a connection has been detached using the Detach return action.
  • Data fires when the server receives new data from a connection.
  • Tick fires immediately after the server starts and will fire again after a specified interval.

Multiple addresses

A server can bind to multiple addresses and share the same event loop.

evio.Serve(events, "tcp://192.168.0.10:5000", "unix://socket")

Ticker

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
}

UDP

The Serve function can bind to UDP addresses.

  • All incoming and outgoing packets are not buffered and sent individually.
  • The Opened and Closed events are not availble for UDP sockets, only the Data event.

Multithreaded

The events.NumLoops options sets the number of loops to use for the server. A value greater than 1 will effectively make the server multithreaded for multi-core machines. Which means you must take care when synchonizing memory between event callbacks. Setting to 0 or 1 will run the server as single-threaded. Setting to -1 will automatically assign this value equal to runtime.NumProcs().

Load balancing

The events.LoadBalance options sets the load balancing method. Load balancing is always a best effort to attempt to distribute the incoming connections between multiple loops. This option is only available when events.NumLoops is set.

  • Random requests that connections are randomly distributed.
  • RoundRobin requests that connections are distributed to a loop in a round-robin fashion.
  • LeastConnections assigns the next accepted connection to the loop with the least number of active connections.

SO_REUSEPORT

Servers can utilize the SO_REUSEPORT option which allows multiple sockets on the same host to bind to the same port.

Just provide reuseport=true to an address:

evio.Serve(events, "tcp://0.0.0.0:1234?reuseport=true"))

More examples

Please check out the examples subdirectory for a simplified redis clone, an echo server, and a very basic http server.

To run an example:

$ go run examples/http-server/main.go
$ go run examples/redis-server/main.go
$ go run examples/echo-server/main.go

Performance

Benchmarks

These benchmarks were run on an ec2 c4.xlarge instance in single-threaded mode (GOMAXPROC=1) over Ipv4 localhost. Check out benchmarks for more info.

echo benchmarkhttp benchmarkredis 1 benchmarkredis 8 benchmark

Contact

Josh Baker @tidwall

License

evio source code is available under the MIT License.