gnet

package
v0.0.0-...-f683eec Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WriteAsync       = network.WriteAsync
	WriteImmediately = network.WriteImmediately
)

import const value

Variables

View Source
var GNETServerContextPool process.ContextPool = &gnetServerContextPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return &sessionCtx{}
		},
	},
}
View Source
var GoClientContextPool process.ContextPool = &goClientContextPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return &clientCtx{}
		},
	},
}

Functions

func InstallClientOptionsWatchDog

func InstallClientOptionsWatchDog(dog func(cc *ClientOptions))

InstallClientOptionsWatchDog install watch dog

func InstallServerOptionsWatchDog

func InstallServerOptionsWatchDog(dog func(cc *ServerOptions))

InstallServerOptionsWatchDog install watch dog

func NewService

func NewService(name string, opt ...ServerOption) app.Service

Types

type Client

type Client = network.Client

import type

func NewClient

func NewClient(opts ...ClientOption) (_ Client, err error)

func NewClientForProxy

func NewClientForProxy(net, addr string, inner *process.InnerOptions) (Client, error)

NewClientForProxy new client for client proxy. NOTE: you should rewrite this function for custom set option

type ClientContext

type ClientContext = network.ClientContext

import type

type ClientOption

type ClientOption func(cc *ClientOptions) ClientOption

ClientOption option define

func WithClientOptionAddr

func WithClientOptionAddr(v string) ClientOption

Addr Server Addr

func WithClientOptionAutoReconnectTime

func WithClientOptionAutoReconnectTime(v int) ClientOption

AutoReconnect auto reconnect server. zero means not reconnect!

func WithClientOptionBlockConnect

func WithClientOptionBlockConnect(v bool) ClientOption

BlockConnect 创建客户端时候,是否阻塞等待链接服务器

func WithClientOptionFrameLogger

func WithClientOptionFrameLogger(v *zaplog.Logger) ClientOption

frame log

func WithClientOptionLoadBalancing

func WithClientOptionLoadBalancing(v gnet.LoadBalancing) ClientOption

WithLoadBalancing sets up the load-balancing algorithm in gnet server.

func WithClientOptionLockOSThread

func WithClientOptionLockOSThread(v bool) ClientOption

WithLockOSThread sets up LockOSThread mode for I/O event-loops.

func WithClientOptionMulticore

func WithClientOptionMulticore(v bool) ClientOption

WithMulticore sets up multi-cores in gnet server.

func WithClientOptionNetwork

func WithClientOptionNetwork(v string) ClientOption

func WithClientOptionNumEventLoop

func WithClientOptionNumEventLoop(v int) ClientOption

WithNumEventLoop sets up NumEventLoop in gnet server.

func WithClientOptionProcessOptions

func WithClientOptionProcessOptions(v ...process.ProcessOption) ClientOption

Process Options

func WithClientOptionReadBufferCap

func WithClientOptionReadBufferCap(v int) ClientOption

WithReadBufferCap sets up ReadBufferCap for reading bytes.

func WithClientOptionReusePort

func WithClientOptionReusePort(v bool) ClientOption

WithReusePort sets up SO_REUSEPORT socket option.

func WithClientOptionReuseReadBuffer

func WithClientOptionReuseReadBuffer(v bool) ClientOption

ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter. 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。

func WithClientOptionRouter

func WithClientOptionRouter(v Router) ClientOption

process router

func WithClientOptionSocketRecvBuffer

func WithClientOptionSocketRecvBuffer(v int) ClientOption

WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.

func WithClientOptionSocketSendBuffer

func WithClientOptionSocketSendBuffer(v int) ClientOption

WithSocketSendBuffer sets the maximum socket send buffer in bytes.

func WithClientOptionStopImmediately

func WithClientOptionStopImmediately(v bool) ClientOption

StopImmediately when session finish,business finish immediately.

func WithClientOptionTCPKeepAlive

func WithClientOptionTCPKeepAlive(v time.Duration) ClientOption

WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.

func WithClientOptionTCPNoDelay

func WithClientOptionTCPNoDelay(v gnet.TCPSocketOpt) ClientOption

WithTCPNoDelay enable/disable the TCP_NODELAY socket option.

func WithClientOptionTicker

func WithClientOptionTicker(v time.Duration) ClientOption

WithTicker indicates that a ticker is set.

func WithClientOptionWriteMethods

func WithClientOptionWriteMethods(v WriteMethod) ClientOption

Write network data method.

type ClientOptions

type ClientOptions struct {
	Network string
	// Addr Server Addr
	Addr string
	// Process Options
	ProcessOptions []process.ProcessOption
	// process router
	Router Router
	// frame log
	FrameLogger *zaplog.Logger
	// AutoReconnect auto reconnect server. zero means not reconnect!
	AutoReconnectTime int
	// StopImmediately when session finish,business finish immediately.
	StopImmediately bool
	// WithMulticore sets up multi-cores in gnet server.
	Multicore bool
	// WithLockOSThread sets up LockOSThread mode for I/O event-loops.
	LockOSThread bool
	// WithLoadBalancing sets up the load-balancing algorithm in gnet server.
	LoadBalancing gnet.LoadBalancing
	// WithNumEventLoop sets up NumEventLoop in gnet server.
	NumEventLoop int
	// WithReusePort sets up SO_REUSEPORT socket option.
	ReusePort bool
	// WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.
	TCPKeepAlive time.Duration
	// WithTCPNoDelay enable/disable the TCP_NODELAY socket option.
	TCPNoDelay gnet.TCPSocketOpt
	// WithReadBufferCap sets up ReadBufferCap for reading bytes.
	ReadBufferCap int
	// WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.
	SocketRecvBuffer int
	// WithSocketSendBuffer sets the maximum socket send buffer in bytes.
	SocketSendBuffer int
	// WithTicker indicates that a ticker is set.
	Ticker time.Duration
	// BlockConnect 创建客户端时候,是否阻塞等待链接服务器
	BlockConnect bool
	// Write network data method.
	WriteMethods WriteMethod
	// ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter.
	// 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。
	// 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。
	ReuseReadBuffer bool
}

ClientOption

func NewClientOptions

func NewClientOptions(opts ...ClientOption) *ClientOptions

NewClientOptions create options instance.

func (*ClientOptions) ApplyOption

func (cc *ClientOptions) ApplyOption(opts ...ClientOption)

ApplyOption modify options

func (*ClientOptions) GetSetOption

func (cc *ClientOptions) GetSetOption(opt ClientOption) ClientOption

GetSetOption modify and get last option

func (*ClientOptions) SetOption

func (cc *ClientOptions) SetOption(opt ClientOption)

SetOption modify options

type GNetClient

type GNetClient struct {
	*rpc.RPCProcess
	// contains filtered or unexported fields
}

GNetClient gnet.Client 封装

func NewClientEx

func NewClientEx(inner *process.InnerOptions, copts *ClientOptions) (cli *GNetClient, err error)

NewClientEx 创建客户端 inner *process.InnerOptions 选项应该由上层ClientProxy去决定如何设置。 copts 内部应该设置链接相关的参数。比如读写超时,如何发送数据 opts 业务方决定

func (*GNetClient) AddCloseClientFunc

func (sess *GNetClient) AddCloseClientFunc(f func(sess Client))

func (*GNetClient) Close

func (c *GNetClient) Close() (err error)

func (*GNetClient) GetConn

func (c *GNetClient) GetConn() interface{}

GetConn get raw conn(net.Conn,websocket.Conn...)

func (*GNetClient) OnBoot

func (c *GNetClient) OnBoot(eng gnet.Engine) (action gnet.Action)

OnBoot fires when the engine is ready for accepting connections. The parameter engine has information and various utilities.

func (*GNetClient) OnClose

func (c *GNetClient) OnClose(conn gnet.Conn, err error) (action gnet.Action)

OnClose fires when a connection has been closed. The parameter err is the last known connection error.

func (*GNetClient) OnOpen

func (c *GNetClient) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action)

OnOpen fires when a new connection has been opened.

The Conn c has information about the connection such as its local and remote addresses. The parameter out is the return value which is going to be sent back to the peer. Sending large amounts of data back to the peer in OnOpen is usually not recommended.

func (*GNetClient) OnShutdown

func (c *GNetClient) OnShutdown(eng gnet.Engine)

OnShutdown fires when the engine is being shut down, it is called right after all event-loops and connections are closed.

func (*GNetClient) OnTick

func (c *GNetClient) OnTick() (delay time.Duration, action gnet.Action)

OnTick fires immediately after the engine starts and will fire again following the duration specified by the delay return value.

func (*GNetClient) OnTraffic

func (c *GNetClient) OnTraffic(conn gnet.Conn) (action gnet.Action)

OnTraffic fires when a socket receives data from the peer.

Note that the []byte returned from Conn.Peek(int)/Conn.Next(int) is not allowed to be passed to a new goroutine, as this []byte will be reused within event-loop after OnTraffic() returns. If you have to use this []byte in a new goroutine, you should either make a copy of it or call Conn.Read([]byte) to read data into your own []byte, then pass the new []byte to the new goroutine.

func (*GNetClient) Write

func (c *GNetClient) Write(in []byte) (n int, err error)

type GNetServer

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

GNetServer impletion gnet.EventHandler

func NewServer

func NewServer(opts ...ServerOption) *GNetServer

func (*GNetServer) Broadcast

func (s *GNetServer) Broadcast(uri interface{}, msg interface{}, md metadata.MD) error

func (*GNetServer) BroadcastFilter

func (s *GNetServer) BroadcastFilter(filter func(Session) bool, uri interface{}, msg interface{}, md metadata.MD) error

func (*GNetServer) ForEach

func (s *GNetServer) ForEach(f func(Session))

func (*GNetServer) OnBoot

func (svr *GNetServer) OnBoot(eng gnet.Engine) (action gnet.Action)

OnBoot fires when the engine is ready for accepting connections. The parameter engine has information and various utilities.

func (*GNetServer) OnClose

func (svr *GNetServer) OnClose(c gnet.Conn, err error) (action gnet.Action)

OnClose fires when a connection has been closed. The parameter err is the last known connection error.

func (*GNetServer) OnOpen

func (svr *GNetServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action)

OnOpen fires when a new connection has been opened.

The Conn c has information about the connection such as its local and remote addresses. The parameter out is the return value which is going to be sent back to the peer. Sending large amounts of data back to the peer in OnOpen is usually not recommended.

func (*GNetServer) OnShutdown

func (svr *GNetServer) OnShutdown(eng gnet.Engine)

func (*GNetServer) OnTick

func (svr *GNetServer) OnTick() (delay time.Duration, action gnet.Action)

OnTick fires immediately after the engine starts and will fire again following the duration specified by the delay return value.

func (*GNetServer) OnTraffic

func (svr *GNetServer) OnTraffic(c gnet.Conn) (action gnet.Action)

OnTraffic fires when a socket receives data from the peer.

Note that the []byte returned from Conn.Peek(int)/Conn.Next(int) is not allowed to be passed to a new goroutine, as this []byte will be reused within event-loop after OnTraffic() returns. If you have to use this []byte in a new goroutine, you should either make a copy of it or call Conn.Read([]byte) to read data into your own []byte, then pass the new []byte to the new goroutine.

func (*GNetServer) Run

func (svr *GNetServer) Run(addr string) (err error)

func (*GNetServer) Shutdown

func (s *GNetServer) Shutdown(ctx context.Context) (err error)

type GNetService

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

GNetService implement app.Service interface

func (*GNetService) Finish

func (svc *GNetService) Finish()

func (*GNetService) Init

func (svc *GNetService) Init(s app.Stoper) (err error)

func (*GNetService) Name

func (svc *GNetService) Name() string

func (*GNetService) Start

func (svc *GNetService) Start(s app.Stoper) (err error)

func (*GNetService) Stop

func (svc *GNetService) Stop()

type GNetSession

type GNetSession struct {
	// process
	*rpc.RPCProcess
	// contains filtered or unexported fields
}

server session

func (*GNetSession) AddCloseSessionFunc

func (sess *GNetSession) AddCloseSessionFunc(f func(sess Session))

func (*GNetSession) Close

func (sess *GNetSession) Close() (err error)

func (*GNetSession) GetConn

func (sess *GNetSession) GetConn() interface{}

GetConn get raw conn(net.Conn,websocket.Conn...)

func (*GNetSession) GetServer

func (sess *GNetSession) GetServer() Server

GetServer get raw server(*WsServer,*TcpServer...)

func (*GNetSession) SessionValue

func (sess *GNetSession) SessionValue(key interface{}) interface{}

Value wrap context.Context.Value

func (*GNetSession) WithSessionValue

func (sess *GNetSession) WithSessionValue(key, value interface{})

WithValue wrap context.WithValue

func (*GNetSession) Write

func (sess *GNetSession) Write(in []byte) (n int, err error)

type Router

type Router = process.Router

import type

type Server

type Server = network.Server

import type

type ServerOption

type ServerOption func(cc *ServerOptions) ServerOption

ServerOption option define

func WithAcceptLoadLimit

func WithAcceptLoadLimit(v func(sess Session, cnt int64) bool) ServerOption

accepted load limit

func WithAddr

func WithAddr(v string) ServerOption

Addr Server Addr

func WithFrameLogger

func WithFrameLogger(v *zaplog.Logger) ServerOption

frame log

func WithHeartbeat

func WithHeartbeat(v time.Duration) ServerOption

Heartbeat use websocket ping/pong.

func WithLoadBalancing

func WithLoadBalancing(v gnet.LoadBalancing) ServerOption

WithLoadBalancing sets up the load-balancing algorithm in gnet server.

func WithLockOSThread

func WithLockOSThread(v bool) ServerOption

WithLockOSThread sets up LockOSThread mode for I/O event-loops.

func WithMulticore

func WithMulticore(v bool) ServerOption

WithMulticore sets up multi-cores in gnet server.

func WithNetConnOption

func WithNetConnOption(v func(net.Conn)) ServerOption

NetOption modify raw options

func WithNewSession

func WithNewSession(v func(in Session) (Session, error)) ServerOption

NewSession custom session

func WithNumEventLoop

func WithNumEventLoop(v int) ServerOption

WithNumEventLoop sets up NumEventLoop in gnet server.

func WithProcessOptions

func WithProcessOptions(v ...process.ProcessOption) ServerOption

Process Options

func WithReadBufferCap

func WithReadBufferCap(v int) ServerOption

WithReadBufferCap sets up ReadBufferCap for reading bytes.

func WithReusePort

func WithReusePort(v bool) ServerOption

WithReusePort sets up SO_REUSEPORT socket option.

func WithReuseReadBuffer

func WithReuseReadBuffer(v bool) ServerOption

ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter. 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。 默认为false,是为了防止错误的配置导致bug。

func WithRouter

func WithRouter(v Router) ServerOption

process router

func WithSessionLogger

func WithSessionLogger(v func(sess Session, global *zaplog.Logger) (r *zaplog.Logger)) ServerOption

SessionLogger custom session logger

func WithSessionRouter

func WithSessionRouter(v func(sess Session, global Router) (r Router)) ServerOption

SessionRouter custom session router

func WithSocketRecvBuffer

func WithSocketRecvBuffer(v int) ServerOption

WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.

func WithSocketSendBuffer

func WithSocketSendBuffer(v int) ServerOption

WithSocketSendBuffer sets the maximum socket send buffer in bytes.

func WithStopImmediately

func WithStopImmediately(v bool) ServerOption

StopImmediately when session finish,business finish immediately.

func WithTCPKeepAlive

func WithTCPKeepAlive(v time.Duration) ServerOption

WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.

func WithTCPNoDelay

func WithTCPNoDelay(v gnet.TCPSocketOpt) ServerOption

WithTCPNoDelay enable/disable the TCP_NODELAY socket option.

func WithTicker

func WithTicker(v time.Duration) ServerOption

WithTicker indicates that a ticker is set.

type ServerOptions

type ServerOptions struct {
	// Addr Server Addr
	Addr string
	// NetOption modify raw options
	NetConnOption func(net.Conn)
	// accepted load limit
	AcceptLoadLimit func(sess Session, cnt int64) bool
	// Process Options
	ProcessOptions []process.ProcessOption
	// process router
	Router Router
	// SessionRouter custom session router
	SessionRouter func(sess Session, global Router) (r Router)
	// frame log
	FrameLogger *zaplog.Logger
	// SessionLogger custom session logger
	SessionLogger func(sess Session, global *zaplog.Logger) (r *zaplog.Logger)
	// NewSession custom session
	NewSession func(in Session) (Session, error)
	// StopImmediately when session finish,business finish immediately.
	StopImmediately bool
	// Heartbeat use websocket ping/pong.
	Heartbeat time.Duration
	// WithMulticore sets up multi-cores in gnet server.
	Multicore bool
	// WithLockOSThread sets up LockOSThread mode for I/O event-loops.
	LockOSThread bool
	// WithLoadBalancing sets up the load-balancing algorithm in gnet server.
	LoadBalancing gnet.LoadBalancing
	// WithNumEventLoop sets up NumEventLoop in gnet server.
	NumEventLoop int
	// WithReusePort sets up SO_REUSEPORT socket option.
	ReusePort bool
	// WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.
	TCPKeepAlive time.Duration
	// WithTCPNoDelay enable/disable the TCP_NODELAY socket option.
	TCPNoDelay gnet.TCPSocketOpt
	// WithReadBufferCap sets up ReadBufferCap for reading bytes.
	ReadBufferCap int
	// WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.
	SocketRecvBuffer int
	// WithSocketSendBuffer sets the maximum socket send buffer in bytes.
	SocketSendBuffer int
	// WithTicker indicates that a ticker is set.
	Ticker time.Duration
	// ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter.
	// 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。
	// 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。
	// 默认为false,是为了防止错误的配置导致bug。
	ReuseReadBuffer bool
}

ServerOption

func NewServerOptions

func NewServerOptions(opts ...ServerOption) *ServerOptions

NewServerOptions create options instance.

func (*ServerOptions) ApplyOption

func (cc *ServerOptions) ApplyOption(opts ...ServerOption)

ApplyOption modify options

func (*ServerOptions) GetSetOption

func (cc *ServerOptions) GetSetOption(opt ServerOption) ServerOption

GetSetOption modify and get last option

func (*ServerOptions) SetOption

func (cc *ServerOptions) SetOption(opt ServerOption)

SetOption modify options

type Session

type Session = network.Session

import type

type SessionContext

type SessionContext = network.SessionContext

import type

type WriteMethod

type WriteMethod = network.WriteMethod

import type

Jump to

Keyboard shortcuts

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