eventqueue

package
v1.13.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2022 License: Apache-2.0 Imports: 10 Imported by: 15

Documentation

Overview

Package eventqueue implements a queue-based system for event processing in a generic fashion in a first-in, first-out manner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Metadata is the information about the event which is sent
	// by its queuer. Metadata must implement the EventHandler interface in
	// order for the Event to be successfully processed by the EventQueue.
	Metadata EventHandler
	// contains filtered or unexported fields
}

Event is an event that can be enqueued onto an EventQueue.

func NewEvent

func NewEvent(meta EventHandler) *Event

NewEvent returns an Event with all fields initialized.

func (*Event) WasCancelled

func (ev *Event) WasCancelled() bool

WasCancelled returns whether the cancelled channel for the given Event has been closed or not. Cancellation occurs if the event was not processed yet by an EventQueue onto which this Event was Enqueued, and the queue is closed, or if the event was attempted to be scheduled onto an EventQueue which has already been closed.

type EventHandler

type EventHandler interface {
	Handle(chan interface{})
}

EventHandler is an interface for allowing an EventQueue to handle events in a generic way. To be processed by the EventQueue, all event types must implement any function specified in this interface.

type EventQueue

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

EventQueue is a structure which is utilized to handle Events in a first-in, first-out order. An EventQueue may be closed, in which case all events which are queued up, but have not been processed yet, will be cancelled (i.e., not ran). It is guaranteed that no events will be scheduled onto an EventQueue after it has been closed; if any event is attempted to be scheduled onto an EventQueue after it has been closed, it will be cancelled immediately. For any event to be processed by the EventQueue, it must implement the `EventHandler` interface. This allows for different types of events to be processed by anything which chooses to utilize an `EventQueue`.

func NewEventQueue

func NewEventQueue() *EventQueue

NewEventQueue returns an EventQueue with a capacity for only one event at a time.

func NewEventQueueBuffered

func NewEventQueueBuffered(name string, numBufferedEvents int) *EventQueue

NewEventQueueBuffered returns an EventQueue with a capacity of, numBufferedEvents at a time, and all other needed fields initialized.

func (*EventQueue) Enqueue

func (q *EventQueue) Enqueue(ev *Event) (<-chan interface{}, error)

Enqueue pushes the given event onto the EventQueue. If the queue has been stopped, the Event will not be enqueued, and its cancel channel will be closed, indicating that the Event was not ran. This function may block if the queue is at its capacity for events. If a single Event has Enqueue called on it multiple times asynchronously, there is no guarantee as to which one will return the channel which passes results back to the caller. It is up to the caller to check whether the returned channel is nil, as waiting to receive on such a channel will block forever. Returns an error if the Event has been previously enqueued, if the Event is nil, or the queue itself is not initialized properly.

func (*EventQueue) Run

func (q *EventQueue) Run()

Run consumes events that have been queued for this EventQueue. It is presumed that the eventQueue is a buffered channel with a length of one (i.e., only one event can be processed at a time). All business logic for handling queued events is contained within this function. The events in the queue must implement the EventHandler interface. If the event queue is closed, then all events which were queued up, but not processed, are cancelled; any event which is currently being processed will not be cancelled.

func (*EventQueue) Stop

func (q *EventQueue) Stop()

Stop stops any further events from being processed by the EventQueue. Any event which is currently being processed by the EventQueue will continue to run. All other events waiting to be processed, and all events that may be enqueued will not be processed by the event queue; they will be cancelled. If the queue has already been stopped, this is a no-op.

func (*EventQueue) WaitToBeDrained

func (q *EventQueue) WaitToBeDrained()

WaitToBeDrained returns the channel which waits for the EventQueue to have been stopped. This allows for queuers to ensure that all events in the queue have been processed or cancelled. If the queue is nil, returns immediately.

Jump to

Keyboard shortcuts

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