orbit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed defines the error if a stream is unexpectedly closed.
	ErrClosed = errors.New("closed")

	// ErrInvalidVersion defines the error if the version of both peers do not match
	// during the version exchange.
	ErrInvalidVersion = errors.New("invalid version")
)
View Source
var (
	// ErrNoData defines the error if no data is available.
	ErrNoData = errors.New("no data available to decode")
)

Functions

This section is empty.

Types

type CallFunc

type CallFunc func(ctx context.Context, s *Session, args *Data) (ret interface{}, err error)

type Config

type Config struct {
	Log   *zerolog.Logger
	Codec codec.Codec

	PrintPanicStackTraces bool
	SendErrToCaller       bool

	// InitTimeout specifies the connection initialization timeout.
	InitTimeout time.Duration

	// CallTimeout specifies the default timeout for any Call.
	// This value can be overridden per call in the .orbit file.
	// A value < 0 means no timeout.
	// A value of 0 will take the default call timeout value.
	CallTimeout time.Duration
}

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a config, where all fields are set to their default values.

type Conn

type Conn interface {
	closer.Closer

	// AcceptStream returns the next stream opened by the peer, blocking until one is available.
	AcceptStream(context.Context) (Stream, error)

	// OpenStream opens a new bidirectional stream.
	// There is no signaling to the peer about new streams:
	// The peer can only accept the stream after data has been sent on the stream.
	OpenStream(context.Context) (Stream, error)

	// LocalAddr returns the local address.
	LocalAddr() net.Addr

	// RemoteAddr returns the address of the peer.
	RemoteAddr() net.Addr
}

type Data

type Data struct {

	// Raw is the byte representation of the encoded data.
	Raw []byte
	// contains filtered or unexported fields
}

func (*Data) Decode

func (d *Data) Decode(v interface{}) error

Decode the data to a custom value The value has to be passed as pointer. Returns ErrNoData, if there is no data available to decode.

type Error

type Error interface {
	// Embeds the standard go error interface.
	error

	// Msg returns a textual explanation of the error and should
	// NOT contain sensitive information about the application.
	Msg() string

	// Code returns an integer that can give a hint about the
	// type of error that occurred.
	Code() int
}

An Error offers a way for handler functions of control calls to determine the information passed to the client, in case an error occurs. That way, sensitive information that may be contained in a standard error, can be hidden from the client. Instead, a Msg and a code can be sent back to give a non-sensitive explanation of the error and a code that is easy to check, to allow handling common errors.

func Err

func Err(err error, msg string, code int) Error

Err constructs and returns a type that satisfies the control.Error interface from the given parameters.

type ErrorCode

type ErrorCode struct {
	// The code is used to identify common errors.
	Code int
	// contains filtered or unexported fields
}

The ErrorCode type extends the standard go error by a simple integer code. It is returned in the Call- functions of this package and allows callers that use them to check for common errors via the code.

func (ErrorCode) Error

func (e ErrorCode) Error() string

Implements the error interface.

type Hook

type Hook interface {

	// Called after the session has established a first stream and
	// performed the handshake.
	// If the returned err != nil, the new session is closed immediately.
	OnNewSession(s *Session, stream Stream) error

	// todo:
	// If the returned err != nil, the call is aborted.
	OnCall(s *Session, service, id string) error
	// todo:
	// If err == nil, then the call completed successfully.
	OnCallCompleted(s *Session, service, id string, err error)

	// If the returned err != nil, the stream is aborted.
	OnNewStream(s *Session, service, id string) error
}

Hook allows third-party code to hook into orbit's logic, to implement for example logging or authentication functionality. Hooks that return an error have the capability to abort the action that triggered them. E.g. if OnNewSession() returns a non-nil error, the session will be closed.

type Listener

type Listener interface {
	closer.Closer

	// Accept waits for and returns the next connection to the listener.
	Accept() (Conn, error)

	// Addr returns the listener's network address.
	Addr() net.Addr
}

type Server

type Server struct {
	closer.Closer
	// contains filtered or unexported fields
}

Server implements a simple orbit server.

func NewServer

func NewServer(ln Listener, cf *ServerConfig, h ServerHandler, hs ...Hook) *Server

NewServer creates a new orbit server. A listener is required the server will use to listen for incoming connections. A config can be provided, where every property of it that has not been set will be initialized with a default value. That makes it possible to overwrite only the interesting properties for the caller.

func (*Server) Listen

func (s *Server) Listen() error

Listen listens for new socket connections, which it passes to the new connection channel that is read by the server workers. This method is blocking.

func (*Server) Session

func (s *Server) Session(id string) (sn *Session)

Session obtains a session by its ID. Returns nil if not found.

func (*Server) Sessions

func (s *Server) Sessions() []*Session

Sessions returns a list of all currently connected sessions.

type ServerConfig

type ServerConfig struct {
	*Config

	// The number of goroutines that handle incoming connections on the server.
	// If not set, defaults to defaultNewConnNumberWorkers.
	NewConnNumberWorkers int

	// The size of the channel on which new connections are passed to the
	// server workers.
	// Should not be less than NewConnNumberWorkers.
	// If not set, defaults to defaultNewConnChanSize.
	NewConnChanSize int

	// The size of the channel on which new server sessions are passed into,
	// so that a user of this package can read them from it.
	// Should not be less than NewConnNumberWorkers.
	// If not set, defaults to defaultNewSessionChanSize.
	NewSessionChanSize int
}

type ServerHandler

type ServerHandler interface {
	SessionHandler

	InitServer(s *Server)
}

type Session

type Session struct {
	closer.Closer
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn Conn, cf *Config, h SessionHandler, hs ...Hook) (s *Session, err error)

func (*Session) Call

func (s *Session) Call(ctx context.Context, service, id string, data interface{}) (d *Data, err error)

func (*Session) CallAsync

func (s *Session) CallAsync(ctx context.Context, service, id string, data interface{}) (d *Data, err error)

func (*Session) CallTimeout

func (s *Session) CallTimeout() time.Duration

CallTimeout returns the default timeout for all calls.

func (*Session) Codec

func (s *Session) Codec() codec.Codec

Codec returns the used transmission codec.

func (*Session) DeleteValue

func (s *Session) DeleteValue(k string)

DeleteValue deletes the value for the key k in the values map. If the key does not exist, this is a no-op.

func (*Session) ID

func (s *Session) ID() string

ID returns the session ID.

func (*Session) LocalAddr

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

LocalAddr returns the local network address.

func (*Session) OpenStream

func (s *Session) OpenStream(ctx context.Context, service, id string) (stream Stream, err error)

OpenStream opens a new stream with the given channel ID.

func (*Session) RegisterCall

func (s *Session) RegisterCall(service, id string, f CallFunc)

func (*Session) RegisterStream

func (s *Session) RegisterStream(service, id string, f StreamFunc)

func (*Session) RemoteAddr

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

RemoteAddr returns the remote network address.

func (*Session) SetValue

func (s *Session) SetValue(k string, v interface{})

SetValue saves the value v for the key k in the values map.

func (*Session) Value

func (s *Session) Value(k string) (v interface{})

Value returns the value for the key k from the values map. If the key does not exist, v == nil.

type SessionHandler

type SessionHandler interface {
	InitSession(s *Session)
}

type Stream

type Stream interface {
	net.Conn

	// IsClosed returns true, if the stream has been closed locally or by the remote peer.
	IsClosed() bool
}

type StreamFunc

type StreamFunc func(s *Session, stream net.Conn)

Jump to

Keyboard shortcuts

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