hub

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package hub describes event hubs that allow providers to broadcast events and allow consumers to receive them

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBatcher

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

func NewEventBatcher

func NewEventBatcher(batchInterval time.Duration) *EventBatcher

func (*EventBatcher) BatchedEvents

func (b *EventBatcher) BatchedEvents() chan struct{}

BatchedEvents allows consumers to receive batched events from a channel

func (*EventBatcher) Notify

func (b *EventBatcher) Notify()

Notify posts an event to the EventBatcher. If the EventBatcher is currently sleeping, then Notify will wake the EventBatcher and the EventBatcher will post a batched event after the batchInterval duration

type NotifyHub

type NotifyHub[T interface{}] struct {
	// contains filtered or unexported fields
}

NotifyHub is responsible for broadcasting generic events to subscribers Example usage:

notifyHub := hub.NewNotifyHub[string](20)
go func() {
	sub, id := notifyHub.Subscribe()
	defer notifyHub.Unsubscribe(id)
	for payload := range sub {
		fmt.Printf("Received payload %#v\n", payload)
	}
}()
time.Sleep(10 * time.Millisecond)
notifyHub.Broadcast("foobar")
time.Sleep(10 * time.Millisecond)

Expected output:

Received payload "foobar"

func NewNotifyHub

func NewNotifyHub[T interface{}](chanSize int) *NotifyHub[T]

NewNotifyHub creates a new hub for broadcasting events to subscribers

func (*NotifyHub[T]) Broadcast

func (h *NotifyHub[T]) Broadcast(payload T)

Broadcast sends the message to all subscribers of this hub

func (*NotifyHub[T]) Subscribe

func (h *NotifyHub[T]) Subscribe() (chan T, uint64)

Subscribe adds a new hub subscriber

func (*NotifyHub[T]) Unsubscribe

func (h *NotifyHub[T]) Unsubscribe(id uint64)

Unsubscribe removes a hub subscriber

Jump to

Keyboard shortcuts

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