protocol

package
v0.0.0-...-8a4113d Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TYPICAL_MTU       = 1500
	MAX_OUT_QUEUE_LEN = 1 << 20 // 16MB of pointers is a lot

	MAX_RETRY_PERIOD = time.Minute
	MIN_RETRY_PERIOD = time.Second / 2
)
View Source
const CaseBit uint8 = 'a' - 'A'

Variables

View Source
var (
	ErrAddressInvalid    = errors.New("the address invalid")
	ErrAddressDuplicated = errors.New("the address already used")

	ErrIncomplete     = errors.New("incomplete data")
	ErrBadRecord      = errors.New("bad TLV record format")
	ErrAddressUnknown = errors.New("address unknown")
	ErrDisconnected   = errors.New("disconnected by user")
)

Functions

func Append

func Append(into []byte, lit byte, body ...[]byte) (res []byte)

Append appends a record to the buffer; note that uppercase type is always explicit, lowercase can be defaulted.

func AppendHeader

func AppendHeader(into []byte, lit byte, bodylen int) (ret []byte)

Feeds the header into the buffer. Subtle: lower-case lit allows for defaulting, uppercase must be explicit.

func AppendTiny

func AppendTiny(into []byte, lit byte, body []byte) (res []byte)

func CloseHeader

func CloseHeader(buf []byte, bookmark int)

CloseHeader closes a streamed TLV record

func Concat

func Concat(msg ...[]byte) []byte

func Incomplete

func Incomplete(data []byte) int

Incomplete returns the number of supposedly yet-unread bytes. 0 for complete, -1 for bad format, >0 for least-necessary read to complete either header or record.

func Join

func Join(records ...[]byte) (ret []byte)

func Lit

func Lit(rec []byte) byte

func OpenHeader

func OpenHeader(buf []byte, lit byte) (bookmark int, res []byte)

OpenHeader opens a streamed TLV record; use append() to create the record body, then call CloseHeader(&buf, bookmark)

func ProbeHeader

func ProbeHeader(data []byte) (lit byte, hdrlen, bodylen int)

ProbeHeader probes a TLV record header. Return values:

  • 0 0 0 incomplete header
  • '-' 0 0 bad format
  • 'A' 2 123 success

func ProbeHeaders

func ProbeHeaders(lits string, data []byte) int

func Pump

func Pump(feeder Feeder, drainer Drainer) (err error)

func PumpCtx

func PumpCtx(ctx context.Context, feeder Feeder, drainer Drainer) (err error)

func PumpCtxCallback

func PumpCtxCallback(ctx context.Context, feeder Feeder, drainer Drainer, f func() bool) (err error)

func PumpN

func PumpN(feeder Feeder, drainer Drainer, n int) (err error)

func PumpThenClose

func PumpThenClose(feed FeedCloser, drain DrainCloser) error

func Record

func Record(lit byte, body ...[]byte) []byte

Record composes a record of a given type

func Relay

func Relay(feeder Feeder, drainer Drainer) error

func Take

func Take(lit byte, data []byte) (body, rest []byte)

Take is used to read safe TLV inputs (e.g. from own storage) with record types known in advance.

func TakeAny

func TakeAny(data []byte) (lit byte, body, rest []byte)

TakeAny is used for safe TLV inputs when record types can vary.

func TakeAnyRecord

func TakeAnyRecord(data []byte) (lit byte, rec, rest []byte)

func TakeAnyWary

func TakeAnyWary(data []byte) (lit byte, body, rest []byte, err error)

TakeWary reads TLV records of arbitrary type from unsafe input.

func TakeRecord

func TakeRecord(lit byte, data []byte) (rec, rest []byte)

func TakeWary

func TakeWary(lit byte, data []byte) (body, rest []byte, err error)

TakeWary reads TLV records of known type from unsafe input.

func TinyRecord

func TinyRecord(lit byte, body []byte) (tiny []byte)

func TotalLen

func TotalLen(inputs [][]byte) (sum int)

Types

type ConnType

type ConnType = uint
const (
	TCP ConnType = iota + 1
	TLS
	QUIC
)

type DestroyCallback

type DestroyCallback func(name string, p Traced)

type DrainCloser

type DrainCloser interface {
	Drainer
	io.Closer
}

type Drainer

type Drainer interface {
	Drain(ctx context.Context, recs Records) error
}

type FeedCloser

type FeedCloser interface {
	Feeder
	io.Closer
}

type FeedDrainCloser

type FeedDrainCloser interface {
	Feeder
	Drainer
	io.Closer
}

type FeedDrainCloserTraced

type FeedDrainCloserTraced interface {
	FeedDrainCloser
	Traced
}

type Feeder

type Feeder interface {
	// Feed reads and returns records.
	// The EoF convention follows that of io.Reader:
	// can either return `records, EoF` or
	// `records, nil` followed by `nil/{}, EoF`
	Feed(ctx context.Context) (recs Records, err error)
}

type InstallCallback

type InstallCallback func(name string) FeedDrainCloserTraced

type Net

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

A TCP/TLS/QUIC server/client for the use case of real-time async communication. Differently from the case of request-response (like HTTP), we do not wait for a request, then dedicating a thread to processing, then sending back the resulting response. Instead, we constantly fan sendQueue tons of tiny messages. That dictates different work patterns than your typical HTTP/RPC server as, for example, we cannot let one slow receiver delay event transmission to all the other receivers.

func NewNet

func NewNet(log utils.Logger, install InstallCallback, destroy DestroyCallback, opts ...NetOpt) *Net

func (*Net) Close

func (n *Net) Close() error

func (*Net) Connect

func (n *Net) Connect(ctx context.Context, addr string) (err error)

func (*Net) ConnectPool

func (n *Net) ConnectPool(ctx context.Context, name string, addrs []string) (err error)

func (*Net) Disconnect

func (de *Net) Disconnect(name string) (err error)

func (*Net) GetStats

func (n *Net) GetStats() NetStats

func (*Net) KeepConnecting

func (n *Net) KeepConnecting(ctx context.Context, name string, addrs []string)

func (*Net) KeepListening

func (n *Net) KeepListening(ctx context.Context, addr string)

func (*Net) Listen

func (n *Net) Listen(ctx context.Context, addr string) error

func (*Net) Unlisten

func (de *Net) Unlisten(addr string) error

type NetOpt

type NetOpt interface {
	Apply(*Net)
}

type NetReadBatchOpt

type NetReadBatchOpt struct {
	ReadAccumTimeLimit time.Duration
	BufferMaxSize      int
	BufferMinToProcess int
}

func (*NetReadBatchOpt) Apply

func (opt *NetReadBatchOpt) Apply(n *Net)

type NetStats

type NetStats struct {
	ReadBuffers map[string]int32
}

type NetTlsConfigOpt

type NetTlsConfigOpt struct {
	Config *tls.Config
}

func (*NetTlsConfigOpt) Apply

func (opt *NetTlsConfigOpt) Apply(n *Net)

type Peer

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

func (*Peer) Close

func (p *Peer) Close()

func (*Peer) GetIncomingPacketBufferSize

func (p *Peer) GetIncomingPacketBufferSize() int32

func (*Peer) GetTraceId

func (p *Peer) GetTraceId() string

func (*Peer) Keep

func (p *Peer) Keep(ctx context.Context) (rerr, werr, cerr error)

type Records

type Records [][]byte

Records (a batch of) as a very universal primitive, especially for database/network op/packet processing. Batching allows for writev() and other performance optimizations. Also, if you have cryptography, blobs are way handier than structs. Records converts easily to net.Buffers.

func Recs

func Recs(lit byte, bodies ...[]byte) (recs Records)

func Split

func Split(data *bytes.Buffer) (recs Records, err error)

func (Records) ExactSuffix

func (recs Records) ExactSuffix(total int64) (suffix Records)

func (Records) TotalLen

func (recs Records) TotalLen() (total int64)

func (Records) WholeRecordPrefix

func (recs Records) WholeRecordPrefix(limit int64) (prefix Records, remainder int64)

type TcpBufferSizeOpt

type TcpBufferSizeOpt struct {
	Read  int
	Write int
}

func (*TcpBufferSizeOpt) Apply

func (opt *TcpBufferSizeOpt) Apply(n *Net)

type Traced

type Traced interface {
	GetTraceId() string
}

Jump to

Keyboard shortcuts

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