localbroker

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package localbroker contains the shared in-process broker implementation.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("local broker: closed")

ErrClosed is returned when publishing or subscribing after the broker closes. ErrClosed 表示 broker 已关闭后再执行发布或订阅操作时返回的错误。

Functions

This section is empty.

Types

type Broker

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

Broker is an in-process best-effort pub/sub broker. Broker 进程内尽力而为的发布/订阅 broker,按 topic 维护 handler 订阅表。

func New

func New(nodeID string) *Broker

New creates an in-process broker for nodeID. New 创建指定节点 ID 的进程内 broker。

func (*Broker) Close

func (b *Broker) Close() error

Close closes the broker and removes all subscriptions. Close 关闭 broker 并清空所有订阅。

func (*Broker) Publish

func (b *Broker) Publish(ctx context.Context, topic string, data []byte) error

Publish delivers data to every handler currently subscribed to topic. Publish 向所有订阅了指定 topic 的 handler 并发投递消息。

func (*Broker) Subscribe

func (b *Broker) Subscribe(ctx context.Context, topic string, handler Handler) (Subscription, error)

Subscribe registers handler for topic. Subscribe 注册 handler 到指定 topic,返回可关闭的订阅句柄。

type Handler

type Handler func(context.Context, Message) error

Handler handles one broker message. Handler 处理单条 broker 消息。

type Message

type Message struct {
	Topic      string // 消息所属 topic
	Data       []byte // 消息原始字节
	SourceNode string // 发布消息的节点 ID
}

Message is delivered to subscribed handlers. Message 是投递给订阅 handler 的消息,包含 topic、原始数据和来源节点。

type Subscription

type Subscription interface {
	Close() error
}

Subscription represents one local broker subscription. Subscription 表示一个本地 broker 订阅句柄。

Jump to

Keyboard shortcuts

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