event

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TimestampLength is the length in bytes it takes to serialize a
	// timestamp with nanosecond precision.
	TimestampLength = 8
)

Variables

This section is empty.

Functions

func MakeUniqueTimestamps

func MakeUniqueTimestamps(events []Event)

MakeUniqueTimestamps takes a slice of event records, sorts it by the event timestamps and then makes sure there are no duplicates in the timestamps. If duplicates are found, some of the timestamps are increased on the nanosecond scale until only unique values remain.

func StoreEvent

func StoreEvent(bucket *bbolt.Bucket, event Event) error

StoreEvent tries to store an event into the given bucket by trying to avoid collisions. If a key for the event timestamp already exists in the database, the timestamp is incremented in nanosecond intervals until a "free" slot is found. The given scratch space byte slice should ideally point to an array to avoid allocating new memory with each iteration. The value found in it after the function returns with a nil-error is the actual storage key that was used.

NOTE: When storing many events at the same time, the MakeUniqueTimestamps function should be called first to ensure the timestamps are already unique among themselves. If no "gap" in the timestamps can be found after 1000 tries an error is returned.

Types

type Event

type Event interface {
	// Type returns the type of the event.
	Type() Type

	// Timestamp is the time the event happened. This will be made unique
	// once it is stored. To avoid collisions, the timestamp is adjusted on
	// the nanosecond scale to reach uniqueness.
	Timestamp() time.Time

	// SetTimestamp updates the timestamp of the event. This is needed to
	// adjust timestamps in case they collide to ensure the global
	// uniqueness of all event timestamps.
	SetTimestamp(time.Time)

	// String returns a human readable representation of the event.
	String() string

	// Serialize writes the event data to a binary storage format. This does
	// not serialize the event type as that's handled generically to allow
	// for easy filtering.
	Serialize(io.Writer) error

	// Deserialize reads the event data from a binary storage format. This
	// does not deserialize the event type as that's handled generically to
	// allow for easy filtering.
	Deserialize(io.Reader) error
}

Event is the main interface all events have to implement.

type Predicate

type Predicate func(time.Time, Type) bool

Predicate is a function type that can be used to filter events. It gets the timestamp and type of an event passed in and can return whether that event is relevant or not.

type Type

type Type uint8

Type denotes the type of an event. The numeric representation of the types must be unique, that's why we define all of them in this package. But the actual implementation is left to the business package.

const (
	// TypeAny denotes no specific type and should only be used for querying
	// the event database, not as an actual event type.
	TypeAny Type = 0

	// TypeOrderCreated is the type of event that is emitted when an order
	// is first created.
	TypeOrderCreated Type = 1

	// TypeOrderStateChange is the type of event that is emitted when an
	// order changes its state in the order database due to it being
	// modified.
	TypeOrderStateChange Type = 2

	// TypeOrderMatch is the type of event that is emitted when an order is
	// being matched in a batch. An event of this type only denotes a
	// participation in a batch attempt and not necessarily final inclusion.
	// If a reject happens for any reason, the order might not make it to
	// the final batch and not all match states would therefore be present.
	TypeOrderMatch Type = 3
)

Jump to

Keyboard shortcuts

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