evio

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 16 Imported by: 0

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/gnuos/evio

This will retrieve the library.

Usage

这个库暴露出的API如下:

type Engine interface {
    Start()
    Stop()
    Serve() error
    Ready() chan bool
    Clear()
    HasErr() bool
    Errors() iter.Seq[error]
}

详细用法可以参考 example_test.go 文件里的代码

Starting a server is easy with evio. Just set up your events and pass them to the NewEngine 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/gnuos/evio"

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

    addresses := []string{"tcp://localhost:5000"}
    engine, err := evio.NewEngine(events, addresses...)
	if err != nil {
		panic(err.Error())
	}

    engine.Serve()

    // or run in background
    // engine.Start()
    // <- engine.Ready()
    //
    // defer func() {
    //     serv.Stop()
    //     serv.Clear()
    // }()

    // Do other things
}

Here the only event being used is OnData, 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:

  • OnServing fires when the server is ready to accept new connections.
  • OnOpened fires when a connection has opened.
  • OnClosed fires when a connection has closed.
  • OnDetach fires when a connection has been detached using the Detach return action.
  • OnData fires when the server receives new data from a connection.
  • OnTick 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.NewEngine(events, "tcp://:5000", "unix://socket")

Ticker

The OnTick event fires ticks at a specified interval. The first tick fires immediately after the OnServing events.

events.Tick = func() (delay time.Duration, action Action){
	log.Printf("tick")
	delay = time.Second
	return
}

UDP

The NewEngine function can bind to UDP addresses.

  • All incoming and outgoing packets are not buffered and sent individually.
  • The OnOpened and OnClosed events are not availble for UDP sockets, only the OnData 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.

Collect connections

The events.ConnDeadline options sets the count of timeout seconds which accepted connection is inactive. This options use 15 as a default value. These accpted connections will be clean automatically.

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.NewEngine(events, "tcp://0.0.0.0:1234?reuseport=true"))

Contact

Josh Baker @tidwall

Kevin email

License

evio source code is available under the MIT License.

Documentation

Overview

evio is forked from https://github.com/tidwall/evio

Package evio is an event loop networking framework.

Index

Examples

Constants

View Source
const DefaultConnDeadline int64 = 15

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action is an action that occurs after the completion of an event.

const (
	// None indicates that no action should occur following an event.
	None Action = iota
	// Detach detaches a connection. Not available for UDP connections.
	Detach
	// Close closes the connection.
	Close
	// Shutdown shutdowns the server.
	Shutdown
)

type Conn

type Conn interface {
	// Context returns a user-defined context.
	Context() any
	// SetContext sets a user-defined context.
	SetContext(any)
	// AddrIndex is the index of server address that was passed to the Serve call.
	AddrIndex() int
	// LocalAddr is the connection's local socket address.
	LocalAddr() net.Addr
	// RemoteAddr is the connection's remote peer address.
	RemoteAddr() net.Addr
	// Wake triggers a Data event for this connection.
	Wake()
}

Conn is an evio connection.

type Engine

type Engine interface {
	Start()
	Stop()
	Serve() error
	Ready() chan bool
	Clear()
	HasErr() bool
	Errors() iter.Seq[error]
}

Engine is an abstract Server definition

Example
package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"strings"
	"time"
)

func main() {
	var (
		port       = 6399
		unixsocket = "example-test.sock"
	)

	var (
		events Events
	)

	events.NumLoops = 2
	events.OnServing = func(srv ServerInfo) (action Action) {
		// log.Printf("echo server started on port %d (loops: %d)", port, srv.NumLoops)
		if unixsocket != "" {
			// log.Printf("echo server started at %s (loops: %d)", unixsocket, srv.NumLoops)
		}
		return
	}

	events.OnOpened = func(ec Conn) (out []byte, opts Options, action Action) {
		// log.Printf("opened: %v", ec.RemoteAddr())
		ec.SetContext((*InputStream)(nil))
		opts.ReuseInputBuffer = true
		opts.TCPKeepAlive = 30 * time.Second
		return
	}

	events.OnClosed = func(ec Conn, err error) (action Action) {
		// log.Printf("closed: %v", ec.RemoteAddr())
		return
	}

	events.OnData = makeHandler()

	addrs := []string{fmt.Sprintf("tcp://:%d", port)}
	if unixsocket != "" {
		addrs = append(addrs, fmt.Sprintf("unix://%s", unixsocket))
	}

	var err error
	var c net.Conn

	serv, err := NewEngine(events, addrs...)
	if err != nil {
		log.Fatal(err)
	}

	serv.Start()

	<-serv.Ready()

	if unixsocket != "" {
		c, err = net.Dial("unix", unixsocket)
	} else {
		c, err = net.Dial("tcp", "127.0.0.1:6399")
	}

	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		_ = c.Close()
		serv.Stop()
		serv.Clear()
		_ = os.Remove(unixsocket)
	}()

	_, err = c.Write([]byte("GET"))
	if err != nil {
		log.Println(err)
		return
	}

	var b = make([]byte, 128)

	_, err = c.Read(b)
	if err != nil {
		log.Println(err)
		return
	}

	fmt.Printf("%s", strings.TrimSpace(string(b)))
}

func makeHandler() func(Conn, []byte) ([]byte, Action) {
	return func(ec Conn, in []byte) (out []byte, action Action) {
		if in == nil {
			log.Printf("wake from %s\n", ec.RemoteAddr())
			return nil, Close
		}
		ec.SetContext((*InputStream)(nil))

		out = in
		return
	}
}

func NewEngine

func NewEngine(events Events, addrs ...string) (Engine, error)

NewEngine create a evloop server instance.

It run many event-loops to process network connection

Addresses should use a scheme prefix and be formatted like `tcp://192.168.0.10:9851` or `unix://socket`. Valid network schemes:

tcp   - bind to both IPv4 and IPv6
tcp4  - IPv4
tcp6  - IPv6
udp   - bind to both IPv4 and IPv6
udp4  - IPv4
udp6  - IPv6
unix  - Unix Domain Socket

The "tcp" network scheme is assumed when one is not specified.

type Events

type Events struct {
	// NumLoops sets the number of loops to use for the server. Setting this
	// to a value greater than 1 will effectively make the server
	// multithreaded for multi-core machines. Which means you must take care
	// with synchonizing memory between all event callbacks. Setting to 0 or 1
	// will run the server single-threaded. Setting to -1 will automatically
	// assign this value equal to runtime.NumProcs().
	NumLoops int

	// ConnDeadline sets dealine duration that apply to every net connection
	// This value is measured in seconds. (Time of idle)
	ConnDeadline int64

	// LoadBalance 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 works when NumLoops is set.
	LoadBalance LoadBalance

	// Serving fires when the server can accept connections. The server
	// parameter has information and various utilities.
	OnServing func(server ServerInfo) (action Action)

	// Opened fires when a new connection has opened.
	// The info parameter has information about the connection such as
	// it's local and remote address.
	// Use the out return value to write data to the connection.
	// The opts return value is used to set connection options.
	OnOpened func(c Conn) (out []byte, opts Options, action Action)

	// Closed fires when a connection has closed.
	// The err parameter is the last known connection error.
	OnClosed func(c Conn, err error) (action Action)

	// Detached fires when a connection has been previously detached.
	// Once detached it's up to the receiver of this event to manage the
	// state of the connection. The Closed event will not be called for
	// this connection.
	// The conn parameter is a ReadWriteCloser that represents the
	// underlying socket connection. It can be freely used in goroutines
	// and should be closed when it's no longer needed.
	OnDetached func(c Conn, rwc io.ReadWriteCloser) (action Action)

	// PreWrite fires just before any data is written to any client socket.
	OnPreWrite func()

	// Data fires when a connection sends the server data.
	// The in parameter is the incoming data.
	// Use the out return value to write data to the connection.
	OnData func(c Conn, in []byte) (out []byte, action Action)

	// Tick fires immediately after the server starts and will fire again
	// following the duration specified by the delay return value.
	OnTick func() (delay time.Duration, action Action)
}

Events represents the server events for the Serve call. Each event has an Action return value that is used manage the state of the connection and server.

type InputStream

type InputStream struct {
	// contains filtered or unexported fields
}

InputStream is a helper type for managing input streams from inside the Data event.

func (*InputStream) Begin

func (is *InputStream) Begin(packet []byte) (data []byte)

Begin accepts a new packet and returns a working sequence of unprocessed bytes.

func (*InputStream) End

func (is *InputStream) End(data []byte)

End shifts the stream to match the unprocessed data.

type LoadBalance

type LoadBalance int

LoadBalance sets the load balancing method.

const (
	// Random requests that connections are randomly distributed.
	Random LoadBalance = iota
	// RoundRobin requests that connections are distributed to a loop in a
	// round-robin fashion.
	RoundRobin
	// LeastConnections assigns the next accepted connection to the loop with
	// the least number of active connections.
	LeastConnections
)

type Options

type Options struct {
	// TCPKeepAlive (SO_KEEPALIVE) socket option.
	TCPKeepAlive time.Duration
	// ReuseInputBuffer will forces the connection to share and reuse the
	// same input packet buffer with all other connections that also use
	// this option.
	// Default value is false, which means that all input data which is
	// passed to the Data event will be a uniquely copied []byte slice.
	ReuseInputBuffer bool
}

Options are set when the client opens.

type ServerInfo

type ServerInfo struct {
	// The addrs parameter is an array of listening addresses that align
	// with the addr strings passed to the Serve function.
	Addrs []net.Addr
	// NumLoops is the number of loops that the server is using.
	NumLoops int
}

ServerInfo represents a server context which provides information about the running server and has control functions for managing state.

Directories

Path Synopsis
Package internal is the concrete implementation of epoll and kqueue in low-level OpenPoll factory method is package entry.
Package internal is the concrete implementation of epoll and kqueue in low-level OpenPoll factory method is package entry.
Package reuseport provides Listen and Dial functions that set socket options in order to be able to reuse ports.
Package reuseport provides Listen and Dial functions that set socket options in order to be able to reuse ports.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL