wire

package
v3.7.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Overview

Package wire provides the wire protocol for how peers scheduler peers communicate.

Index

Constants

This section is empty.

Variables

View Source
var (
	// LocalScheduler is the address of the local scheduler when using the
	// [Local] listener.
	LocalScheduler net.Addr = localAddr("scheduler")

	// LocalWorker is the address of the local worker when using the
	// [Local] listener.
	LocalWorker net.Addr = localAddr("worker")
)
View Source
var ErrConnClosed = errors.New("connection closed")

ErrConnClosed indicates a closed connection between peers.

Functions

This section is empty.

Types

type AckFrame

type AckFrame struct {
	// ID of the [MessageFrame] being acknowledged.
	ID uint64
}

AckFrame is a Frame that acknowledges a MessageFrame was processed successfully.

func (AckFrame) FrameKind

func (a AckFrame) FrameKind() FrameKind

FrameKind returns FrameKindAck.

type Conn

type Conn interface {
	// Send sends the provided Frame to the peer. Send blocks until the Frame
	// has been sent to the peer, but does not wait for the peer to acknowledge
	// receipt of the Frame.
	//
	// Send returns an error if the context is canceled or if the connection is
	// closed.
	Send(context.Context, Frame) error

	// Recv receives the next Frame from the peer. Recv blocks until a Frame is
	// available. Recv returns an error if the context is canceled or if the
	// connection is closed.
	//
	// Callers should take care to avoid long periods of where there is not an
	// active call to Recv to avoid blocking the peer's Send call.
	Recv(context.Context) (Frame, error)

	// Close closes the Conn. Close may be called by either side of the
	// connection. After the connection has been closed, calls to Send or Recv
	// return [ErrConnClosed].
	Close() error

	// LocalAddr returns the address of the local side of the connection.
	LocalAddr() net.Addr

	// RemoteAddr returns the address of the remote side of the connection.
	RemoteAddr() net.Addr
}

Conn is a communication stream between two peers.

type Dialer

type Dialer interface {
	// Dial connects to the scheduler peer at the provided "to" address. The
	// "from" address is used to establish the address that can be used to
	// connect back to the caller. Dial returns an error if the context is
	// canceled or if the connection cannot be established.
	Dial(ctx context.Context, from, to net.Addr) (Conn, error)
}

A Dialer establishes connections to scheduler peers.

type DiscardFrame

type DiscardFrame struct {
	// ID of the [MessageFrame] being discarded.
	ID uint64
}

DiscardFrame is a Frame that informs the peer that a MessageFrame has been discarded and an acknowledgement is no longer needed.

A peer receiving a DiscardFrame should produce no acknowledgement.

func (DiscardFrame) FrameKind

func (d DiscardFrame) FrameKind() FrameKind

FrameKind returns FrameKindDiscard.

type Error

type Error struct {
	// Code is an HTTP status code representing the kind of error received.
	Code int32

	// Message is a human-readable description of the error.
	Message string
}

Error represents an error received in response to a message.

func Errorf

func Errorf(code int32, format string, args ...interface{}) *Error

Errorf creates a new Error with the given code and formatted message.

func (*Error) Error

func (e *Error) Error() string

Error returns the message of the error.

func (*Error) Is

func (e *Error) Is(target error) bool

Is returns true if the target is identical to the error, providing functionality for errors.Is.

type Frame

type Frame interface {
	FrameKind() FrameKind
	// contains filtered or unexported methods
}

A Frame is the lowest level of communication between two peers. Frames are an envelope for messages between peers.

type FrameKind

type FrameKind int

FrameKind represents the type of a Frame.

const (
	// FrameKindInvalid represents an invalid frame.
	FrameKindInvalid FrameKind = iota

	FrameKindMessage // FrameKindMessage is used for [MessageFrame].
	FrameKindAck     // FrameKindAck is used for [AckFrame].
	FrameKindNack    // FrameKindNack is used for [NackFrame].
	FrameKindDiscard // FrameKindDiscard is used for [DiscardFrame].
)

func (FrameKind) String

func (k FrameKind) String() string

String returns a string representation of k.

type HTTP2Dialer

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

HTTP2Dialer holds an http client to pool the connections.

func NewHTTP2Dialer

func NewHTTP2Dialer(path string) *HTTP2Dialer

NewHTTP2Dialer creates a Dialer that can open HTTP/2 connections to the specified address.

func (*HTTP2Dialer) Dial

func (d *HTTP2Dialer) Dial(ctx context.Context, from, to net.Addr) (Conn, error)

Dial establishes an HTTP/2 connection to the specified address.

type HTTP2Listener

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

HTTP2Listener implements Listener for HTTP/2-based connections.

func NewHTTP2Listener

func NewHTTP2Listener(addr net.Addr, optFuncs ...HTTP2ListenerOptFunc) *HTTP2Listener

NewHTTP2Listener creates a new HTTP/2 listener on the specified address.

func (*HTTP2Listener) Accept

func (l *HTTP2Listener) Accept(ctx context.Context) (Conn, error)

Accept waits for and returns the next connection to the listener.

func (*HTTP2Listener) Addr

func (l *HTTP2Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*HTTP2Listener) Close

func (l *HTTP2Listener) Close(_ context.Context) error

Close closes the listener.

func (*HTTP2Listener) ServeHTTP

func (l *HTTP2Listener) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming connections.

type HTTP2ListenerOptFunc

type HTTP2ListenerOptFunc func(*http2ListenerOpts)

func WithHTTP2ListenerLogger

func WithHTTP2ListenerLogger(logger log.Logger) HTTP2ListenerOptFunc

type Handler

type Handler func(ctx context.Context, peer *Peer, message Message) error

Handler is a function that handles a message received from the peer. The local peer is passed as an argument to allow using the same Handler for multiple Peers.

Handlers are invoked in a dedicated goroutine. Slow handlers cause backpressure on the connection.

Once Handler returns, the sending peer will be informed about the message delivery status. If Handler returns an error, the error message will be sent to the peer.

type Listener

type Listener interface {
	// Accept waits for and returns the next connection to the listener. Accept
	// returns an error if the context is canceled or if the listener is closed.
	Accept(ctx context.Context) (Conn, error)

	// Close closes the listener. Any blocked Accept operations will be
	// unblocked and return errors.
	Close(ctx context.Context) error

	// Addr returns the listener's advertised network address. Peers use this
	// address to connect to the listener.
	Addr() net.Addr
}

Listener waits for incoming connections from scheduler peers.

type Local

type Local struct {
	// Address to broadcast when connecting to peers. Should be [LocalScheduler]
	// for the scheduler and [LocalWorker] for the worker.
	Address net.Addr
	// contains filtered or unexported fields
}

Local is a Listener that accepts connections from the local process without using the network.

func (*Local) Accept

func (l *Local) Accept(ctx context.Context) (Conn, error)

Accept waits for and returns the next connection to the listener.

func (*Local) Addr

func (l *Local) Addr() net.Addr

Addr returns the listener's advertised address.

func (*Local) Close

func (l *Local) Close(_ context.Context) error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*Local) DialFrom

func (l *Local) DialFrom(ctx context.Context, from net.Addr) (Conn, error)

DialFrom dials the local listener, blocking until the connection is accepted or the context is canceled. Returns the caller's connection.

type LocalDialer

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

LocalDialer is a Dialer that can open local connections to one or more Local listeners.

func NewLocalDialer

func NewLocalDialer(listeners ...*Local) *LocalDialer

NewLocalDialer creates a new LocalDialer that can open connections to the provided listeners.

func (*LocalDialer) Dial

func (d *LocalDialer) Dial(ctx context.Context, from, to net.Addr) (Conn, error)

Dial opens a connection to the provided address. Returns an error if the address was not registered.

type Message

type Message interface {

	// Kind returns the kind of message.
	Kind() MessageKind
	// contains filtered or unexported methods
}

A Message is a message exchanged between peers.

type MessageFrame

type MessageFrame struct {
	// ID uniquely identifies the message. It is up to the sender to ensure that
	// IDs are unique within a stream.
	ID uint64

	// Message being sent to the peer.
	Message Message
}

MessageFrame is a Frame that sends a Message to the peer. MessageFrames are paired with an AckFrame to acknowledge that the message has been successfully processed, or NackFrame in case of failure.

func (MessageFrame) FrameKind

func (m MessageFrame) FrameKind() FrameKind

FrameKind returns FrameKindMessage.

type MessageKind

type MessageKind int

MessageKind represents the type of a message.

const (
	// MessageKindInvalid represents an invalid message.
	MessageKindInvalid MessageKind = iota

	MessageKindWorkerHello     // MessageKindWorkerHello represents [WorkerHelloMessage].
	MessageKindWorkerSubscribe // MessageKindWorkerSubscribe represents [WorkerSubscribeMessage].
	MessageKindWorkerReady     // MessageKindWorkerReady represents [WorkerReadyMessage].
	MessageKindTaskAssign      // MessageKindTaskAssign represents [TaskAssignMessage].
	MessageKindTaskCancel      // MessageKindTaskCancel represents [TaskCancelMessage].
	MessageKindTaskFlag        // MessageKindTaskFlag represents [TaskFlagMessage].
	MessageKindTaskStatus      // MessageKindTaskStatus represents [TaskStatusMessage].
	MessageKindStreamBind      // MessageKindStreamBind represents [StreamBindMessage].
	MessageKindStreamData      // MessageKindStreamData represents [StreamDataMessage].
	MessageKindStreamStatus    // MessageKindStreamStatus represents [StreamStatusMessage].
)

func (MessageKind) String

func (k MessageKind) String() string

String returns a string representation of k.

type Metrics

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

Metrics is a set of metrics for a Peer.

func NewMetrics

func NewMetrics() *Metrics

func (*Metrics) Register

func (m *Metrics) Register(reg prometheus.Registerer) error

func (*Metrics) Unregister

func (m *Metrics) Unregister(reg prometheus.Registerer)

type NackFrame

type NackFrame struct {
	// ID of the [MessageFrame] being negatively acknowledged.
	ID uint64

	// Error is the error that occurred.
	Error *Error
}

NackFrame is a Frame that notifies that a MessageFrame could not be processed.

func (NackFrame) FrameKind

func (n NackFrame) FrameKind() FrameKind

FrameKind returns FrameKindNack.

type Peer

type Peer struct {
	Logger  log.Logger
	Metrics *Metrics
	Conn    Conn    // Connection to use for communication.
	Handler Handler // Handler for incoming messages from the remote peer.
	Buffer  int     // Buffer size for incoming and outgoing messages.
	// contains filtered or unexported fields
}

Peer wraps a Conn into a synchronous API that acts as both a server and a client.

Callers must call Peer.Serve to run the peer.

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

LocalAddr returns the address of the local peer.

func (*Peer) RemoteAddr

func (p *Peer) RemoteAddr() net.Addr

RemoteAddr returns the address of the remote peer.

func (*Peer) SendMessage

func (p *Peer) SendMessage(ctx context.Context, message Message) error

SendMessage sends a message to the remote peer. SendMessage blocks until the provided context is canceled or the remote peer positively or negatively acknowledges the message.

Peer.Serve must be running when SendMessage is called, otherwise it blocks until the context is canceled.

func (*Peer) SendMessageAsync

func (p *Peer) SendMessageAsync(ctx context.Context, message Message) error

SendMessageAsync sends a message to the remote peer asynchronously. SendMessageAsync blocks until the message has been sent over the connection but does not wait for an acknowledgement or response.

Peer.Serve must be running before SendMessageAsync is called, otherwise it blocks until the context is canceled.

func (*Peer) Serve

func (p *Peer) Serve(ctx context.Context) error

Serve runs the peer, blocking until the provided context is canceled.

type StreamBindMessage

type StreamBindMessage struct {
	StreamID ulid.ULID // ID of the stream.
	Receiver net.Addr  // Address of the stream receiver.
}

StreamBindMessage is sent by the scheduler to a worker to inform the worker about the location of a stream receiver.

func (StreamBindMessage) Kind

Kind returns MessageKindStreamBind.

type StreamDataMessage

type StreamDataMessage struct {
	StreamID ulid.ULID         // ID of the stream.
	Data     arrow.RecordBatch // Payload data for the stream.
}

StreamDataMessage is sent by a worker to a stream receiver to provide payload data for a stream.

func (StreamDataMessage) Kind

Kind returns MessageKindStreamData.

type StreamStatusMessage

type StreamStatusMessage struct {
	StreamID ulid.ULID            // ID of the stream.
	State    workflow.StreamState // State of the stream.
}

StreamStatusMessage communicates the status of the sending side of a stream. It is sent in two cases:

  • By the sender of the stream, to inform the scheduler about the status of that stream.
  • By the scheduler, to inform the stream reader about the status of the stream.

The scheduler is responsible for informing stream receivers about stream status to avoid keeping streams alive if the sender disconnects.

func (StreamStatusMessage) Kind

Kind returns MessageKindStreamStatus.

type TaskAssignMessage

type TaskAssignMessage struct {
	Task *workflow.Task // Task to run.

	// StreamStates holds the most recent state of each stream that the task
	// reads from.
	//
	// StreamStates does not have any entries for streams that the task
	// writes to.
	StreamStates map[ulid.ULID]workflow.StreamState

	// Metadata holds additional metadata about the task.
	Metadata http.Header
}

TaskAssignMessage is sent by the scheduler to a worker when there is a task to run.

Workers that have no threads available should reject task assignment with a HTTP 429 Too Many Requests. When this happens, the scheduler will remove the ready state from the worker until it receives a WorkerReadyMessage.

func (TaskAssignMessage) Kind

Kind returns MessageKindTaskAssign.

type TaskCancelMessage

type TaskCancelMessage struct {
	ID ulid.ULID // ID of the Task to cancel.
}

TaskCancelMessage is sent by the scheduler to a worker when a task is no longer needed, and running that task should be aborted.

func (TaskCancelMessage) Kind

Kind returns MessageKindTaskCancel.

type TaskFlagMessage

type TaskFlagMessage struct {
	ID ulid.ULID // ID of the Task to update.

	// Interruptible indicates that tasks blocked on writing or reading to a
	// [Stream] can be paused, and that worker can accept new tasks to run.
	// Tasks are not interruptible by default.
	Interruptible bool
}

TaskFlagMessage is sent by the scheduler to update the runtime flags of a task.

func (TaskFlagMessage) Kind

Kind returns MessageKindTaskFlag.

type TaskStatusMessage

type TaskStatusMessage struct {
	ID     ulid.ULID           // ID of the Task to update.
	Status workflow.TaskStatus // Current status of the task.
}

TaskStatusMessage is sent by the worker to the scheduler to inform the scheduler of the current status of a task.

func (TaskStatusMessage) Kind

Kind returns MessageKindTaskStatus.

type WorkerHelloMessage

type WorkerHelloMessage struct {
	// Threads is the number of threads the worker has available.
	//
	// The scheduler uses Threads to determine the maximum number of tasks
	// that can be assigned concurrently to a worker.
	Threads int
}

WorkerHelloMessage is sent by a peer to the scheduler to establish itself as a control plane connection that can run tasks.

WorkerHelloMessage must be sent by workers before any other worker messages.

func (WorkerHelloMessage) Kind

Kind returns MessageKindWorkerHello.

type WorkerReadyMessage

type WorkerReadyMessage struct {
}

WorkerReadyMessage is sent by a worker to the scheduler to signal that the worker has at least one worker thread available for running tasks.

Workers may send WorkerReadyMessage at any time, but one must be sent in response to a WorkerSubscribeMessage once at least one worker thread is available.

func (WorkerReadyMessage) Kind

Kind returns MessageKindWorkerReady.

type WorkerSubscribeMessage

type WorkerSubscribeMessage struct {
}

WorkerSubscribeMessage is sent by a scheduler to request a WorkerReadyMessage from workers once they have at least one worker thread available.

The subscription is cleared once the next WorkerReadyMessage is sent.

func (WorkerSubscribeMessage) Kind

Kind returns MessageKindWorkerSubscribe.

Jump to

Keyboard shortcuts

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