knet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 4, 2019 License: Apache-2.0 Imports: 13 Imported by: 4

README

In memory of 2014~2015

knet is a network frame inspired by apache mina.

Here for examples: https://github.com/k81/knet/tree/master/examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientClosed       = errors.New("client closed")
	ErrClientDisconnected = errors.New("client disconnected")
)
View Source
var (
	ErrSessionClosed = errors.New("session closed")
	ErrPeerDead      = errors.New("peer dead")
	ErrTimeout       = errors.New("timeout")
)
View Source
var ErrClientPoolClosed = errors.New("client manager closed")
View Source
var ErrServerClosed = errors.New("server closed")

Functions

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener that accepts at most n simultaneous connections from the provided Listener.

func SetLogger

func SetLogger(logger Logger)

func TCPListen

func TCPListen(addr string) (ln net.Listener, err error)

Types

type CircuitBreakerClient

type CircuitBreakerClient struct {
	Client
	// contains filtered or unexported fields
}

func NewCircuitBreakerClient

func NewCircuitBreakerClient(client Client, breaker *gobreaker.CircuitBreaker) *CircuitBreakerClient

func (*CircuitBreakerClient) Call

func (c *CircuitBreakerClient) Call(ctx context.Context, req Request) (resp Response, err error)

func (*CircuitBreakerClient) CallWithTimeout

func (c *CircuitBreakerClient) CallWithTimeout(ctx context.Context, req Request, timeout time.Duration) (resp Response, err error)

func (*CircuitBreakerClient) Dial

func (c *CircuitBreakerClient) Dial(addr string) (err error)

func (*CircuitBreakerClient) Send

func (c *CircuitBreakerClient) Send(ctx context.Context, msg Message) (err error)

func (*CircuitBreakerClient) SendWithTimeout

func (c *CircuitBreakerClient) SendWithTimeout(ctx context.Context, msg Message, timeout time.Duration) (err error)

type Client

type Client interface {
	Dial(addr string) error
	Close()
	Disconnect()
	SetProtocol(Protocol)
	SetIoHandler(h IoHandler)
	Call(ctx context.Context, req Request) (Response, error)
	CallWithTimeout(ctx context.Context, req Request, timeout time.Duration) (Response, error)
	Send(ctx context.Context, msg Message) error
	SendWithTimeout(ctx context.Context, msg Message, timeout time.Duration) error
	GetSession() *IoSession
	IsClosed() bool
	IsConnected() bool
}

type ClientBase

type ClientBase struct {
	*IoServiceBase
	sync.Mutex
	// contains filtered or unexported fields
}

func NewClientBase

func NewClientBase(ctx context.Context, dial DialFunc, conf *ClientConfig) *ClientBase

func (*ClientBase) Call

func (c *ClientBase) Call(ctx context.Context, req Request) (Response, error)

func (*ClientBase) CallWithTimeout

func (c *ClientBase) CallWithTimeout(ctx context.Context, req Request, timeout time.Duration) (resp Response, err error)

func (*ClientBase) Close

func (c *ClientBase) Close()

func (*ClientBase) Dial

func (c *ClientBase) Dial(addr string) (err error)

func (*ClientBase) Disconnect

func (c *ClientBase) Disconnect()

func (*ClientBase) GetSession

func (c *ClientBase) GetSession() (session *IoSession)

func (*ClientBase) IsClosed

func (c *ClientBase) IsClosed() (closed bool)

func (*ClientBase) IsConnected

func (c *ClientBase) IsConnected() bool

func (*ClientBase) OnConnected

func (c *ClientBase) OnConnected(session *IoSession) error

func (*ClientBase) OnDisconnected

func (c *ClientBase) OnDisconnected(session *IoSession)

func (*ClientBase) OnError

func (c *ClientBase) OnError(session *IoSession, err error)

func (*ClientBase) OnIdle

func (c *ClientBase) OnIdle(session *IoSession) error

func (*ClientBase) OnMessage

func (c *ClientBase) OnMessage(session *IoSession, msg Message) error

func (*ClientBase) Send

func (c *ClientBase) Send(ctx context.Context, msg Message) error

func (*ClientBase) SendWithTimeout

func (c *ClientBase) SendWithTimeout(ctx context.Context, msg Message, timeout time.Duration) (err error)

func (*ClientBase) SetIoHandler

func (c *ClientBase) SetIoHandler(h IoHandler)

type ClientConfig

type ClientConfig struct {
	Io struct {
		SendQueueSize int
		RecvQueueSize int
		ReadTimeout   time.Duration
		WriteTimeout  time.Duration
	}
	AutoReconnect bool
}

func NewClientConfig

func NewClientConfig() *ClientConfig

type ClientFactory

type ClientFactory interface {
	NewClient() (Client, error)
}

type ClientPool

type ClientPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewClientPool

func NewClientPool(ctx context.Context, factory ClientFactory, conf ClientPoolConfig) *ClientPool

func (*ClientPool) Close

func (p *ClientPool) Close()

func (*ClientPool) Get

func (p *ClientPool) Get() Client

func (*ClientPool) Open

func (p *ClientPool) Open() error

type ClientPoolConfig

type ClientPoolConfig struct {
	IdleMin int `json:"idle_min"`
	IdleMax int `json:"idle_max"`
	Max     int `json:"max"`
}

type Conn

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

func (*Conn) GetReadBytes

func (c *Conn) GetReadBytes() uint32

func (*Conn) GetWriteBytes

func (c *Conn) GetWriteBytes() uint32

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

func (*Conn) SetReadTimeout

func (c *Conn) SetReadTimeout(d time.Duration) (err error)

func (*Conn) SetTimeout

func (c *Conn) SetTimeout(d time.Duration)

func (*Conn) SetWriteTimeout

func (c *Conn) SetWriteTimeout(d time.Duration) (err error)

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

type DialFunc

type DialFunc func(addr string) (net.Conn, error)

func TCPDialFunc

func TCPDialFunc(timeout time.Duration) DialFunc

type IoConfig

type IoConfig struct {
	SendQueueSize int
	RecvQueueSize int
	ReadTimeout   time.Duration
	WriteTimeout  time.Duration
}

type IoHandler

type IoHandler interface {
	OnConnected(*IoSession) error
	OnDisconnected(*IoSession)
	OnIdle(*IoSession) error
	OnError(*IoSession, error)
	OnMessage(*IoSession, Message) error
}

type IoHandlerAdapter

type IoHandlerAdapter struct {
}

func (*IoHandlerAdapter) OnConnected

func (h *IoHandlerAdapter) OnConnected(*IoSession) error

func (*IoHandlerAdapter) OnDisconnected

func (h *IoHandlerAdapter) OnDisconnected(*IoSession)

func (*IoHandlerAdapter) OnError

func (h *IoHandlerAdapter) OnError(*IoSession, error)

func (*IoHandlerAdapter) OnIdle

func (h *IoHandlerAdapter) OnIdle(*IoSession) error

func (*IoHandlerAdapter) OnMessage

func (h *IoHandlerAdapter) OnMessage(*IoSession, Message) error

type IoService

type IoService interface {
	Protocol() Protocol
	IoHandler() IoHandler
	IoConfig() *IoConfig
	AddRef()
	DecRef()
	NextSessionId() uint64
}

type IoServiceBase

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

func NewIoServiceBase

func NewIoServiceBase(conf *IoConfig) *IoServiceBase

func (*IoServiceBase) AddRef

func (srv *IoServiceBase) AddRef()

func (*IoServiceBase) DecRef

func (srv *IoServiceBase) DecRef()

func (*IoServiceBase) IoConfig

func (srv *IoServiceBase) IoConfig() *IoConfig

func (*IoServiceBase) IoHandler

func (srv *IoServiceBase) IoHandler() IoHandler

func (*IoServiceBase) NextSessionId

func (srv *IoServiceBase) NextSessionId() uint64

func (*IoServiceBase) Protocol

func (srv *IoServiceBase) Protocol() Protocol

func (*IoServiceBase) SetIoHandler

func (srv *IoServiceBase) SetIoHandler(h IoHandler)

func (*IoServiceBase) SetProtocol

func (srv *IoServiceBase) SetProtocol(p Protocol)

type IoSession

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

func NewIoSession

func NewIoSession(ctx context.Context, srv IoService, conn net.Conn) *IoSession

func (*IoSession) Close

func (s *IoSession) Close()

func (*IoSession) Context

func (s *IoSession) Context() context.Context

func (*IoSession) GetAttr

func (s *IoSession) GetAttr(key interface{}) (v interface{})

func (*IoSession) GetIdleCount

func (s *IoSession) GetIdleCount() uint32

func (*IoSession) Id

func (s *IoSession) Id() uint64

func (*IoSession) IoService

func (s *IoSession) IoService() IoService

func (*IoSession) IsClosed

func (s *IoSession) IsClosed() bool

func (*IoSession) IsConnected

func (s *IoSession) IsConnected() bool

func (*IoSession) LocalAddr

func (s *IoSession) LocalAddr() net.Addr

func (*IoSession) Open

func (s *IoSession) Open()

func (*IoSession) RemoteAddr

func (s *IoSession) RemoteAddr() net.Addr

func (*IoSession) RemoveAttr

func (s *IoSession) RemoveAttr(key string)

func (*IoSession) Send

func (s *IoSession) Send(ctx context.Context, m Message) error

func (*IoSession) SendWithTimeout

func (s *IoSession) SendWithTimeout(ctx context.Context, m Message, timeout time.Duration) error

func (*IoSession) SetAttr

func (s *IoSession) SetAttr(key, value interface{})

func (*IoSession) String

func (s *IoSession) String() string

type ListenFunc

type ListenFunc func(addr string) (net.Listener, error)

type Logger

type Logger interface {
	Printf(fmt string, args ...interface{})
}

type Message

type Message interface{}

type Protocol

type Protocol interface {
	ProtocolEncoder
	ProtocolDecoder
}

type ProtocolDecoder

type ProtocolDecoder interface {
	// if not full message is read, should return (nil, nil)
	Decode(*IoSession, io.Reader) (Message, error)
}

type ProtocolEncoder

type ProtocolEncoder interface {
	Encode(*IoSession, Message) ([]byte, error)
}

type Request

type Request interface {
	Message
	Id() uint64
}

type Response

type Response interface {
	Message
	Id() uint64
}

type Server

type Server interface {
	ListenAndServe(addr string) error
	Serve(ln net.Listener) error
	Close()
}

type ServerBase

type ServerBase struct {
	*IoServiceBase
	// contains filtered or unexported fields
}

func NewServerBase

func NewServerBase(ctx context.Context, listen ListenFunc, conf *ServerConfig) *ServerBase

func (*ServerBase) AddRef

func (srv *ServerBase) AddRef()

func (*ServerBase) Close

func (srv *ServerBase) Close()

func (*ServerBase) DecRef

func (srv *ServerBase) DecRef()

func (*ServerBase) ListenAndServe

func (srv *ServerBase) ListenAndServe(addr string) error

func (*ServerBase) Serve

func (srv *ServerBase) Serve(l net.Listener) error

type ServerConfig

type ServerConfig struct {
	Io struct {
		SendQueueSize int
		RecvQueueSize int
		ReadTimeout   time.Duration
		WriteTimeout  time.Duration
	}
	MaxConnection int
}

func NewServerConfig

func NewServerConfig() *ServerConfig

type TCPClient

type TCPClient struct {
	*ClientBase
}

func NewTCPClient

func NewTCPClient(ctx context.Context, conf *TCPClientConfig) *TCPClient

type TCPClientConfig

type TCPClientConfig struct {
	Io struct {
		SendQueueSize int
		RecvQueueSize int
		ReadTimeout   time.Duration
		WriteTimeout  time.Duration
	}
	DialTimeout   time.Duration
	AutoReconnect bool
}

func NewTCPClientConfig

func NewTCPClientConfig() *TCPClientConfig

type TCPListener

type TCPListener struct {
	*net.TCPListener
}

func (TCPListener) Accept

func (ln TCPListener) Accept() (c net.Conn, err error)

type TCPServer

type TCPServer struct {
	*ServerBase
}

func NewTCPServer

func NewTCPServer(ctx context.Context, conf *TCPServerConfig) *TCPServer

type TCPServerConfig

type TCPServerConfig ServerConfig

func NewTCPServerConfig

func NewTCPServerConfig() *TCPServerConfig

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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