gomit

package module
v0.0.0-...-07f4a70 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: Apache-2.0 Imports: 4 Imported by: 78

README

DISCONTINUATION OF PROJECT.

This project will no longer be maintained by Intel.

This project has been identified as having known security escapes.

Intel has ceased development and contributions including, but not limited to, maintenance, bug fixes, new releases, or updates, to this project.

Intel no longer accepts patches to this project.

GoMit

Build Status

GoMit (short for "go emit") provides facilities for defining, emitting, and handling events within a Go program. It's used in Snap to simplify event handling. It's core principles are:

  • Speed over abstraction
  • No order guarantees
  • No persistence

Using GoMit

With Go installed, you can go get it:

$ go get -d github.com/intelsdi-x/gomit
Examples

From gomit_test.go:

type MockEventBody struct {
}

type MockThing struct {
	LastNamespace string
}

func (m *MockEventBody) Namespace() string {
	return "Mock.Event"
}
//create a function to handle the gomit event
func (m *MockThing) HandleGomitEvent(e Event) {
	m.LastNamespace = e.Namespace()
}

//create an event controller
event_controller := new(EventController)
//add registration to handler
mt := new(MockThing)
event_controller.RegisterHandler("m1", mt)
//emit event
eb := new(MockEventBody)
i, e := event_controller.Emit(eb)
//unregister handler
event_controller.UnregisterHandler("m1")
//check if handler is registered
b := event_controller.IsHandlerRegistered("m1")
Roadmap

GoMit does all we need it to do and we plan to keep it that simple. If you find a bug in your own usage, please let us know through an Issue.

Maintainers

The maintainers for GoMit are the same as Snap.

License

GoMit is an Open Source software released under the Apache 2.0 License.

Documentation

Overview

GoMit provides facilities for defining, emitting, and handling events within a Go service.

Core principles:

1. Speed over abstraction 2. No order guarantees 3. No persistence

Example
event_controller := new(EventController)
/*
	type Widget struct {
		EventCount int
	}

	func (w *Widget) HandleGomitEvent(e Event) {
		w.EventCount++
	}
*/
widget := new(Widget)

event_controller.RegisterHandler("widget1", widget)

event_controller.Emit(new(RandomEventBody))
event_controller.Emit(new(RandomEventBody))
event_controller.Emit(new(RandomEventBody))

time.Sleep(time.Millisecond * 100)
fmt.Println(widget.EventCount)
Output:

3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Delegator

type Delegator interface {
	RegisterHandler(string, Handler) error
	UnregisterHandler(string) error
	IsHandlerRegistered(string) bool
	HandlerCount() int
}

Represents something that takes Handler registrations and unregistrations and delegates event to handlers

type Emitter

type Emitter interface {
	Emit(EventBody) (int, error)
}

Represents something that emits events

type Event

type Event struct {
	Header EventHeader
	Body   EventBody
}

Represents an event emitted by an Emitter and handled by a Handler

func (*Event) Namespace

func (e *Event) Namespace() string

Provides a string Namespace for the Event. Namespace is open to implementation with the producers/consumers for routing.

type EventBody

type EventBody interface {
	Namespace() string
}

Represents the compatible event body provided by the producer.

type EventController

type EventController struct {
	Handlers map[string]Handler
	// contains filtered or unexported fields
}

Takes registration and unregistration of Handlers and emits Events to be handled by the Handlers.

func NewEventController

func NewEventController() *EventController

initializes an EventController with Handlers

func (*EventController) Emit

func (e *EventController) Emit(b EventBody) (int, error)

Emits an Event from the EventController. Takes an EventBody which is used to build an Event. Returns number of handlers that received the event and error if an error was raised.

func (*EventController) HandlerCount

func (e *EventController) HandlerCount() int

Return count (int) of Handlers

func (*EventController) IsHandlerRegistered

func (e *EventController) IsHandlerRegistered(n string) bool

Returns bool on whether the Handler is registered with this EventController.

func (*EventController) RegisterHandler

func (e *EventController) RegisterHandler(n string, h Handler) error

Registers Handler with the EventController. Takes a string for the unique name(key) and the handler that conforms the to Handler interface. The name(key) is used to unregister or check if registered.

func (*EventController) UnregisterHandler

func (e *EventController) UnregisterHandler(n string) error

Unregisters Handler from the EventController. This is idempotent where if a Handler is not registered no error is returned.

type EventHeader

type EventHeader struct {
	Time time.Time
}

Contains common data across all Event instances.

type Handler

type Handler interface {
	HandleGomitEvent(Event)
}

Something that handles the Events emitted by the EventController.

Jump to

Keyboard shortcuts

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