fader

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

Documentation Go Report Card Build Status License

Fader

In-Memory storage that distributes items via UDP multicast.

Interface

type Fader interface {
    Put([]byte, time.Time, []byte) error
    Get([]byte) (time.Time, []byte)
    Earliest() ([]byte, time.Time, []byte)
    Select([]byte) [][]byte
    Size() int
    Close() error
}

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)
defer memoryFader.Close()

memoryFader.Put([]byte("key"), time.Now(), []byte("value"))
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", key)
defer multicastFaderOne.Close()

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

multicastFaderOne.Put([]byte("key"), time.Now(), []byte("value"))
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

Package fader 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)
defer memoryFader.Close()

memoryFader.Put([]byte("key"), time.Now(), []byte("value"))
memoryFader.Size() // => 1

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

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

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

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

multicastFaderOne.Put([]byte("key"), time.Now(), []byte("value"))
multicastFaderOne.Size() // => 1

time.Sleep(10*time.Millisecond)

multicastFaderTwo.Size() // => 1

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidKeyLength = errors.New("invalid key length")

ErrInvalidKeyLength is returns if a key with an invalid length is provided. Valid lengths are 16, 24 and 32.

Functions

This section is empty.

Types

type Fader

type Fader interface {
	Put([]byte, time.Time, []byte) error
	Get([]byte) (time.Time, []byte)
	Earliest() ([]byte, time.Time, []byte)
	Select([]byte) ([]time.Time, [][]byte)
	Size() int
	Clear()
	Close() error
}

Fader defines the fader interface.

type Memory

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

Memory implements a memory fader.

func NewMemory

func NewMemory(expiresIn time.Duration) *Memory

NewMemory 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 (*Memory) Clear added in v0.3.0

func (m *Memory) Clear()

Clear removes all items from the fader.

func (*Memory) Close

func (m *Memory) Close() error

Close tears down the fader.

func (*Memory) Earliest

func (m *Memory) Earliest() ([]byte, time.Time, []byte)

Earliest returns key, time and value of the earliest item in the fader.

func (*Memory) Get

func (m *Memory) Get(key []byte) (time.Time, []byte)

Get returns time and value for the provided key. If no such key exists, a value of nil is returned.

func (*Memory) Put

func (m *Memory) Put(key []byte, t time.Time, value []byte) error

Put places an item with the provided key, time and value in the fader.

func (*Memory) Select

func (m *Memory) Select(key []byte) ([]time.Time, [][]byte)

Select returns all times and values with the provided key.

func (*Memory) Size

func (m *Memory) Size() int

Size returns the number of items in the fader.

type Multicast

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

Multicast implements a multicast faader.

func NewMulticast

func NewMulticast(
	parent Fader,
	address string,
	key []byte,
	id []byte,
	itemReceivedHandler ReceivedHandler,
) (*Multicast, error)

NewMulticast 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. A 10-byte long id can be set to avoid collisions. 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.

func (*Multicast) Clear added in v0.3.0

func (m *Multicast) Clear()

Clear performs a clear on the parent fader.

func (*Multicast) Close

func (m *Multicast) Close() error

Close tears down the fader.

func (*Multicast) Earliest

func (m *Multicast) Earliest() ([]byte, time.Time, []byte)

Earliest returns key, time and value of the earliest item in the fader.

func (*Multicast) Get

func (m *Multicast) Get(key []byte) (time.Time, []byte)

Get returns time and value for the provided key. If no such key exists, a value of nil is returned.

func (*Multicast) Put

func (m *Multicast) Put(key []byte, time time.Time, value []byte) error

Put places an item with the provided key, time and value in the fader.

func (*Multicast) Select

func (m *Multicast) Select(key []byte) ([]time.Time, [][]byte)

Select returns all times and values with the provided key.

func (*Multicast) Size

func (m *Multicast) Size() int

Size returns the number of items in the fader.

type ReceivedHandler

type ReceivedHandler func([]byte, time.Time, []byte) bool

ReceivedHandler defines a handler for received items.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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