intracom

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrInvalidIntracomNil    = Error("invalid intracom, cannot be nil")
	ErrIntracomClosed        = Error("intracom is closed")
	ErrTopicNotFound         = Error("topic not found")
	ErrTopicAlreadyExists    = Error("topic already exists")
	ErrTopicDoesNotExist     = Error("topic does not exist")
	ErrInvalidTopicType      = Error("topic exists but with a different type")
	ErrTopicClosed           = Error("topic is closed")
	ErrConsumerAlreadyExists = Error("consumer already exists")
	ErrMaxTimeoutReached     = Error("max timeout reached")
)
View Source
const (
	ActionClosingIntracom      = Action("closing intracom")
	ActionClosingTopic         = Action("closing topic")
	ActionRemovingTopic        = Action("removing topic")
	ActionCreatingTopic        = Action("creating topic")
	ActionRemovingSubscription = Action("removing subscription")
	ActionCreatingSubscription = Action("creating subscription")
)

Variables

This section is empty.

Functions

func Close

func Close(ic *Intracom) error

Close interacts with the Intracom registry and closes all topics.

func CreateSubscription

func CreateSubscription[T any](ctx context.Context, ic *Intracom, topic string, maxWait time.Duration, conf SubscriberConfig[T]) (<-chan T, error)

CreateSubscription will (if set) wait a max timeout for a topic to exist and the proceed to subscribe to that topic. If the topic does not exist within the maxWait duration, an error is returned. If maxWait is 0, the function will wait indefinitely for the topic to exist. If the intracom is closed, an error is returned. If the context is canceled, a context error is returned.

func RemoveSubscription

func RemoveSubscription[T any](ic *Intracom, topic string, consumer string, ch <-chan T) error

RemoveSubscription removes a subscription from a topic consumer name and consumer channel are required to remove the subscription. Normally whoever created the subscription should also be in-charge of removing it.

func RemoveTopic

func RemoveTopic[T any](ic *Intracom, name string) error

Types

type Action

type Action string

Action is the action that was attempted when an error occurred.

type Broadcaster added in v1.2.1

type Broadcaster[T any] interface {
	Broadcast(requests <-chan any, messages chan T)
}

type BufferPolicyDropNewest added in v1.2.1

type BufferPolicyDropNewest[T any] struct{}

func (BufferPolicyDropNewest[T]) Handle added in v1.2.1

func (d BufferPolicyDropNewest[T]) Handle(ch chan T, message T, stopC <-chan struct{}) error

type BufferPolicyDropNewestAfterTimeout added in v1.2.1

type BufferPolicyDropNewestAfterTimeout[T any] struct {
	Timer      *time.Timer
	DropTimout time.Duration
}

func (BufferPolicyDropNewestAfterTimeout[T]) Handle added in v1.2.1

func (d BufferPolicyDropNewestAfterTimeout[T]) Handle(ch chan T, message T, stopC <-chan struct{}) error

type BufferPolicyDropNone added in v1.2.1

type BufferPolicyDropNone[T any] struct{}

func (BufferPolicyDropNone[T]) Handle added in v1.2.1

func (d BufferPolicyDropNone[T]) Handle(ch chan T, message T, stopC <-chan struct{}) error

type BufferPolicyDropOldest added in v1.2.1

type BufferPolicyDropOldest[T any] struct{}

func (BufferPolicyDropOldest[T]) Handle added in v1.2.1

func (d BufferPolicyDropOldest[T]) Handle(ch chan T, message T, stopC <-chan struct{}) error

type BufferPolicyDropOldestAfterTimeout added in v1.2.1

type BufferPolicyDropOldestAfterTimeout[T any] struct {
	Timer       *time.Timer
	DropTimeout time.Duration
}

func (BufferPolicyDropOldestAfterTimeout[T]) Handle added in v1.2.1

func (d BufferPolicyDropOldestAfterTimeout[T]) Handle(ch chan T, message T, stopC <-chan struct{}) error

type BufferPolicyHandler added in v1.2.1

type BufferPolicyHandler[T any] interface {
	Handle(ch chan T, message T, stopC <-chan struct{}) error
}

type Channel added in v1.2.1

type Channel[T any] interface {
	ChannelCloser[T]
	ChannelSender[T]
	Chan() <-chan T
}

type ChannelCloser added in v1.2.1

type ChannelCloser[T any] interface {
	Close() error
}

type ChannelSender added in v1.2.1

type ChannelSender[T any] interface {
	Send(message T) error
}

type ErrIntracom

type ErrIntracom struct {
	Action Action
	Err    error
}

func (ErrIntracom) Error

func (e ErrIntracom) Error() string

type ErrSubscribe

type ErrSubscribe struct {
	Topic    string
	Consumer string
	Action   Action
	Err      error
}

func (ErrSubscribe) Error

func (e ErrSubscribe) Error() string

type ErrTopic

type ErrTopic struct {
	Topic  string
	Action Action
	Err    error
}

func (ErrTopic) Error

func (e ErrTopic) Error() string

type Error

type Error string

Error is a custom error type for the intracom package.

func (Error) Error

func (e Error) Error() string

type Intracom

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

Intracom acts as a registry for all topic channels. The Intracom struct is thread-safe and can be used concurrently. Use the pure generic functions below to operate against the Intracom struct: CreateTopic, CreateSubscription, RemoveSubscription, Close

func New

func New(name string, opts ...Option) *Intracom

New creates a new instance of Intracom with the given name and logger and starts the broker routine.

type Option

type Option func(*Intracom)

type SubscriberConfig

type SubscriberConfig[T any] struct {
	ConsumerGroup string
	ErrIfExists   bool
	BufferSize    int
	BufferPolicy  BufferPolicyHandler[T]
	DropTimeout   time.Duration
}

type Subscription

type Subscription struct {
	Topic         string
	ConsumerGroup string
	// MaxWaitTimeout is the max time to wait before error due to a topic not existing.
	MaxWaitTimeout time.Duration
}

type SyncBroadcaster added in v1.2.1

type SyncBroadcaster[T any] struct {
	SubscriberAware bool // if true, broadcaster wont broadcast if there are no subscribers.
}

func (SyncBroadcaster[T]) Broadcast added in v1.2.1

func (b SyncBroadcaster[T]) Broadcast(requests <-chan any, broadcast chan T)

type Topic

type Topic[T any] interface {
	Name() string                                                              // Name returns the unique name of the topic.
	PublishChannel() chan<- T                                                  // PublishChannel returns the channel publishers use to send messages to the topic.
	Subscribe(ctx context.Context, conf SubscriberConfig[T]) (<-chan T, error) // Subscribe will attemp to add a consumer group to the topic.
	Unsubscribe(consumer string, ch <-chan T) error                            // Unsubscribe will remove the consumer group from the topic and close the subscriber channel.
	Close() error                                                              // Close will remove all consumer groups from the topic and close all channels.
}

func CreateTopic

func CreateTopic[T any](ic *Intracom, conf TopicConfig) (Topic[T], error)

CreateTopic creates a new topic with the given configuration. Topic names must be unique, if the topic already exists, an error is returned.

func NewTopic added in v1.2.1

func NewTopic[T any](conf TopicConfig, opts ...TopicOption[T]) Topic[T]

type TopicConfig

type TopicConfig struct {
	Name            string // unique name for the topic
	ErrIfExists     bool   // return error if topic already exists
	SubscriberAware bool   // if true, topic broadcaster wont broadcast if there are no subscribers.
}

type TopicOption added in v1.2.1

type TopicOption[T any] func(*topic[T])

func WithBroadcaster added in v1.2.1

func WithBroadcaster[T any](b Broadcaster[T]) TopicOption[T]