https://github.com/kovstas/fs2-throttler
Throttling for FS2 based on the Token bucket algorithm.
https://github.com/kovstas/fs2-throttler
fs2 scala stream throttle throttler throttling
Last synced: about 2 months ago
JSON representation
Throttling for FS2 based on the Token bucket algorithm.
- Host: GitHub
- URL: https://github.com/kovstas/fs2-throttler
- Owner: kovstas
- License: mit
- Created: 2021-07-04T17:59:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-18T15:22:03.000Z (2 months ago)
- Last Synced: 2025-03-21T14:22:41.166Z (2 months ago)
- Topics: fs2, scala, stream, throttle, throttler, throttling
- Language: Scala
- Homepage:
- Size: 146 KB
- Stars: 18
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
FS2 Throttler
====
[](https://github.com/kovstas/fs2-throttler/actions)
[](http://search.maven.org/#search%7Cga%7C1%7Cfs2-throttler)
Throttling for [FS2](https://fs2.io) based on the [Token bucket](https://en.wikipedia.org/wiki/Token_bucket) algorithm.
This implementation supports:
- burst in the processing of elements
- calculates a cost for every element of the stream
- two throttle modes (Shaping / Enforcing)## Install
Add the following to your `build.sbt` file:
```scala
libraryDependencies += "dev.kovstas" %% "fs2-throttler" % Version
```## Usage
To use the throttler, import the throttle function and apply it to your stream:
```scala
import cats.effect.IO
import fs2.Stream
import scala.concurrent.duration._
import dev.kovstas.fs2throttler.Throttler._val stream = Stream(1, 2, 3, 4, 5)
val shapedStream = stream.through(throttle(2, 1.second, Shaping))
val enforcedStream = stream.through(throttle(2, 1.second, Enforcing))val costFunction: Int => Long = i => i.toLong
val throttledCostStream = stream.through(throttle(2, 1.second, Shaping, costFunction))
```
For more examples, please refer to the tests.