Skip to content

Latest commit

 

History

History
67 lines (47 loc) · 1.45 KB

README.md

File metadata and controls

67 lines (47 loc) · 1.45 KB

mix test Hex.pm

Buffered

Buffered queue and counter for Erlang/Elixir

Installation

If available in Hex, the package can be installed by adding buffered to your list of dependencies in mix.exs:

def deps do
  [
    {:buffered, "~> 0.4.0"}
  ]
end

Usage

Queue

alias Buffered.Queue
{:ok, pid} = Queue.start_link(%{size: 2, timeout: 3000}, &IO.inspect/1)

Queue.enqueue(pid, [1])
Queue.enqueue(pid, [2, 3])
# [1, 2, 3] immediately as size of [1, 2, 3] > size 2

Queue.enqueue(pid, [4])
Process.sleep(5000)
# [4] as ellapsed time 5000ms > timeout 3000ms

Queue.enqueue(pid, [5])
Queue.flush(pid)
# [5] due to flush

Counter

alias Buffered.Counter
{:ok, pid} = Counter.start_link(%{start: 100, threshold: 10, timeout: 3000}, &IO.inspect/1)

Counter.add(pid, 9)
Counter.add(pid, 2)
# 111 immediately as change 11 > threshold 10

Counter.add(pid, 2)
Process.sleep(5000)
# 113 as ellapsed time 5000ms > timeout 3000ms

Counter.add(pid, -8)
Counter.flush(pid)
# 105 due to flush

How to extend?

Both of Buffered.Counter, Buffered.Queue are just customization of Buffered.

Defining append/2, overflow?/1, reset/1 for your own data is enough.

See how Buffered.Queue is implemented here.