core

package
v1.9.3-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClientLogPrefix     = "\033[36m[core:client]\033[0m "
	ServerLogPrefix     = "\033[32m[core:server]\033[0m "
	ParseFrameLogPrefix = "\033[36m[core:stream_parser]\033[0m "
)

Prefix is the prefix for logger.

View Source
const (
	// DefaultListenAddr is the default address to listen.
	DefaultListenAddr = "0.0.0.0:9000"
)

Variables

View Source
var DefalutQuicConfig = &quic.Config{
	Versions:                       []quic.VersionNumber{quic.Version1, quic.VersionDraft29},
	MaxIdleTimeout:                 time.Second * 5,
	KeepAlivePeriod:                time.Second * 2,
	MaxIncomingStreams:             1000,
	MaxIncomingUniStreams:          1000,
	HandshakeIdleTimeout:           time.Second * 3,
	InitialStreamReceiveWindow:     1024 * 1024 * 2,
	InitialConnectionReceiveWindow: 1024 * 1024 * 2,
}

DefalutQuicConfig be used when `quicConfig` is nil.

Functions

func GetConnID

func GetConnID(conn quic.Connection) string

GetConnID get quic connection id

func ParseFrame

func ParseFrame(stream io.Reader) (frame.Frame, error)

ParseFrame parses the frame from QUIC stream.

Types

type AsyncHandler

type AsyncHandler func([]byte) (byte, []byte)

AsyncHandler is the request-response mode (asnyc)

type Client

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

Client is the abstraction of a YoMo-Client. a YoMo-Client can be Source, Upstream Zipper or StreamFunction.

func NewClient

func NewClient(appName string, connType ClientType, opts ...ClientOption) *Client

NewClient creates a new YoMo-Client.

func (*Client) ClientID added in v1.8.0

func (c *Client) ClientID() string

ClientID return the client ID

func (*Client) Close

func (c *Client) Close() error

Close the client.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, addr string) error

Connect connects to YoMo-Zipper.

func (*Client) Init

func (c *Client) Init(opts ...ClientOption) error

Init the options.

func (*Client) Logger added in v1.6.0

func (c *Client) Logger() log.Logger

Logger get client's logger instance, you can customize this using `yomo.WithLogger`

func (*Client) ServerAddr

func (c *Client) ServerAddr() string

ServerAddr returns the address of the server.

func (*Client) SetBackflowFrameObserver added in v1.8.0

func (c *Client) SetBackflowFrameObserver(fn func(*frame.BackflowFrame))

SetBackflowFrameObserver sets the backflow frame handler.

func (*Client) SetCloseHandler added in v1.9.1

func (c *Client) SetCloseHandler(fn func())

SetCloseHandler set close handler

func (*Client) SetDataFrameObserver

func (c *Client) SetDataFrameObserver(fn func(*frame.DataFrame))

SetDataFrameObserver sets the data frame handler.

func (*Client) SetErrorHandler added in v1.7.3

func (c *Client) SetErrorHandler(fn func(err error))

SetErrorHandler set error handler

func (*Client) SetObserveDataTags added in v1.6.0

func (c *Client) SetObserveDataTags(tag ...byte)

SetObserveDataTags set the data tag list that will be observed. Deprecated: use yomo.WithObserveDataTags instead

func (*Client) WriteFrame

func (c *Client) WriteFrame(frm frame.Frame) error

WriteFrame writes a frame to the connection, gurantee threadsafe.

type ClientOption

type ClientOption func(*ClientOptions)

ClientOption YoMo client options

func WithClientQuicConfig

func WithClientQuicConfig(qc *quic.Config) ClientOption

WithClientQuicConfig sets quic config for the client.

func WithClientTLSConfig

func WithClientTLSConfig(tc *tls.Config) ClientOption

WithClientTLSConfig sets tls config for the client.

func WithCredential

func WithCredential(payload string) ClientOption

WithCredential sets the client credential method (used by client).

func WithLogger added in v1.6.0

func WithLogger(logger log.Logger) ClientOption

WithLogger sets logger for the client.

func WithObserveDataTags added in v1.6.0

func WithObserveDataTags(tags ...byte) ClientOption

WithObserveDataTags sets data tag list for the client.

type ClientOptions

type ClientOptions struct {
	ObserveDataTags []byte
	QuicConfig      *quic.Config
	TLSConfig       *tls.Config
	Credential      *auth.Credential
	Logger          log.Logger
}

ClientOptions are the options for YoMo client.

type ClientType

type ClientType byte

ClientType represents the connection type.

const (
	// ClientTypeNone is connection type "None".
	ClientTypeNone ClientType = 0xFF
	// ClientTypeSource is connection type "Source".
	ClientTypeSource ClientType = 0x5F
	// ClientTypeUpstreamZipper is connection type "Upstream Zipper".
	ClientTypeUpstreamZipper ClientType = 0x5E
	// ClientTypeStreamFunction is connection type "Stream Function".
	ClientTypeStreamFunction ClientType = 0x5D
)

func (ClientType) String

func (c ClientType) String() string

type ConnState

type ConnState = string

ConnState represents the state of the connection.

const (
	ConnStateReady        ConnState = "Ready"
	ConnStateDisconnected ConnState = "Disconnected"
	ConnStateConnecting   ConnState = "Connecting"
	ConnStateConnected    ConnState = "Connected"
	ConnStateClosed       ConnState = "Closed"
)

ConnState represents the state of a connection.

type Connection added in v1.7.4

type Connection interface {
	io.Closer

	// Name returns the name of the connection, which is set by clients
	Name() string
	// ClientID connection client ID
	ClientID() string
	// ClientType returns the type of the client (Source | SFN | UpstreamZipper)
	ClientType() ClientType
	// Metadata returns the extra info of the application
	Metadata() Metadata
	// Write should goroutine-safely send y3 frames to peer side
	Write(f frame.Frame) error
	// ObserveDataTags observed data tags
	ObserveDataTags() []byte
}

Connection wraps the specific io connections (typically quic.Connection) to transfer y3 frames

type ConnectionHandler added in v1.9.0

type ConnectionHandler func(conn quic.Connection)

ConnectionHandler is the handler for quic connection

type Connector

type Connector interface {
	// Add a connection.
	Add(connID string, conn Connection)
	// Remove a connection.
	Remove(connID string)
	// Get a connection by connection id.
	Get(connID string) Connection
	// GetSnapshot gets the snapshot of all connections.
	GetSnapshot() map[string]string
	// GetSourceConns gets the connections by source observe tags.
	GetSourceConns(sourceID string, tags byte) []Connection
	// Clean the connector.
	Clean()
}

Connector is a interface to manage the connections and applications.

type Context

type Context struct {
	// Conn is the connection of client.
	Conn quic.Connection

	// Stream is the long-lived connection between client and server.
	Stream io.ReadWriteCloser
	// Frame receives from client.
	Frame frame.Frame
	// Keys store the key/value pairs in context.
	Keys map[string]interface{}
	// contains filtered or unexported fields
}

Context for YoMo Server.

func (*Context) Clean

func (c *Context) Clean()

Clean the context.

func (*Context) CloseWithError

func (c *Context) CloseWithError(code yerr.ErrorCode, msg string)

CloseWithError closes the stream and cleans the context.

func (*Context) ConnID

func (c *Context) ConnID() string

ConnID get quic connection id

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, exists bool)

Get the value by a specified key.

func (*Context) GetBool

func (c *Context) GetBool(key string) (b bool)

GetBool gets a bool value by a specified key.

func (*Context) GetDuration

func (c *Context) GetDuration(key string) (d time.Duration)

GetDuration gets a time.Duration value by a specified key.

func (*Context) GetFloat64

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 gets a float64 value by a specified key.

func (*Context) GetInt

func (c *Context) GetInt(key string) (i int)

GetInt gets an int value by a specified key.

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 gets an int64 value by a specified key.

func (*Context) GetString

func (c *Context) GetString(key string) (s string)

GetString gets a string value by a specified key.

func (*Context) GetStringMap

func (c *Context) GetStringMap(key string) (sm map[string]interface{})

GetStringMap gets a map[string]interface{} value by a specified key.

func (*Context) GetStringMapString

func (c *Context) GetStringMapString(key string) (sms map[string]string)

GetStringMapString gets a map[string]string value by a specified key.

func (*Context) GetStringMapStringSlice

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice gets a map[string][]string value by a specified key.

func (*Context) GetStringSlice

func (c *Context) GetStringSlice(key string) (ss []string)

GetStringSlice gets a []string value by a specified key.

func (*Context) GetTime

func (c *Context) GetTime(key string) (t time.Time)

GetTime gets a time.Time value by a specified key.

func (*Context) GetUint

func (c *Context) GetUint(key string) (ui uint)

GetUint gets an uint value by a specified key.

func (*Context) GetUint64

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 gets an uint64 value by a specified key.

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set a key/value pair to context.

func (*Context) WithFrame

func (c *Context) WithFrame(f frame.Frame) *Context

WithFrame sets a frame to context.

type FrameHandler

type FrameHandler func(c *Context) error

FrameHandler is the handler for frame.

type FrameStream

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

FrameStream is the QUIC Stream with the minimum unit Frame.

func NewFrameStream

func NewFrameStream(s io.ReadWriter) *FrameStream

NewFrameStream creates a new FrameStream.

func (*FrameStream) ReadFrame

func (fs *FrameStream) ReadFrame() (frame.Frame, error)

ReadFrame reads next frame from QUIC stream.

func (*FrameStream) WriteFrame

func (fs *FrameStream) WriteFrame(f frame.Frame) (int, error)

WriteFrame writes a frame into QUIC stream.

type Listener

type Listener interface {
	quic.Listener
	// Name listerner's name
	Name() string
	// Versions
	Versions() []string
}

A Listener for incoming connections

type Metadata added in v1.7.4

type Metadata interface {
	// Encode is the serialize method
	Encode() []byte
}

Metadata is used for storing extra info of the application

type MetadataBuilder added in v1.7.4

type MetadataBuilder interface {
	// Build will return an Metadata instance according to the handshake frame passed in
	Build(f *frame.HandshakeFrame) (Metadata, error)
	// Decode is the deserialize method
	Decode(buf []byte) (Metadata, error)
}

MetadataBuilder is the builder of Metadata

type PipeHandler

type PipeHandler func(in <-chan []byte, out chan<- *frame.PayloadFrame)

PipeHandler is the bidirectional stream mode (blocking).

type Route

type Route interface {
	// Add a route.
	Add(connID string, name string, observeDataTags []byte) error
	// Remove a route.
	Remove(connID string) error
	// GetForwardRoutes returns all the subscribers by the given data tag.
	GetForwardRoutes(tag byte) []string
}

Route manages data subscribers according to their observed data tags.

type Router

type Router interface {
	// Route gets the route
	Route(metadata Metadata) Route
	// Clean the routes.
	Clean()
}

Router is the interface to manage the routes for applications.

type Server

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

Server is the underlining server of Zipper

func NewServer

func NewServer(name string, opts ...ServerOption) *Server

NewServer create a Server instance.

func (*Server) AddDownstreamServer

func (s *Server) AddDownstreamServer(addr string, c *Client)

AddDownstreamServer add a downstream server to this server. all the DataFrames will be dispatch to all the downstreams.

func (*Server) Close

func (s *Server) Close() error

Close will shutdown the server.

func (*Server) ConfigMetadataBuilder added in v1.7.4

func (s *Server) ConfigMetadataBuilder(builder MetadataBuilder)

ConfigMetadataBuilder is used to set metadataBuilder by zipper

func (*Server) ConfigRouter

func (s *Server) ConfigRouter(router Router)

ConfigRouter is used to set router by zipper

func (*Server) Connector

func (s *Server) Connector() Connector

Connector returns the connector of server.

func (*Server) Downstreams

func (s *Server) Downstreams() map[string]*Client

Downstreams return all the downstream servers.

func (*Server) Init

func (s *Server) Init(opts ...ServerOption) error

Init the options.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe starts the server.

func (*Server) Options

func (s *Server) Options() ServerOptions

Options returns the options of server.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, conn net.PacketConn) error

Serve the server with a net.PacketConn.

func (*Server) SetAfterHandlers

func (s *Server) SetAfterHandlers(handlers ...FrameHandler)

SetAfterHandlers set the after handlers of server.

func (*Server) SetBeforeHandlers

func (s *Server) SetBeforeHandlers(handlers ...FrameHandler)

SetBeforeHandlers set the before handlers of server.

func (*Server) SetConnectionCloseHandlers added in v1.9.0

func (s *Server) SetConnectionCloseHandlers(handlers ...ConnectionHandler)

SetConnectionCloseHandlers set the connection close handlers of server.

func (*Server) StatsCounter

func (s *Server) StatsCounter() int64

StatsCounter returns how many DataFrames pass through server.

func (*Server) StatsFunctions

func (s *Server) StatsFunctions() map[string]string

StatsFunctions returns the sfn stats of server.

type ServerOption

type ServerOption func(*ServerOptions)

ServerOption is the option for server.

func WithAddr

func WithAddr(addr string) ServerOption

WithAddr sets the server address.

func WithAuth

func WithAuth(name string, args ...string) ServerOption

WithAuth sets the server authentication method.

func WithConn

func WithConn(conn net.PacketConn) ServerOption

WithConn sets the connection for the server.

func WithServerQuicConfig

func WithServerQuicConfig(qc *quic.Config) ServerOption

WithServerQuicConfig sets the QUIC configuration for the server.

func WithServerTLSConfig

func WithServerTLSConfig(tc *tls.Config) ServerOption

WithServerTLSConfig sets the TLS configuration for the server.

type ServerOptions

type ServerOptions struct {
	QuicConfig *quic.Config
	TLSConfig  *tls.Config
	Addr       string
	Auths      []auth.Authentication
	Conn       net.PacketConn
}

ServerOptions are the options for YoMo server.

type Workflow

type Workflow struct {
	// Seq represents the sequence id when executing workflows.
	Seq int

	// Token represents the name of workflow.
	Name string
}

Workflow describes stream function workflows.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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