Documentation
¶
Overview ¶
Package runtime schedules message delivery across a flow graph.
The scheduling model is the main departure from Node-RED. Node-RED runs everything on one event loop and puts a setImmediate between every wire hop, so a message crossing five nodes yields to the loop five times and a CPU-heavy node stalls the editor, the HTTP endpoints and every other flow. Its send queue is also unbounded: a fast source outruns a slow sink until the process dies (node-red#855).
Emberwire gives each node instance its own goroutine and its own bounded inbox. Within a node, messages are handled strictly in order, so a node keeps state across messages without locking. Across nodes, work runs in parallel. When an inbox fills, the configured overflow policy decides what gives — and something always gives, visibly, rather than the process growing until the kubelet kills it.
Index ¶
- Constants
- Variables
- type Event
- type Options
- type OverflowPolicy
- type Runtime
- func (rt *Runtime) Events() <-chan Event
- func (rt *Runtime) Inject(nodeID string, msg *engine.Msg) error
- func (rt *Runtime) Instances() []string
- func (rt *Runtime) NodeStatus(nodeID string) (node.Status, bool)
- func (rt *Runtime) Publish(e Event)
- func (rt *Runtime) Running(nodeID string) bool
- func (rt *Runtime) SetContexts(c *store.ScopedContexts)
- func (rt *Runtime) SetCredentials(fn func(nodeID string) map[string]string)
- func (rt *Runtime) SetMetricsHooks(execTime, queueLatency func(nodeID, typ string, d time.Duration))
- func (rt *Runtime) Snapshots() []Snapshot
- func (rt *Runtime) Start(ctx context.Context) []StartError
- func (rt *Runtime) Stop(ctx context.Context) []error
- func (rt *Runtime) Warnings() []string
- type Snapshot
- type StartError
- type Stats
Constants ¶
const ( DefaultInboxCapacity = 1024 DefaultBlockTimeout = 30 * time.Second DefaultCloseTimeout = 15 * time.Second )
Defaults applied when Options leaves a field zero.
const ( TypeCatch = "catch" TypeStatus = "status" TypeComplete = "complete" )
Node types with runtime-wide routing behaviour rather than ordinary wiring.
const ( TopicStatus = "status" TopicError = "error" TopicDropped = "dropped" TopicLog = "log" TopicDebug = "debug" )
Event topics.
Variables ¶
var ErrBlockTimeout = errors.New("timed out waiting for space in a node inbox")
ErrBlockTimeout is raised when OverflowBlock waits longer than the configured timeout. A flow graph may legally contain a cycle, and a cycle of full inboxes under a blocking policy would deadlock permanently; bounding the wait turns that into a reported error instead of a wedged runtime.
var ErrInboxFull = errors.New("node inbox is full")
ErrInboxFull is raised under OverflowError when a target inbox is full.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct {
Topic string `json:"topic"`
Data map[string]any `json:"data"`
At time.Time `json:"at"`
}
Event is something the runtime wants the editor to know about: a status change, a debug message, a dropped message, a log line.
type Options ¶
type Options struct {
// InboxCapacity is how many messages may queue for one node. Zero uses
// DefaultInboxCapacity.
InboxCapacity int
// Overflow is the default policy for nodes that do not override it.
Overflow OverflowPolicy
// BlockTimeout bounds how long OverflowBlock waits before giving up.
BlockTimeout time.Duration
// CloseTimeout bounds how long a node may take to shut down. Node-RED
// waited indefinitely until 0.17 and now allows 15 seconds; a node that
// will not close must not be able to wedge a deploy.
CloseTimeout time.Duration
// Now is the clock, injectable for tests.
Now func() time.Time
}
Options tunes the scheduler.
type OverflowPolicy ¶
type OverflowPolicy string
OverflowPolicy decides what happens when a node's inbox is full.
const ( // OverflowBlock makes the sender wait, propagating back-pressure upstream // to the source. This is the default because it is the only policy that // does not lose data. OverflowBlock OverflowPolicy = "block" // OverflowDropNewest discards the arriving message. Correct for sampling a // high-rate sensor feed where the freshest reading is not sacred. OverflowDropNewest OverflowPolicy = "drop-newest" // OverflowDropOldest evicts the oldest queued message to make room. Correct // when only the latest value matters, such as a display update. OverflowDropOldest OverflowPolicy = "drop-oldest" // OverflowError raises an error to the nearest Catch node instead of // queueing, letting the flow decide. OverflowError OverflowPolicy = "error" )
func (OverflowPolicy) Valid ¶
func (p OverflowPolicy) Valid() bool
Valid reports whether p is a recognised policy.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime owns a running flow graph.
func (*Runtime) Events ¶
Events returns the channel the editor subscribes to. The channel is buffered; when it fills, events are dropped rather than blocking message delivery, because a slow editor must never be able to stall the plant floor.
func (*Runtime) Inject ¶
Inject pushes a message into a node from outside the graph. Used by the editor's manual Inject button and by tests.
func (*Runtime) NodeStatus ¶
NodeStatus returns the retained badge for a node.
func (*Runtime) SetContexts ¶
func (rt *Runtime) SetContexts(c *store.ScopedContexts)
SetContexts replaces the context stores, which is how a persistent store is substituted for the default in-memory one.
func (*Runtime) SetCredentials ¶
SetCredentials installs the lookup used to hand a node its decrypted secrets.
func (*Runtime) SetMetricsHooks ¶
func (rt *Runtime) SetMetricsHooks(execTime, queueLatency func(nodeID, typ string, d time.Duration))
SetMetricsHooks installs optional observers for per-node timings.
func (*Runtime) Snapshots ¶
Snapshots returns per-node counters for every running node, sorted by id.
func (*Runtime) Start ¶
func (rt *Runtime) Start(ctx context.Context) []StartError
Start builds and starts every enabled node.
It returns the per-node failures it tolerated. A non-nil slice does not mean the runtime failed to start.
type Snapshot ¶
type Snapshot struct {
NodeID string `json:"nodeId"`
Type string `json:"type"`
Received int64 `json:"received"`
Sent int64 `json:"sent"`
Errors int64 `json:"errors"`
Dropped int64 `json:"dropped"`
Blocked int64 `json:"blocked"`
QueueLen int `json:"queueLen"`
QueueCap int `json:"queueCap"`
QueueHigh int64 `json:"queueHigh"`
}
Snapshot is a point-in-time copy of a node's counters.
type StartError ¶
StartError records a node that failed to build or start. A failure is scoped to the one node: the rest of the flow still runs, and the editor is told which node is broken. Failing the whole deploy because one node's config is wrong is how a single typo takes a line down.
func (StartError) Error ¶
func (e StartError) Error() string
type Stats ¶
type Stats struct {
Received atomic.Int64
Sent atomic.Int64
Errors atomic.Int64
Dropped atomic.Int64
Blocked atomic.Int64
QueueHigh atomic.Int64
}
Stats are the per-node counters the metrics endpoint and the editor's status badge read. Counters are read with atomics so a scrape never blocks delivery.