fader

package module
v0.0.0-...-61ac082 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2014 License: Apache-2.0 Imports: 12 Imported by: 0

README

Fader

In-Memory storage that distributes items via UDP multicast.

Documentation is available at godoc.org

Interface

type Item interface {
    Key() string
    Time() time.Time
}

type Fader interface {
    Open() error
    Close() error

    Store(Item) error
    Earliest() Item
    Select(string) []Item
    Detect(string) Item
    Size() int
}

A remove function is missing, because every stored item is supposed to expire after a defined period of time. See the memory fader implementation for details.

Memory Fader

An implementation of the Fader interface, that stores all items in memory using container/heap. The constructor function takes a time.Duration that specifies the period after which a stored item is removed.

memoryFader := fader.NewMemory(1*time.Second)
memoryFader.Open()
defer memoryFader.Close()

memoryFader.Store(item)
memoryFader.Size() // => 1

time.Sleep(2*time.Second)
memoryFader.Size() // => 0

Multicast Fader

Another implementation of the Fader interface. It does not store item directly, but delegates them to a given parent instance. Additionally, every Store operation is converted into an UDP packet which is sent to the given multicast group. The packet is encrypted using the given key.

multicastFaderOne := fader.NewMulticast(memoryFaderOne, "224.0.0.1:1888", fader.DefaultKey)
multicastFaderOne.Open()
defer multicastFaderOne.Close()

multicastFaderTwo := fader.NewMulticast(memoryFaderTwo, "224.0.0.1:1888", fader.DefaultKey)
multicastFaderTwo.Open()
defer multicastFaderTwo.Close()

multicastFaderOne.Store(item)
multicastFaderOne.Size() // => 1

time.Sleep(10*time.Millisecond)

multicastFaderTwo.Size() // => 1

Contribution

Any contribution is welcome! Feel free to open an issue or do a pull request.

Documentation

Overview

This package provides an interface to store and fetch items. The implementation is responsible for the removal after a expiry period.

Example for a memory fader, that expires items after 2 seconds

memoryFader := fader.NewMemory(2*time.Second)
memoryFader.Open()
defer memoryFader.Close()

memoryFader.Store(item)
memoryFader.Size() // => 1

time.Sleep(3*time.Second)
memoryFader.Size() // => 0

The multicast fader can be used to distribute `Store` operations via a multicast group. Other instances that listen to the same group, will perform that operation on thier own, so that each instance end up with the same data.

multicastFaderOne := fader.NewMulticast(memoryFaderOne, "224.0.0.1:1888", fader.DefaultKey)
multicastFaderOne.Open()
defer multicastFaderOne.Close()

multicastFaderTwo := fader.NewMulticast(memoryFaderTwo, "224.0.0.1:1888", fader.DefaultKey)
multicastFaderTwo.Open()
defer multicastFaderTwo.Close()

multicastFaderOne.Store(item)
multicastFaderOne.Size() // => 1

time.Sleep(10*time.Millisecond)

multicastFaderTwo.Size() // => 1

Index

Constants

View Source
const (
	IDSize = 10
)

Variables

View Source
var (
	DefaultKey = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
)

Functions

This section is empty.

Types

type Fader

type Fader interface {
	Open() error
	Close() error

	Store(Item) error
	Earliest() Item
	Select(string) []Item
	Detect(string) Item
	Size() int
}

func NewMemory

func NewMemory(expiresIn time.Duration) Fader

Creates a Fader instance that stores all data in the memory. The expiresIn parameter defines after which period a stored item will be removed.

func NewMulticast

func NewMulticast(
	parent Fader,
	address string,
	key []byte,
	id []byte,
	itemReceivedHandler func(Item) bool,
) Fader

Creates a Fader instance that delegates all calls to a parent Fader instance. Additional to that, all store-operations are published to a multicast group which is specified by the given address. All packets will encrypted with AES-GCM using the given key. The length of the key's byte-slice, can be 16, 24 or 32 and will define if AES-128, AES-192 or AES-256 is used. For testing purposes, a 10-byte long id can be set. If no id is nil, a random id will be generated. The argument can take a function that is called every time an item is received. If the function is nil or returns true, the received item will be stored in the parent fader. Otherwise, the item will be dismissed.

type Item

type Item interface {
	Key() string
	Time() time.Time
}

This interface has to be implemented by any item that should be handled by the fader. The Key() function should return a string that identified the item. The provided Time() will help the fader to schedule the expiry of the item.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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