pubsub

package module
v0.0.0-...-606da80 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 5 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

pubsub

Simple pubsub framework for Go.

This is (should be, fingers crossed) safe to be used from multiple goroutines. Designed such that only one goroutine makes changes to the object structure

var svc pubsub.Service

// Create a Loopback object for in-memory broadcasting.
// You can create similar publishing mechanism for gRPC, or whatever.
l := pubsub.NewLoopback(&svc)

var msgs []interface{}
// You can create your own subscriber, of course, but this
// is the built-in hack to allow closures (eek)
sub := pubsub.SubscribeFunc(func(v interface{}) {
  msgs = append(msgs, v)
})

// Subscribing before starting the main loop is safe
svc.Subscribe(sub)

// Sending before starting the main loop is safe 
l.Send(`Hello`)

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

// Start the main loop
go svc.Run(ctx)

// Sending after starting the main loop.. is obviously safe.
l.Send(`World!`)

// If you have another subscriber, you can add it here.
// It will only receive subsequent pubsub requests
// svc.Subscribe(sub2)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandOption

type CommandOption interface {
	Option
	// contains filtered or unexported methods
}

func WithAck

func WithAck(b bool) CommandOption

WithAck specifies if the command method such as Subscribe() should block to wait for an ack from the Service

type Loopback

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

Loopback is used for in-memory broadcasting and debugging. Whatever you send to it, it loops back to the pubsub.Service

func NewLoopback

func NewLoopback(svc *Service) *Loopback

func (*Loopback) Send

func (l *Loopback) Send(v interface{}) error

type Option

type Option = option.Interface

type Service

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

Service is responsible for accepting a payload, and pubsubing it to all data sinks. It is expected that the data sinks do not block more than absolutely necessary

func (*Service) Receive

func (svc *Service) Receive(v interface{}, options ...CommandOption) error

Receive should only be used by whatever ingress service. When there is new data coming in from the ingress, this method can be used to pubsub the data to the subscribers

func (*Service) Run

func (svc *Service) Run(ctx context.Context) error

func (*Service) Subscribe

func (svc *Service) Subscribe(s Subscriber, options ...CommandOption) error

Subscribe registers a subscriber to receive pubsub messages

func (*Service) Unsubscribe

func (svc *Service) Unsubscribe(s Subscriber, options ...CommandOption) error

Unsubscribe unregisters a previously registered subscriber

type SubscribeFunc

type SubscribeFunc func(interface{}) error

func (SubscribeFunc) Equal

func (s SubscribeFunc) Equal(another Subscriber) bool

func (SubscribeFunc) Receive

func (s SubscribeFunc) Receive(v interface{}) error

type Subscriber

type Subscriber interface {
	Receive(interface{}) error
}

Directories

Path Synopsis
examples
grpc module

Jump to

Keyboard shortcuts

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