eviop

package module
v0.0.0-...-d421704 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: MIT Imports: 13 Imported by: 5

README

eviop

Github Actions LICENSE Go Report Card Codacy Badge Code Size

eviop 是一个快速、轻便的事件循环网络框架。

它使用直接的 epoll 和 kqueue 系统调用,而不是使用标准的 Go net 包,并且以与 libuv 和 libevent 类似的方式工作。

eviop 从 evio fork 而来,并且优化了些许,尽可能减少内存拷贝,性能更优。

如下测试数据在 MacBook Air 上运行得出 (kqueue) image

如下测试数据在 Ubuntu 18.04 上运行得出 (epoll) image

安装

$ go get -u github.com/Allenxuxu/eviop

示例

package main

import (
	"flag"
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/Allenxuxu/eviop"
)

func main() {
	var port int
	var loops int
	var udp bool
	var trace bool
	var reuseport bool

	flag.IntVar(&port, "port", 5000, "server port")
	flag.BoolVar(&udp, "udp", false, "listen on udp")
	flag.BoolVar(&reuseport, "reuseport", false, "reuseport (SO_REUSEPORT)")
	flag.BoolVar(&trace, "trace", false, "print packets to console")
	flag.IntVar(&loops, "loops", 0, "num loops")
	flag.Parse()

	var events eviop.Events
	events.NumLoops = loops
	events.Serving = func(srv eviop.Server) (action eviop.Action) {
		log.Printf("echo server started on port %d (loops: %d)", port, srv.NumLoops)
		if reuseport {
			log.Printf("reuseport")
		}
		return
	}
	events.Data = func(c *eviop.Conn) (action eviop.Action) {
		first, end := c.PeekAll()
		if trace {
			log.Printf("%s", strings.TrimSpace(string(first)+string(end)))
		}
		c.Send(first)
		if len(end) > 0 {
			c.Send(end)
		}
		c.RetrieveAll()
		return
	}
	scheme := "tcp"
	if udp {
		scheme = "udp"
	}
	log.Fatal(eviop.Serve(events, time.Second*10, fmt.Sprintf("%s://:%d?reuseport=%t", scheme, port, reuseport)))
}

相关文章

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(events Events, waitTimeout time.Duration, 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
	// Close closes the connection.
	Close
	// Shutdown shutdowns the server.
	Shutdown
)

type AtomicBool

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

AtomicBool 原子操作封装的 bool 类型

func (*AtomicBool) Get

func (a *AtomicBool) Get() bool

Get 获取指

func (*AtomicBool) Set

func (a *AtomicBool) Set(b bool)

Set 设置值

type Conn

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

Conn tcp连接

func (*Conn) AddrIndex

func (c *Conn) AddrIndex() int

AddrIndex AddrIndex

func (*Conn) Context

func (c *Conn) Context() interface{}

Context 获取 Context

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr LocalAddr

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr RemoteAddr

func (*Conn) Send

func (c *Conn) Send(buf []byte)

Send 发送 供非 loop 协程调用

func (*Conn) SetContext

func (c *Conn) SetContext(ctx interface{})

SetContext 设置 Context

func (*Conn) Wake

func (c *Conn) Wake()

Wake 唤醒 loop

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
	// 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.
	Serving func(server Server) (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.
	Opened 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.
	Closed func(c *Conn, err error) (action Action)
	// PreWrite fires just before any data is written to any client socket.
	PreWrite 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.
	Data func(c *Conn, in *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.

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
}

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
examples

Jump to

Keyboard shortcuts

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