broadcast

package module
Version: v0.0.0-...-57bd5bb Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 0 Imported by: 449

README

pubsubbing channels.

This project primarily exists because I've been copying and pasting the exact same two files into numerous projects. It does work well, though.

See the documentation for usage and examples.

Documentation

Overview

Package broadcast provides pubsub of messages over channels.

A provider has a Broadcaster into which it Submits messages and into which subscribers Register to pick up those messages.

Example

Example of a simple broadcaster sending numbers to two workers.

Five messages are sent. The first worker prints all five. The second worker prints the first and then unsubscribes.

package main

import "log"

// Example of a simple broadcaster sending numbers to two workers.
//
// Five messages are sent.  The first worker prints all five.  The second worker prints the first and then unsubscribes.
func main() {
	b := NewBroadcaster(100)

	workerOne(b)
	workerTwo(b)

	for i := 0; i < 5; i++ {
		log.Printf("Sending %v", i)
		b.Submit(i)
	}
	b.Close()
}

func workerOne(b Broadcaster) {
	ch := make(chan interface{})
	b.Register(ch)
	defer b.Unregister(ch)

	// Dump out each message sent to the broadcaster.
	go func() {
		for v := range ch {
			log.Printf("workerOne read %v", v)
		}
	}()
}

func workerTwo(b Broadcaster) {
	ch := make(chan interface{})
	b.Register(ch)
	defer b.Unregister(ch)
	defer log.Printf("workerTwo is done\n")

	go func() {
		log.Printf("workerTwo read %v\n", <-ch)
	}()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broadcaster

type Broadcaster interface {
	// Register a new channel to receive broadcasts
	Register(chan<- interface{})
	// Unregister a channel so that it no longer receives broadcasts.
	Unregister(chan<- interface{})
	// Shut this broadcaster down.
	Close() error
	// Submit a new object to all subscribers
	Submit(interface{})
	// Try Submit a new object to all subscribers return false if input chan is fill
	TrySubmit(interface{}) bool
}

The Broadcaster interface describes the main entry points to broadcasters.

func NewBroadcaster

func NewBroadcaster(buflen int) Broadcaster

NewBroadcaster creates a new broadcaster with the given input channel buffer length.

type MuxObserver

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

A MuxObserver multiplexes several streams of observations onto a single delivery goroutine.

func NewMuxObserver

func NewMuxObserver(qlen, reglen int) *MuxObserver

NewMuxObserver constructs a new MuxObserver.

qlen is the size of the channel buffer for observations sent into the mux observer and reglen is the size of the channel buffer for registration/unregistration events.

func (*MuxObserver) Close

func (m *MuxObserver) Close() error

Close shuts down this mux observer.

func (*MuxObserver) Sub

func (m *MuxObserver) Sub() Broadcaster

Sub creates a new sub-broadcaster from this MuxObserver.

Jump to

Keyboard shortcuts

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