Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 3 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 (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T07:48:34.000Z (3 months ago)
- Last Synced: 2024-10-29T08:30:34.167Z (3 months ago)
- Topics: fs2, scala, stream, throttle, throttler, throttling
- Language: Scala
- Homepage:
- Size: 144 KB
- Stars: 19
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
FS2 Throttler
====
[![CI Status](https://github.com/kovstas/fs2-throttler/workflows/Build/badge.svg)](https://github.com/kovstas/fs2-throttler/actions)
[![Maven Central](https://img.shields.io/maven-central/v/dev.kovstas/fs2-throttler_2.13.svg)](http://search.maven.org/#search%7Cga%7C1%7Cfs2-throttler)![Cats Friendly Badge](https://typelevel.org/cats/img/cats-badge-tiny.png)
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.