geb

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultMaxElapsedTime      = 1 * time.Minute
	DefaultMaxInterval         = 5 * time.Second
	DefaultRandomizationFactor = 0.5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(*Event) error

Callback is signature for the callback method passed to OnEvent.

type Codec

type Codec interface {
	// Name returns the name of the encoder, for debugging purposes.
	Name() string
	// NewEvent returns an empty event that can be marshalled into.
	NewEvent() codecEvent
	// Encode marshals the event into a byte stream.
	Encode(codecEvent) ([]byte, error)
	// Decode unmarhals a byte stream into a codecEvent.
	Decode(data []byte) (codecEvent, error)
}

Codec is an interface for marshaling/unmarshaling event body and headers.

func JSONCodec

func JSONCodec(opts ...CodecOption) Codec

JSONCodec returns a codec that marshals both headers and body into a json object.

func RawCodec

func RawCodec() Codec

RawCodec does not support headers, it simply sends the body as-is.

type CodecOption

type CodecOption func(*codecSettings)

func UseTag

func UseTag(tag string) CodecOption

UseTag option can be used to change the struct tag used for marshaling/unmarshaling (default is json).

type Event

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

Event is the public representation of a geb event, used for both Publish and OnEvent.

func (*Event) Context

func (e *Event) Context() context.Context

Context returns the event's context. Publish and OnEvent does NOT use this context, meaning that context cancel does NOT work. Middlewares should communicate through the context.

func (*Event) EventName

func (e *Event) EventName() string

EventName returns the name of the event, for debugging purposes.

func (*Event) SetContext

func (e *Event) SetContext(ctx context.Context)

SetContext can be used for setting the event's context. Publish and OnEvent does NOT use this context, meaning that context cancel does NOT work. Middlewares should communicate through the context.

type ExponentialBackoff

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

func NewExponentialBackOff

func NewExponentialBackOff(maxElapsedTime, maxInterval time.Duration, randomizationFactor float64) *ExponentialBackoff

func (*ExponentialBackoff) NextBackOff

func (b *ExponentialBackoff) NextBackOff() (bool, time.Duration)

type Handler

type Handler interface {
	// Start must be called after OnError, but before any Publish/OnEvent calls are made.
	Start() error
	// Close can be called to gracefully close the queue. No new events will be processed,
	// but existing event processings will continue. Publishing on a closed handler will return with an error.
	Close() error
	// OnError callback is called when a non-event specific (not a marshaling/unmarshaling) error occurs.
	// eg: connection error
	OnError(cb func(err error)) error
	// OnEvent for msgpack and json codecs, either 'codec' or 'json' tags may be used
	OnEvent(eventName string, callback func(payload []byte) error, options OnEventOptions) error
	// Publish for msgpack and json codecs, either 'codec' or 'json' tags may be used
	Publish(eventName string, payload []byte) error
}

Handler is an interface for the actual messaging implementation.

type Middleware

type Middleware func(e *Event, next func(*Event) error) error

Middleware is the signature of middlewares. Middlewares are layered (like an onion), meaning that the last added middleware will be executed first. You should always call next() from the middleware. If next() is not called, none of the inner (previously added) middlewares will run.

func RecoveryMiddleware

func RecoveryMiddleware() Middleware

func RetryMiddleware

func RetryMiddleware() Middleware

type OnEvent

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

OnEvent is the struct for building OnEvent subscriptions.

func (*OnEvent) Codec

func (oe *OnEvent) Codec(c Codec) *OnEvent

Codec can be used to set the codec for the OnEvent. By default the codec is inherited from the queue.

func (*OnEvent) Listen

func (oe *OnEvent) Listen(cb Callback) error

Listen starts the OnEvent subscription, using the configuration and middlewares stored in the OnEvent object. All middlewares MUST be registered before calling Listen.

func (*OnEvent) Use

func (oe *OnEvent) Use(m Middleware) *OnEvent

Use a middleware for this specific OnEvent processing. Also see: geb.Middleware.

type OnEventOption

type OnEventOption func(oe *OnEventOptions)

func MaxGoroutines

func MaxGoroutines(maxGoroutines int) OnEventOption

MaxGoroutines sets the maximum number of concurrent OnEvent callbacks for a given eventName. Default is 1.

type OnEventOptions

type OnEventOptions struct {
	MaxGoroutines int
}

OnEventOptions contains the options for event subscriptions (should be supported by Handler implementations.) This should NOT be used directly, instead use methods with OnEventOption return type.

type Publish

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

Publish is the struct for preparing publishes.

func (*Publish) Body

func (p *Publish) Body(v interface{}) *Publish

Body sets the event's body.

func (*Publish) Codec

func (p *Publish) Codec(codec Codec) *Publish

Codec can be used to set the codec for the Publish. By default the codec is inherited from the queue.

func (*Publish) Context

func (p *Publish) Context(ctx context.Context) *Publish

Context sets the Publish context. It runs before any middlewares, so all middlewares will have access to this context.

func (*Publish) Do

func (p *Publish) Do() error

Do publishes the event, using the configuration and middlewares stored in the Publish object. All middlewares MUST be registered before calling Do.

func (*Publish) Headers

func (p *Publish) Headers(headers map[string]string) *Publish

Headers sets the event's headers.

func (*Publish) Use

func (p *Publish) Use(m Middleware) *Publish

Use a middleware for this specific Publish. Also see: geb.Middleware.

type Queue

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

Queue is the main struct for the geb client. You can publish/listen to multiple event types on a single queue.

func NewQueue

func NewQueue(handler Handler, codec Codec) *Queue

func (*Queue) Close

func (q *Queue) Close() error

Close can be called to gracefully close the queue. No new events will be processed, but existing event processings will continue. Publishing on a closed queue will return with an error.

func (*Queue) OnError

func (q *Queue) OnError(callback func(err error)) error

OnError callback is called when a non-event specific (not a marshaling/unmarshaling) error occurs. eg: connection error

func (*Queue) OnEvent

func (q *Queue) OnEvent(eventName string, options ...OnEventOption) *OnEvent

OnEvent returns a builder for an OnEvent subscription. Add all the necessary middlewares, then call Listen(callback).

func (*Queue) Publish

func (q *Queue) Publish(eventName string) *Publish

Publish returns a builder for a Publish call. Add all the necessary middlewares, then call Do() for actually publishing the event.

func (*Queue) Start

func (q *Queue) Start() error

Start must be called after OnError, but before any Publish/OnEvent calls are made.

func (*Queue) UseOnEvent

func (q *Queue) UseOnEvent(m Middleware) *Queue

UseOnEvent adds a middleware for all OnEvent processings. To add a middleware to a specific OnEvent only, call OnEvent.Use() instead. Also see: geb.Middleware.

func (*Queue) UsePublish

func (q *Queue) UsePublish(m Middleware) *Queue

UsePublish adds a middleware for all Publish calls. To add a middleware to a specific Publish only, call Publish.Use() instead. Also see: geb.Middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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