2020-08-18 16:19:53 +01:00
|
|
|
// Package events contains interfaces for managing events within distributed systems
|
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2020-11-03 02:02:32 +03:00
|
|
|
"context"
|
2020-08-18 16:19:53 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"time"
|
2020-11-18 16:50:41 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-08-18 16:19:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrMissingTopic is returned if a blank topic was provided to publish
|
|
|
|
ErrMissingTopic = errors.New("Missing topic")
|
|
|
|
// ErrEncodingMessage is returned from publish if there was an error encoding the message option
|
|
|
|
ErrEncodingMessage = errors.New("Error encoding message")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Stream of events
|
|
|
|
type Stream interface {
|
2020-11-03 02:02:32 +03:00
|
|
|
Publish(ctx context.Context, topic string, msg interface{}, opts ...PublishOption) error
|
|
|
|
Subscribe(ctx context.Context, topic string, opts ...SubscribeOption) (<-chan Event, error)
|
2020-08-18 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store of events
|
|
|
|
type Store interface {
|
2020-11-03 02:02:32 +03:00
|
|
|
Read(ctx context.Context, opts ...ReadOption) ([]*Event, error)
|
|
|
|
Write(ctx context.Context, event *Event, opts ...WriteOption) error
|
2020-08-18 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event is the object returned by the broker when you subscribe to a topic
|
|
|
|
type Event struct {
|
|
|
|
// ID to uniquely identify the event
|
|
|
|
ID string
|
|
|
|
// Topic of event, e.g. "registry.service.created"
|
|
|
|
Topic string
|
|
|
|
// Timestamp of the event
|
|
|
|
Timestamp time.Time
|
|
|
|
// Metadata contains the encoded event was indexed by
|
2020-11-18 16:50:41 +03:00
|
|
|
Metadata metadata.Metadata
|
2020-08-18 16:19:53 +01:00
|
|
|
// Payload contains the encoded message
|
|
|
|
Payload []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal the events message into an object
|
|
|
|
func (e *Event) Unmarshal(v interface{}) error {
|
|
|
|
return json.Unmarshal(e.Payload, v)
|
|
|
|
}
|