pubsub

package
v1.0.4-gitspaces-beta Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed = errors.New("pubsub: subscriber is closed")
)
View Source
var WireSet = wire.NewSet(
	ProvidePubSub,
)

Functions

This section is empty.

Types

type Config

type Config struct {
	App       string // app namespace prefix
	Namespace string

	Provider Provider

	HealthInterval time.Duration
	SendTimeout    time.Duration
	ChannelSize    int
}

type Consumer

type Consumer interface {
	Subscribe(ctx context.Context, topics ...string) error
	Unsubscribe(ctx context.Context, topics ...string) error
	Close() error
}

type InMemory

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

func NewInMemory

func NewInMemory(options ...Option) *InMemory

NewInMemory create an instance of memory pubsub implementation.

func (*InMemory) Close

func (r *InMemory) Close(_ context.Context) error

func (*InMemory) Publish

func (r *InMemory) Publish(ctx context.Context, topic string, payload []byte, opts ...PublishOption) error

Publish event to message broker with payload.

func (*InMemory) Subscribe

func (r *InMemory) Subscribe(
	ctx context.Context,
	topic string,
	handler func(payload []byte) error,
	options ...SubscribeOption,
) Consumer

Subscribe consumer to process the event with payload.

type Option

type Option interface {
	Apply(*Config)
}

An Option configures a pubsub instance.

func WithApp

func WithApp(value string) Option

WithApp returns an option that set config app name.

func WithHealthCheckInterval

func WithHealthCheckInterval(value time.Duration) Option

WithHealthCheckInterval specifies the config health check interval. PubSub will ping Server if it does not receive any messages within the interval (redis, ...). To disable health check, use zero interval.

func WithNamespace

func WithNamespace(value string) Option

WithNamespace returns an option that set config namespace.

func WithSendTimeout

func WithSendTimeout(value time.Duration) Option

WithSendTimeout specifies the pubsub send timeout after which the message is dropped.

func WithSize

func WithSize(value int) Option

WithSize specifies the Go chan size in config that is used to buffer incoming messages.

type OptionFunc

type OptionFunc func(*Config)

OptionFunc is a function that configures a pubsub config.

func (OptionFunc) Apply

func (f OptionFunc) Apply(config *Config)

Apply calls f(config).

type Provider

type Provider string
const (
	ProviderMemory Provider = "inmemory"
	ProviderRedis  Provider = "redis"
)

type PubSub

type PubSub interface {
	Publisher
	// Subscribe consumer to process the topic with payload, this should be
	// blocking operation.
	Subscribe(ctx context.Context, topic string,
		handler func(payload []byte) error, options ...SubscribeOption) Consumer
}

func ProvidePubSub

func ProvidePubSub(config Config, client redis.UniversalClient) PubSub

type PublishConfig

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

type PublishOption

type PublishOption interface {
	Apply(*PublishConfig)
}

func WithPublishApp

func WithPublishApp(value string) PublishOption

WithPublishApp modifies publish config app identifier.

func WithPublishNamespace

func WithPublishNamespace(value string) PublishOption

WithPublishNamespace modifies publish config namespace.

type PublishOptionFunc

type PublishOptionFunc func(*PublishConfig)

PublishOptionFunc is a function that configures a publish config.

func (PublishOptionFunc) Apply

func (f PublishOptionFunc) Apply(config *PublishConfig)

Apply calls f(publishConfig).

type Publisher

type Publisher interface {
	// Publish topic to message broker with payload.
	Publish(ctx context.Context, topic string, payload []byte,
		options ...PublishOption) error
}

type Redis

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

func NewRedis

func NewRedis(client redis.UniversalClient, options ...Option) *Redis

NewRedis create an instance of redis PubSub implementation.

func (*Redis) Close

func (r *Redis) Close(_ context.Context) error

func (*Redis) Publish

func (r *Redis) Publish(ctx context.Context, topic string, payload []byte, opts ...PublishOption) error

Publish event topic to message broker with payload.

func (*Redis) Subscribe

func (r *Redis) Subscribe(
	ctx context.Context,
	topic string,
	handler func(payload []byte) error,
	options ...SubscribeOption,
) Consumer

Subscribe consumer to process the event with payload.

type SubscribeConfig

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

type SubscribeOption

type SubscribeOption interface {
	Apply(*SubscribeConfig)
}

SubscribeOption configures a subscription config.

func WithChannelHealthCheckInterval

func WithChannelHealthCheckInterval(value time.Duration) SubscribeOption

WithChannelHealthCheckInterval specifies the channel health check interval. PubSub will ping Server if it does not receive any messages within the interval. To disable health check, use zero interval.

func WithChannelNamespace

func WithChannelNamespace(value string) SubscribeOption

WithNamespace returns an channel option that configures namespace.

func WithChannelSendTimeout

func WithChannelSendTimeout(value time.Duration) SubscribeOption

WithChannelSendTimeout specifies the channel send timeout after which the message is dropped.

func WithChannelSize

func WithChannelSize(value int) SubscribeOption

WithChannelSize specifies the Go chan size that is used to buffer incoming messages for subscriber.

func WithTopics

func WithTopics(topics ...string) SubscribeOption

WithTopics specifies the topics to subsribe.

type SubscribeOptionFunc

type SubscribeOptionFunc func(*SubscribeConfig)

SubscribeOptionFunc is a function that configures a subscription config.

func (SubscribeOptionFunc) Apply

func (f SubscribeOptionFunc) Apply(config *SubscribeConfig)

Apply calls f(subscribeConfig).

Jump to

Keyboard shortcuts

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