https://github.com/jechol/buffered
Buffered Queue and Counter for Elixir
https://github.com/jechol/buffered
Last synced: about 1 year ago
JSON representation
Buffered Queue and Counter for Elixir
- Host: GitHub
- URL: https://github.com/jechol/buffered
- Owner: jechol
- Created: 2020-09-18T03:32:31.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-10T09:47:26.000Z (about 2 years ago)
- Last Synced: 2025-02-15T07:12:14.768Z (over 1 year ago)
- Language: Elixir
- Size: 31.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README


# Buffered
Buffered queue and counter for Erlang/Elixir
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `buffered` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:buffered, "~> 0.4.0"}
]
end
```
## Usage
### Queue
```elixir
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
```elixir
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](https://github.com/jechol/buffered/blob/master/lib/buffered/queue.ex).