server

package
v0.0.0-...-a3eedf2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	ID      uint64
	Conn    net.Conn
	Server  ZServer
	Context map[string]interface{}
	// contains filtered or unexported fields
}

func (*Connection) CallPostStart

func (c *Connection) CallPostStart()

func (*Connection) CallPreStop

func (c *Connection) CallPreStop()

func (*Connection) Close

func (c *Connection) Close()

Clenup current connection before exit

func (*Connection) DeleteContext

func (c *Connection) DeleteContext(key string)

func (*Connection) GetContext

func (c *Connection) GetContext(key string) interface{}

func (*Connection) GetID

func (c *Connection) GetID() uint64

func (*Connection) GetServer

func (c *Connection) GetServer() ZServer

func (*Connection) Reader

func (c *Connection) Reader()

Read from the TCP stream payload and decode the raw bytes to struct Prepare a processed request and send it to a worker to handle it

func (*Connection) RespondToClient

func (c *Connection) RespondToClient(ct encoding.ZContentType, data []byte) error

Called by handler after dealing with the request Send the raw bytes to Writer

func (*Connection) Start

func (c *Connection) Start()

Seperate read/write thread, leave the handling part to ZMux

func (*Connection) UpdateContext

func (c *Connection) UpdateContext(key string, value interface{})

func (*Connection) Writer

func (c *Connection) Writer()

Write the processed response (raw bytes received from handler) to client Quit on receiving the close signal after Reader quits

type ConnectionAdmin

type ConnectionAdmin struct {
	// A vault of connections
	Pool  map[uint64]ZConnection
	Mutex sync.RWMutex
}

func (*ConnectionAdmin) Evacuate

func (ca *ConnectionAdmin) Evacuate()

Called when daemon is shut down, closing all active connections to ensure a clean quit

func (*ConnectionAdmin) PoolSize

func (ca *ConnectionAdmin) PoolSize() int

func (*ConnectionAdmin) Register

func (ca *ConnectionAdmin) Register(conn ZConnection)

Add new connection to the vault

func (*ConnectionAdmin) Remove

func (ca *ConnectionAdmin) Remove(conn ZConnection)

Delete the connection from the vault

func (*ConnectionAdmin) Retrieve

func (ca *ConnectionAdmin) Retrieve(cid uint64) ZConnection

Get the connection via its id

func (*ConnectionAdmin) RetrieveAll

func (ca *ConnectionAdmin) RetrieveAll() []ZConnection

type Handler

type Handler struct{}

Define the behavior of server in response to client's request Need to be implemented at server side and registerd to ZJunx server's multiplexer (ZMux) after server is up

func (*Handler) Handle

func (h *Handler) Handle(req ZRequest)

type Mux

type Mux struct {
	// workers' number
	WorkerProcesses uint64
	// request queues for each worker
	WorkerBacklog []chan ZRequest
	// channel attached to each worker to receive quit signal
	//WorkerExit	[]chan bool
	// the cancel function linked to ZMux's context
	WorkerExit  context.CancelFunc
	WorkerGroup sync.WaitGroup
	// A bunch of registered handler to handle request
	HandlerSet map[encoding.ZContentType]ZHandler
	// legitime value: RoundRobin, Random, LeastConn
	ScheduleAlgo string
}

func (*Mux) Handle

func (m *Mux) Handle(req ZRequest)

Handle client's request if related handler is registered

func (*Mux) Register

func (m *Mux) Register(ct encoding.ZContentType, h ZHandler)

Register the serverside defined handler to ZMux

func (*Mux) Schedule

func (m *Mux) Schedule(req ZRequest)

Scheduling the request to appropriate worker depending on the algorithm

func (*Mux) WorkerDismiss

func (m *Mux) WorkerDismiss()

type Request

type Request struct {
	Conn ZConnection
	Cont *encoding.Content
}

Group the content decoded sent by client and associate ZConnection

func (*Request) Connection

func (r *Request) Connection() ZConnection

func (*Request) ContentData

func (r *Request) ContentData() []byte

func (*Request) ContentType

func (r *Request) ContentType() encoding.ZContentType

type Server

type Server struct {
	Name       string
	ListenIP   string
	IPVersion  string
	ListenPort uint64

	Mux    ZMux
	CnxAdm ZConnectionAdmin

	PostStartHook func(ZConnection)
	PreStopHook   func(ZConnection)
	// contains filtered or unexported fields
}

func (*Server) GetCnxAdm

func (s *Server) GetCnxAdm() ZConnectionAdmin

func (*Server) GetMux

func (s *Server) GetMux() ZMux

func (*Server) GetPostStartHook

func (s *Server) GetPostStartHook() func(ZConnection)

func (*Server) GetPreStopHook

func (s *Server) GetPreStopHook() func(ZConnection)

func (*Server) SetInterruptHandler

func (s *Server) SetInterruptHandler()

func (*Server) Start

func (s *Server) Start()

Lauch a ZJunx server

func (*Server) Stop

func (s *Server) Stop()

type ServerOption

type ServerOption func(*Server)

func WithHandler

func WithHandler(ct encoding.ZContentType, h ZHandler) ServerOption

func WithPostStart

func WithPostStart(hook func(ZConnection)) ServerOption

func WithPreStop

func WithPreStop(hook func(ZConnection)) ServerOption

type ZConnection

type ZConnection interface {
	Start()
	Reader()
	Writer()
	Close()
	RespondToClient(encoding.ZContentType, []byte) error
	GetID() uint64
	GetServer() ZServer
	UpdateContext(string, interface{})
	GetContext(string) interface{}
	DeleteContext(string)
	CallPostStart()
	CallPreStop()
}

func ConnInit

func ConnInit(cnxID uint64, conn net.Conn, s ZServer) ZConnection

type ZConnectionAdmin

type ZConnectionAdmin interface {
	Register(ZConnection)
	Retrieve(uint64) ZConnection
	RetrieveAll() []ZConnection
	Remove(ZConnection)
	Evacuate()
	PoolSize() int
}

func AdmInit

func AdmInit() ZConnectionAdmin

type ZHandler

type ZHandler interface {
	Handle(ZRequest)
}

type ZMux

type ZMux interface {
	Register(encoding.ZContentType, ZHandler)
	Schedule(ZRequest)
	Handle(ZRequest)
	WorkerDismiss()
}

A multiplexer dealing with client's requests

func MuxInit

func MuxInit() ZMux

type ZRequest

type ZRequest interface {
	ContentType() encoding.ZContentType
	ContentData() []byte
	Connection() ZConnection
}

func ReqInit

func ReqInit(conn ZConnection, cont *encoding.Content) ZRequest

type ZServer

type ZServer interface {
	Start()
	Stop()

	GetMux() ZMux
	GetCnxAdm() ZConnectionAdmin
	GetPostStartHook() func(ZConnection)
	GetPreStopHook() func(ZConnection)

	SetInterruptHandler()
}

func NewServer

func NewServer(opts ...ServerOption) ZServer

Jump to

Keyboard shortcuts

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