gfaio

package module
v0.0.0-...-14c233f Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

fastnet

🔧A fast Server in go.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler interface {
	connection.CallBack
	OnConnect(c *connection.Connection)
}

Handler:Server 注册接口

type Option

type Option func(*Options)

Option ...

func Address

func Address(a string) Option

Address:server 监听地址

func IdleTime

func IdleTime(t time.Duration) Option

IdleTime:最大空闲时间(秒)

func Network

func Network(n string) Option

Network:暂时只支持tcp

func NumLoops

func NumLoops(n int) Option

NumLoops:work eventloop 的数量

func Protocol

func Protocol(p connection.Protocol) Option

Protocol:数据包处理

func ReusePort

func ReusePort(reusePort bool) Option

ReusePort:设置 SO_REUSEPORT

type Options

type Options struct {
	Network   string // 网络协议
	Address   string // 端口地址
	NumLoops  int    // work 协程,负责处理已连接客户端的读写事件
	ReusePort bool   // 是否开启端口复用

	IdleTime time.Duration       // 最大空闲时间(秒)
	Protocol connection.Protocol // 连接协议
	// contains filtered or unexported fields
}

Options:服务配置

type Server

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

Server:gfaio Server

func NewServer

func NewServer(handler Handler, opts ...Option) (server *Server, err error)

NewServer:创建 Server

func (*Server) Options

func (s *Server) Options() Options

Options:返回 options

func (*Server) RunAfter

func (s *Server) RunAfter(d time.Duration, f func()) *timingwheel.Timer

RunAfter:延时任务

Example
handler := new(example)

s, err := NewServer(handler,
	Network("tcp"),
	Address(":1833"),
	NumLoops(8),
	ReusePort(true))
if err != nil {
	panic(err)
}

go s.Start()
defer s.Stop()

s.RunAfter(time.Second, func() {
	fmt.Println("RunAfter")
})

time.Sleep(2500 * time.Millisecond)
Output:

RunAfter

func (*Server) RunEvery

func (s *Server) RunEvery(d time.Duration, f func()) *timingwheel.Timer

RunEvery:定时任务

Example
handler := new(example)

s, err := NewServer(handler,
	Network("tcp"),
	Address(":1833"),
	NumLoops(8),
	ReusePort(true))
if err != nil {
	panic(err)
}

go s.Start()
defer s.Stop()

t := s.RunEvery(time.Second, func() {
	fmt.Println("EveryFunc")
})

time.Sleep(4500 * time.Millisecond)
t.Stop()
time.Sleep(4500 * time.Millisecond)
Output:

EveryFunc
EveryFunc
EveryFunc
EveryFunc

func (*Server) Start

func (s *Server) Start()

Start:启动 Server

func (*Server) Stop

func (s *Server) Stop()

Stop:关闭 Server

Directories

Path Synopsis
example
Package log is from https://github.com/micro/go-micro/blob/master/util/log/log.go
Package log is from https://github.com/micro/go-micro/blob/master/util/log/log.go
plugins
tool

Jump to

Keyboard shortcuts

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