broker

package
v1.0.22 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 0 Imported by: 3

README

broker

broker defines the message-broker abstraction used across the Meshery ecosystem (Meshery Server, MeshSync, adapters). Producers publish and consumers subscribe through the Handler interface, decoupling callers from the concrete transport.

Two implementations ship with MeshKit:

  • broker/nats — a NATS-backed handler (Nats) used in cluster deployments, where MeshSync publishes discovery events to Meshery Broker (NATS) and Meshery Server consumes them.
  • broker/channel — an in-process handler (ChannelBrokerHandler) backed by Go channels, used for embedded/library mode and tests, with no external broker.

Handler interface

type Handler interface {
	Publish(subject string, message *Message) error
	PublishWithChannel(subject string, msgch chan *Message) error
	Subscribe(subject, queue string, message []byte) error
	SubscribeWithChannel(subject, queue string, msgch chan *Message) error
	Unsubscribe(subject string) error
	Info() string
	DeepCopyObject() Handler
	DeepCopyInto(Handler)
	IsEmpty() bool
	CloseConnection()
	ConnectedEndpoints() []string
}

Unsubscribe

Unsubscribe(subject string) error tears down every subscription previously created for subject (across all queue groups) and releases the resources they hold: for the NATS handler the underlying nats.Subscription(s); for the channel handler the per-queue delivery channels (which ends the goroutines started by SubscribeWithChannel).

It is:

  • a no-op for a subject with no active subscriptions (including a nil/uninitialized connection), and
  • safe to call more than once.
When to call it

Long-lived, request-scoped subscriptions must be torn down when the request ends, or the subscription and its delivery goroutine leak for the lifetime of the process. The canonical case is an interactive session (exec, log stream) keyed by a unique subject:

subject := fmt.Sprintf("input.%s", sessionID)
if err := handler.SubscribeWithChannel(subject, connName, msgCh); err != nil {
	return err
}
defer handler.Unsubscribe(subject) // release the subscription on session teardown

Subscriptions that live for the whole process (a server's long-running consumer) do not need explicit unsubscription; CloseConnection tears everything down.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Request          ObjectType = "request-payload"
	MeshSync         ObjectType = "meshsync-data"
	LogStreamObject  ObjectType = "log-stream"
	SMI              ObjectType = "smi-data"
	ErrorObject      ObjectType = "error"
	ExecInputObject  ObjectType = "exec-input"
	ExecOutputObject ObjectType = "exec-output"
	ActiveExecObject ObjectType = "active-exec"

	Add        EventType = "ADDED"
	Update     EventType = "MODIFIED"
	Delete     EventType = "DELETED"
	ErrorEvent EventType = "ERROR"
	ReSync     EventType = "RESYNC"

	LogRequestEntity      RequestEntity = "log-stream"
	ReSyncDiscoveryEntity RequestEntity = "resync-discovery"
	ExecRequestEntity     RequestEntity = "exec-request"
	ActiveExecEntity      RequestEntity = "active-exec"
)
View Source
var (
	NotConnected = "not-connected"
)

Functions

This section is empty.

Types

type EventType

type EventType string

type Handler

type Handler interface {
	PublishInterface
	SubscribeInterface
	Info() string
	DeepCopyObject() Handler
	DeepCopyInto(Handler)
	IsEmpty() bool
	CloseConnection()
	ConnectedEndpoints() []string //To get the IP addresses of connected endpoints
	IsConnected() bool            //Whether the underlying connection is currently live
}

type Message

type Message struct {
	ObjectType ObjectType
	EventType  EventType
	Request    *RequestObject
	Object     interface{}
}

type ObjectType

type ObjectType string

type PublishInterface

type PublishInterface interface {
	Publish(string, *Message) error
	PublishWithChannel(string, chan *Message) error
}

type RequestEntity

type RequestEntity string

type RequestObject

type RequestObject struct {
	Entity  RequestEntity
	Payload interface{}
}

type SubscribeInterface

type SubscribeInterface interface {
	Subscribe(string, string, []byte) error
	SubscribeWithChannel(string, string, chan *Message) error
	// Unsubscribe tears down every subscription previously created for the given
	// subject (across all queue groups) and releases the resources associated
	// with them - for the NATS handler the underlying nats.Subscription(s), for
	// the channel handler the per-queue delivery channels. It is safe to call for
	// a subject with no active subscriptions (a no-op) and safe to call more than
	// once. Callers that create a subscription for the lifetime of a session
	// (e.g. an interactive exec/log stream keyed by a unique subject) should call
	// Unsubscribe on teardown so the subscription and its delivery goroutine do
	// not leak.
	Unsubscribe(subject string) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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