Documentation
¶
Overview ¶
Package pubsub provides a lightweight, concurrent-safe publish-subscribe implementation.
Index ¶
- Variables
- func RegisterPublisher(p *Publisher)
- func RegisterSubscriber(s *Subscriber)
- func UnregisterPublisher(p *Publisher)
- func UnregisterSubscriber(s *Subscriber)
- type Message
- type PubSubID
- type Publisher
- type Subscriber
- func (s *Subscriber) Consume() (Message, error)
- func (s *Subscriber) ConsumeContext(ctx context.Context) (Message, error)
- func (s *Subscriber) Receive(msg Message)
- func (s *Subscriber) Subscribe(pubID PubSubID, topic string)
- func (s *Subscriber) TryConsume() (Message, bool, error)
- func (s *Subscriber) Unregister()
- func (s *Subscriber) UnsubscribePublisher(pubID PubSubID)
- func (s *Subscriber) UnsubscribeTopic(pubID PubSubID, topics []string)
Constants ¶
This section is empty.
Variables ¶
var ErrSubscriberClosed = errors.New("subscriber closed")
ErrSubscriberClosed is returned when trying to consume from a closed subscriber.
var Publishers = make(map[PubSubID]*Publisher)
Publishers is a global map of all registered publishers, keyed by their ID.
var Subscribers = make(map[PubSubID]*Subscriber)
Subscribers is a global map of all registered subscribers, keyed by their ID.
Functions ¶
func RegisterPublisher ¶
func RegisterPublisher(p *Publisher)
RegisterPublisher registers a publisher in the global Publishers map.
func RegisterSubscriber ¶
func RegisterSubscriber(s *Subscriber)
RegisterSubscriber registers a subscriber in the global Subscribers map.
func UnregisterPublisher ¶
func UnregisterPublisher(p *Publisher)
UnregisterPublisher removes a publisher from the global Publishers map.
func UnregisterSubscriber ¶
func UnregisterSubscriber(s *Subscriber)
UnregisterSubscriber removes a subscriber from the global Subscribers map.
Types ¶
type Message ¶
type Message struct {
// ID is the unique identifier for this message.
ID PubSubID
// Data contains the payload of the message.
Data []byte
// Topics is a list of topics that this message belongs to.
Topics []string
// Publisher is the ID of the publisher that sent this message.
Publisher PubSubID
// CreatedAt is the timestamp when the message was created.
CreatedAt time.Time
}
Message represents a message in the pubsub system.
type PubSubID ¶
type PubSubID string
PubSubID represents a unique identifier for publishers, subscribers, and messages.
type Publisher ¶
type Publisher struct {
// ID is the unique identifier for this publisher.
ID PubSubID
// Subscribers is a map of topic to subscriber IDs.
Subscribers map[string]map[PubSubID]struct{}
// contains filtered or unexported fields
}
Publisher represents a publisher in the pubsub system. It maintains a list of subscribers for each topic.
func GetPublisher ¶
GetPublisher retrieves a publisher by its ID from the global Publishers map. Returns nil if the publisher is not found.
func NewPublisher ¶
NewPublisher creates a new publisher. If autoRegister is true, the publisher is automatically registered in the global Publishers map.
func (*Publisher) AddSubscriber ¶
AddSubscriber adds a subscriber to a specific topic. This method is safe for concurrent use.
func (*Publisher) Publish ¶
Publish sends a message to all subscribers of the specified topics. The message is sent asynchronously using goroutines. If an empty string is a topic, all subscribers (regardless of their specific topic subscriptions) will receive the message.
func (*Publisher) RemoveSubscriber ¶
RemoveSubscriber removes a subscriber from the specified topics. This method is safe for concurrent use.
func (*Publisher) Unregister ¶
func (p *Publisher) Unregister()
Unregister removes this publisher from the global Publishers map.
type Subscriber ¶
type Subscriber struct {
// ID is the unique identifier for this subscriber.
ID PubSubID
// Subscriptions is a map of publisher ID to topic subscriptions.
Subscriptions map[PubSubID]map[string]struct{}
// MsgChan is the channel through which messages are received.
MsgChan chan Message
// contains filtered or unexported fields
}
Subscriber represents a subscriber in the pubsub system. It maintains subscriptions to publishers and their topics, and provides methods to consume messages.
func GetSubscriber ¶
func GetSubscriber(id PubSubID) *Subscriber
GetSubscriber retrieves a subscriber by its ID from the global Subscribers map. Returns nil if the subscriber is not found.
func NewSubscriber ¶
func NewSubscriber(bufferSize int, autoRegister bool) *Subscriber
NewSubscriber creates a new subscriber. bufferSize specifies the size of the message channel buffer. If bufferSize <= 0, it defaults to 10. If autoRegister is true, the subscriber is automatically registered in the global Subscribers map.
func (*Subscriber) Consume ¶
func (s *Subscriber) Consume() (Message, error)
Consume blocks until a message is received from the message channel. Returns the received message and any error that occurred.
func (*Subscriber) ConsumeContext ¶
func (s *Subscriber) ConsumeContext(ctx context.Context) (Message, error)
ConsumeContext blocks until a message is received or the context is cancelled. Returns the received message and any error that occurred.
func (*Subscriber) Receive ¶
func (s *Subscriber) Receive(msg Message)
Receive receives a message and sends it to the subscriber's message channel. This method is safe for concurrent use.
func (*Subscriber) Subscribe ¶
func (s *Subscriber) Subscribe(pubID PubSubID, topic string)
Subscribe subscribes to a topic of a specific publisher. This method is safe for concurrent use.
func (*Subscriber) TryConsume ¶
func (s *Subscriber) TryConsume() (Message, bool, error)
TryConsume attempts to receive a message without blocking. Returns the received message, a boolean indicating if a message was received, and any error that occurred.
func (*Subscriber) Unregister ¶
func (s *Subscriber) Unregister()
Unregister closes the message channel and removes the subscriber from the global Subscribers map.
func (*Subscriber) UnsubscribePublisher ¶
func (s *Subscriber) UnsubscribePublisher(pubID PubSubID)
UnsubscribePublisher unsubscribes from all topics of a specific publisher. This method is safe for concurrent use.
func (*Subscriber) UnsubscribeTopic ¶
func (s *Subscriber) UnsubscribeTopic(pubID PubSubID, topics []string)
UnsubscribeTopic unsubscribes from specific topics of a publisher. This method is safe for concurrent use.