Documentation
¶
Overview ¶
gnet is an event-driven 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 netty and libuv.
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, non-blocking, event-driven networking framework written in pure Go which works on transport layer with TCP/UDP/Unix-Socket protocols, so it allows developers to implement their own protocols of application layer upon gnet for building diversified network applications, for instance, you get an HTTP Server or Web Framework 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.
Echo server built upon gnet is shown below:
package main import ( "log" "github.com/panjf2000/gnet" ) type echoServer struct { *gnet.EventServer } func (es *echoServer) React(c gnet.Conn) (out []byte, action gnet.Action) { out = c.Read() c.ResetBuffer() return } func main() { echo := new(echoServer) log.Fatal(gnet.Serve(echo, "tcp://:9000", gnet.WithMulticore(true))) }
Index ¶
- func Serve(eventHandler EventHandler, addr string, opts ...Option) error
- type Action
- type Conn
- type EventHandler
- type EventServer
- func (es *EventServer) OnClosed(c Conn, err error) (action Action)
- func (es *EventServer) OnInitComplete(svr Server) (action Action)
- func (es *EventServer) OnOpened(c Conn) (out []byte, action Action)
- func (es *EventServer) PreWrite()
- func (es *EventServer) React(c Conn) (out []byte, action Action)
- func (es *EventServer) Tick() (delay time.Duration, action Action)
- type IEventLoopGroup
- type Option
- type Options
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Serve ¶
func Serve(eventHandler EventHandler, addr string, opts ...Option) 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 Conn ¶
type Conn interface { // Context returns a user-defined context. Context() (ctx interface{}) // SetContext sets a user-defined context. SetContext(ctx interface{}) // LocalAddr is the connection's local socket address. LocalAddr() (addr net.Addr) // RemoteAddr is the connection's remote peer address. RemoteAddr() (addr net.Addr) // Read reads all data from inbound ring-buffer without moving "read" pointer, which means // it does not evict the data from ring-buffer actually and those data will present in ring-buffer until the // ResetBuffer method is invoked. Read() (buf []byte) // ResetBuffer resets the inbound ring-buffer, which means all data in the inbound ring-buffer has been evicted. ResetBuffer() // ReadN reads bytes with the given length from inbound ring-buffer and event-loop-buffer, it would move // "read" pointer, which means it will evict the data from buffer and it can't be revoked (put back to buffer), // it reads data from the inbound ring-buffer and event-loop-buffer when the length of the available data is equal // to the given "n", otherwise, it will not read any data from the inbound ring-buffer. So you should use this // function only if you know exactly the length of subsequent TCP streams based on the protocol, like the // Content-Length attribute in an HTTP request which indicates you how much data you should read from inbound ring-buffer. ReadN(n int) (size int, buf []byte) // BufferLength returns the length of available data in the inbound ring-buffer. BufferLength() (size int) // AyncWrite writes data to client/connection asynchronously. AsyncWrite(buf []byte) // Wake triggers a React event for this connection. Wake() }
Conn is a interface of gnet connection.
type EventHandler ¶ added in v1.0.0
type EventHandler interface { // OnInitComplete fires when the server is ready for accepting connections. // The server parameter has information and various utilities. OnInitComplete(server Server) (action Action) // OnOpened fires when a new connection has been 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. OnOpened(c Conn) (out []byte, action Action) // OnClosed fires when a connection has been closed. // The err parameter is the last known connection error. OnClosed(c Conn, err error) (action Action) // PreWrite fires just before any data is written to any client socket. PreWrite() // React fires when a connection sends the server data. // Invoke c.Read() or c.ReadN(n) within the parameter c to read incoming data from client/connection. // Use the out return value to write data to the client/connection. React(c Conn) (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() (delay time.Duration, action Action) }
EventHandler represents the server events' callbacks for the Serve call. Each event has an Action return value that is used manage the state of the connection and server.
type EventServer ¶ added in v1.0.0
type EventServer struct { }
EventServer is a built-in implementation of EventHandler which sets up each method with a default implementation, you can compose it with your own implementation of EventHandler when you don't want to implement all methods in EventHandler.
func (*EventServer) OnClosed ¶ added in v1.0.0
func (es *EventServer) OnClosed(c Conn, err error) (action Action)
OnClosed fires when a connection has been closed. The err parameter is the last known connection error.
func (*EventServer) OnInitComplete ¶ added in v1.0.0
func (es *EventServer) OnInitComplete(svr Server) (action Action)
OnInitComplete fires when the server is ready for accepting connections. The server parameter has information and various utilities.
func (*EventServer) OnOpened ¶ added in v1.0.0
func (es *EventServer) OnOpened(c Conn) (out []byte, action Action)
OnOpened fires when a new connection has been 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.
func (*EventServer) PreWrite ¶ added in v1.0.0
func (es *EventServer) PreWrite()
PreWrite fires just before any data is written to any client socket.
func (*EventServer) React ¶ added in v1.0.0
func (es *EventServer) React(c Conn) (out []byte, action Action)
React fires when a connection sends the server data. Invoke c.Read() or c.ReadN(n) within the parameter c to read incoming data from client/connection. Use the out return value to write data to the client/connection.
type IEventLoopGroup ¶ added in v1.0.0
type IEventLoopGroup interface {
// contains filtered or unexported methods
}
IEventLoopGroup represents a set of event-loops.
type Option ¶ added in v1.0.0
type Option func(opts *Options)
Option is a function that will set up option.
func WithOptions ¶ added in v1.0.0
WithOptions sets up all options.
func WithTCPKeepAlive ¶ added in v1.0.0
WithTCPKeepAlive ...
type Options ¶
type Options struct { // Multicore indicates whether the server will be effectively created with multi-cores, if so, // then you must take care with synchonizing memory between all event callbacks, otherwise, // it will run the server with single thread. The number of threads in the server will be automatically // assigned to the value of runtime.NumCPU(). Multicore bool // ReusePort indicates whether to set up the SO_REUSEPORT option. ReusePort bool // Ticker indicates whether the ticker has been set up. Ticker bool // TCPKeepAlive (SO_KEEPALIVE) socket option. TCPKeepAlive time.Duration }
Options are set when the client opens.
type Server ¶
type Server struct { // Multicore indicates whether the server will be effectively created with multi-cores, if so, // then you must take care of synchronizing the shared data between all event callbacks, otherwise, // it will run the server with single thread. The number of threads in the server will be automatically // assigned to the value of runtime.NumCPU(). Multicore bool // The addrs parameter is an array of listening addresses that align // with the addr strings passed to the Serve function. Addr net.Addr // NumLoops is the number of loops that the server is using. NumLoops int // ReUsePort indicates whether SO_REUSEPORT is enable. ReUsePort bool // TCPKeepAlive (SO_KEEPALIVE) socket option. TCPKeepAlive time.Duration }
Server represents a server context which provides information about the running server and has control functions for managing state.