Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/esl/opuntia
Opuntia is a basic set of tools for traffic shaping for erlang and elixir. Because if you touch it too fast, it stings you.
https://github.com/esl/opuntia
elixir erlang shaper traffic-shaping
Last synced: 2 months ago
JSON representation
Opuntia is a basic set of tools for traffic shaping for erlang and elixir. Because if you touch it too fast, it stings you.
- Host: GitHub
- URL: https://github.com/esl/opuntia
- Owner: esl
- License: apache-2.0
- Created: 2024-01-02T10:02:57.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-03-08T12:19:25.000Z (10 months ago)
- Last Synced: 2024-09-30T03:41:56.742Z (3 months ago)
- Topics: elixir, erlang, shaper, traffic-shaping
- Language: Erlang
- Homepage: https://hexdocs.pm/opuntia/readme.html
- Size: 46.9 KB
- Stars: 5
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `opuntia`
[![Actions Status](https://github.com/esl/opuntia/actions/workflows/ci.yml/badge.svg)](https://github.com/esl/opuntia/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/esl/opuntia/branch/main/graph/badge.svg)](https://codecov.io/gh/esl/opuntia)
[![Hex](http://img.shields.io/hexpm/v/opuntia.svg)](https://hex.pm/packages/opuntia)`opuntia` is a basic set of tools for traffic shaping for erlang and elixir
It implements the [token bucket algorithm](https://en.wikipedia.org/wiki/Token_bucket).
There are two ways to use it, checking availability a priori or accepting a penalisation.
After creating a bucket
```erl
Bucket = opuntia:new(#{bucket_size => 10, rate => 1, time_unit => millisecond, start_full => true}),
```
you can either consume all tokens queued and see the suggested delay, considering that this might
allow you to consume at once much more than the bucket size:
```erl
{NewShaper, Delay} = opuntia:update(Shaper, 50),
timer:sleep(Delay), %% Will suggest to sleep 40ms
```
or you can first how many tokens are available for you to consume before doing so:
```erl
Allowed = opuntia:peek(Shaper),
consume_tokens(Allowed),
{NewShaper, 0} = opuntia:update(Shaper), %% Will suggest no delay if you were diligent and consume less that adviced
```