event

package
v0.0.0-...-b49207b Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: MIT Imports: 3 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateId

type AggregateId string
const (
	AuthAggregate    AggregateId = "auth"
	UserAggregate    AggregateId = "user"
	ArticleAggregate AggregateId = "article"
	CommentAggregate AggregateId = "comment"
)

type Broker

type Broker interface {
	Consumer
	Publisher
}

type Consumer

type Consumer interface {
	Consume(ctx context.Context, queue string, eventType EventType) (<-chan Event, error)
}

type Event

type Event struct {
	AggregateId AggregateId       `json:"aggregate_id,omitempty"`
	Type        EventType         `json:"type,omitempty"`
	Body        []byte            `json:"body,omitempty"` // Must be marshaled to JSON.
	Timestamp   time.Time         `json:"timestamp,omitempty"`
	Metadata    map[string]string // TraceID etc.
}

Events are sent to the queue in JSON format.

func MakeEvent

func MakeEvent(aggregateId AggregateId, eType EventType, body interface{}, metadata map[string]string) (Event, error)

MakeEvent returns an event serialized for general use. Returns an error when given body cannot be marshaled into json.

type EventType

type EventType string

All event names must be lowercase and follow the structure: "{noun}-{action}". Eg. article-created, notification-sent, order-accepted. For longer names use snake-case naming. Eg. changed_password_notification-sent.

const (
	ArticleCreated EventType = "article-created"
	ArticleDeleted EventType = "article-deleted"
	ArticleUpdated EventType = "article-updated"

	UserCreated EventType = "user-created"
	UserDeleted EventType = "user-deleted"
	UserUpdated EventType = "user-updated"

	UserLoggedIn  EventType = "user-logged_in"
	UserLoggedOut EventType = "user-logged_out"

	KeySetUpdated EventType = "key_set-updated"
)

type Handler

type Handler interface {
	Handle(Event)
}

type HandlerFunc

type HandlerFunc func(Event)

func (HandlerFunc) Handle

func (fn HandlerFunc) Handle(event Event)

type Publisher

type Publisher interface {

	// Exchanges and queues are maintained internally depending on the type of event.
	Publish(context.Context, Event) error

	// Resilient publish returns only parsing error and on any other error retries each event until it succeeds.
	ResilientPublish(Event) error
}

type Subscriber

type Subscriber interface {
	// Subscribe registers an event handler for sepcified types of events.
	Subscribe(Handler, ...EventType)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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