subscriptionmanager

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0, BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

SubscriptionManager keeps track of subscribed topics of clients. This allows to get notified when a client connects or disconnects or a topic is subscribed or unsubscribed.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxTopicSubscriptionsPerClientReached = errors.New("maximum amount of topic subscriptions per client reached")
)

Functions

func WithCleanupThresholdCount

func WithCleanupThresholdCount[C ClientID, T Topic](cleanupThresholdCount int) options.Option[SubscriptionManager[C, T]]

WithCleanupThresholdCount defines the count of deletions that triggers shrinking of the map.

func WithCleanupThresholdRatio

func WithCleanupThresholdRatio[C ClientID, T Topic](cleanupThresholdRatio float32) options.Option[SubscriptionManager[C, T]]

WithCleanupThresholdRatio defines the ratio between the amount of deleted keys and the current map's size before shrinking is triggered.

func WithMaxTopicSubscriptionsPerClient

func WithMaxTopicSubscriptionsPerClient[C ClientID, T Topic](maxTopicSubscriptionsPerClient int) options.Option[SubscriptionManager[C, T]]

WithMaxTopicSubscriptionsPerClient defines the max amount of subscriptions per client before the client is seen as malicious and gets dropped. 0 = deactivated (default).

Types

type ClientEvent

type ClientEvent[C ClientID] struct {
	ClientID C
}

type ClientID

type ClientID interface {
	comparable
}

type ClientTopicEvent

type ClientTopicEvent[C ClientID, T Topic] struct {
	ClientID C
	Topic    T
}

type DropClientEvent

type DropClientEvent[C ClientID] struct {
	ClientID C
	Reason   error
}

type Events

type Events[C ClientID, T Topic] struct {
	// ClientConnected event is triggered when a new client connected.
	ClientConnected *event.Event1[*ClientEvent[C]]
	// ClientDisconnected event is triggered when a client disconnected.
	ClientDisconnected *event.Event1[*ClientEvent[C]]
	// TopicSubscribed event is triggered when a client subscribed to a topic.
	TopicSubscribed *event.Event1[*ClientTopicEvent[C, T]]
	// TopicUnsubscribed event is triggered when a client unsubscribed from a topic.
	TopicUnsubscribed *event.Event1[*ClientTopicEvent[C, T]]
	// TopicAdded event is triggered when a topic is subscribed for the first time by any client.
	TopicAdded *event.Event1[*TopicEvent[T]]
	// TopicRemoved event is triggered when a topic is not subscribed anymore by any client.
	TopicRemoved *event.Event1[*TopicEvent[T]]
	// DropClient event is triggered when a client should be dropped.
	DropClient *event.Event1[*DropClientEvent[C]]
}

Events contains all the events that are triggered by the SubscriptionManager.

type SubscriptionManager

type SubscriptionManager[C ClientID, T Topic] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SubscriptionManager keeps track of subscribed topics of clients. This allows to get notified when a client connects or disconnects or a topic is subscribed or unsubscribed.

func New

func New[C ClientID, T Topic](opts ...options.Option[SubscriptionManager[C, T]]) *SubscriptionManager[C, T]

func (*SubscriptionManager[C, T]) ClientSubscribedToTopic

func (s *SubscriptionManager[C, T]) ClientSubscribedToTopic(clientID C, topic T) bool

func (*SubscriptionManager[C, T]) Connect

func (s *SubscriptionManager[C, T]) Connect(clientID C)

func (*SubscriptionManager[C, T]) Disconnect

func (s *SubscriptionManager[C, T]) Disconnect(clientID C)

func (*SubscriptionManager[C, T]) Events

func (s *SubscriptionManager[C, T]) Events() *Events[C, T]

func (*SubscriptionManager[C, T]) Subscribe

func (s *SubscriptionManager[C, T]) Subscribe(clientID C, topic T)

func (*SubscriptionManager[C, T]) SubscribersSize

func (s *SubscriptionManager[C, T]) SubscribersSize() int

SubscribersSize returns the size of the underlying map of the SubscriptionManager.

func (*SubscriptionManager[C, T]) TopicHasSubscribers

func (s *SubscriptionManager[C, T]) TopicHasSubscribers(topic T) bool

func (*SubscriptionManager[C, T]) TopicsSize

func (s *SubscriptionManager[C, T]) TopicsSize() int

TopicsSize returns the size of the underlying map of the SubscriptionManager.

func (*SubscriptionManager[C, T]) TopicsSizeAll

func (s *SubscriptionManager[C, T]) TopicsSizeAll() int

TopicsSize returns the size of all underlying maps of the SubscriptionManager.

func (*SubscriptionManager[C, T]) Unsubscribe

func (s *SubscriptionManager[C, T]) Unsubscribe(clientID C, topic T)

type Topic

type Topic interface {
	constraints.Integer | ~string
}

type TopicEvent

type TopicEvent[T Topic] struct {
	Topic T
}

Jump to

Keyboard shortcuts

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