gnet

package module
v0.0.1-rc.2 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2019 License: MIT Imports: 14 Imported by: 128

README

gnet

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

gnet sells itself as a high-performance, lightweight, nonblocking network library written in pure Go.

gent is derived from project evio while having higher performance.

gnet is still under active development, so if you are interested in gnet, please feel free to make your code contributions to it ~~

Benchmark Test

On Linux (epoll)

Test Environment
Go Version: go1.12.9 linux/amd64
Machine:    Amazon c5.2xlarge
OS:         Ubuntu 18.04
CPU:        8 Virtual CPUs
Memory:     16.0 GiB
Echo Server

HTTP Server

On FreeBSD (kqueue)

Test Environment
Go Version: go version go1.12.9 darwin/amd64
Machine:    MacBook Pro
OS:         macOS Mojave 10.14.6
CPU:        4 CPUs
Memory:     8.0 GiB
Echo Server

HTTP Server

Documentation

Index

Constants

View Source
const (
	// RingBufferSize indicates the size of disruptor ring-buffer.
	RingBufferSize = 1024
	// RingBufferMask ...
	RingBufferMask = RingBufferSize - 1
	// DisruptorCleanup ...
	DisruptorCleanup = time.Millisecond * 10
)

Variables

This section is empty.

Functions

func Serve

func Serve(events Events, addr ...string) error

Serve starts handling events for the specified addresses.

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.

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() interface{}
	// SetContext sets a user-defined context.
	SetContext(interface{})
	// 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 React event for this connection.
	Wake()
}

Conn is an gnet connection.

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
	// OnInitComplete fires when the server can accept connections. The server
	// parameter has information and various utilities.
	OnInitComplete func(server Server) (action Action)
	// OnOpened 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)
	// OnClosed fires when a connection has closed.
	// The err parameter is the last known connection error.
	OnClosed func(c Conn, err error) (action Action)
	// OnDetached 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 OnClosed 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.
	PreWrite func()
	// React 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.
	React func(c Conn, inBuf *ringbuffer.RingBuffer) (out []byte, action Action)
	// Tick fires immediately after the server starts and will fire again
	// following the duration specified by the delay return value.
	Tick 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 LoadBalance

type LoadBalance int

LoadBalance sets the load balancing method.

type Options

type Options struct {
	// TCPKeepAlive (SO_KEEPALIVE) socket option.
	TCPKeepAlive time.Duration
}

Options are set when the client opens.

type Server

type Server 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
}

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

Directories

Path Synopsis
fasthttp-server command
net-echo-server command
net-http-server command
examples
echo-server command
http-server command

Jump to

Keyboard shortcuts

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