broadcaster

package module
v0.0.0-...-e104bc4 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2016 License: MIT Imports: 3 Imported by: 0

README

Package broadcaster provides a way to send events to multiple listeners.

It offers a normal broadcaster and a broadcaster that keeps a short history of events to update new listeners.

Godocs?

Godoc!

Presentation

I present to you Broadcaster:

b := broadcaster.New() // create a broadcaster
b.Send(event)          // send an event to listeners
l := b.Listen()        // create a new listener
b.Close()              // close the broadcast, release the listeners

and Listener:

l := b.Listen()       // create a listener
event, ok := l.Next() // block for the next event
l.Close()             // stop listening

Usage

Here's how you can use them:

b := broadcaster.New()
b.Send("Possibly nobody will get this")

for i := 0; i < listenCount; i++ {
  l := b.Listen()
  go func() {
    defer l.Close()
    for {
      e, ok := l.Next()
      if !ok {
        // broadcast stopped!
        return
      }
      fmt.Println(e)
    }
  }()
}

time.Sleep(time.Second * 1)
b.Close()

Listeners can be closed to announce that they do not wish to receive further events.

l := b.Listen()

b.Send("hello!") // l receives this one

l.Close()

b.Send("hello?") // l is not listening anymore
b.Close()

And broadcasters can announce to their listeners that no further events will be sent (and therefore the listeners should be released).

l := b.Listen()
b.Close()

go func() {
  defer l.Close()
  _, ok := l.Next()
  if !ok {
    return // the broadcaster closed
  }
}()

It's pretty simple!

Two variants

There are two implementations. The first is a broadcaster that sends the events that it receives to all its listeners, as you would expect.

b := broadcaster.New()
b.Send("Possibly nobody will get this")

l1 := b.Listen()
defer l1.Close()

b.Send("Hello l1")

l2 := b.Listen()
defer l2.Close()

b.Send("Hello l1 and l2")

b.Close()

The second does the same thing, but also keeps an history of the last events and brings up to date listeners that just subscribed.

b := broadcaster.NewBacklog(3)
b.Send("Everybody will get this")

l1 := b.Listen()
defer l1.Close()

b.Send("Hello l1, hello future l2")

l2 := b.Listen()
defer l2.Close()

b.Send("Hello l1 and l2")

b.Close()

License

MIT.

Documentation

Overview

Package broadcaster provides a way to send events to multiple listeners. Listeners can be closed to announce that they do not wish to receive further events, and broadcasters can announce to their listeners that no further events will be sent (and therefore the listeners should be released).

There are two implementations. The first is a broadcaster that sends the events that it receives to all its listeners.

The second does the same thing, but also keeps an history of the last events and brings up to date listeners that just subscribed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broadcaster

type Broadcaster interface {
	// Close the broadcaster goroutine and stop broadcasting. Can only be
	// be called once.
	Close()
	// Send an Event to all the listeners. Sending on a closed broacaster
	// will panic.
	Send(ctx context.Context, e Event)
	// Listen returns a listener for this broadcaster.
	Listen() Listener
}

Broadcaster sends Events it receives to all its listeners.

Failing to close a broadcaster that is not needed anymore is a memory leak.

func New

func New() Broadcaster

New broadcaster that sends Events in its internal goroutine. It does not preserve a backlog and will not attempt to update new listeners.

func NewBacklog

func NewBacklog(backlog int) Broadcaster

NewBacklog broadcaster that sends Events in its internal goroutine. When a new listener is given out, it will have an history of recent events.

type Event

type Event interface{}

Event that can be broadcasted.

type Listener

type Listener interface {
	// Next blocks until the next Event from the broadcaster. It returns
	// false if the broadcaster is closed.
	//
	// Calling Next after a call to Close is an error and will panic.
	Next(ctx context.Context) (Event, bool)
	// Close removes this listener from the broadcaster's listeners.
	Close()
}

Listener can read Events and be closed when the owner wishes to stop listening.

Failing to close a listener once you are done with it is a memory leak.

Jump to

Keyboard shortcuts

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