akka-throttled


License

License

MIT
Categories

Categories

Akka Container Microservices Reactive libraries
GroupId

GroupId

com.mehmetyucel
ArtifactId

ArtifactId

akka-throttled_2.12
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

akka-throttled
akka-throttled
Project URL

Project URL

https://github.com/baaa/akka-throttled
Project Organization

Project Organization

com.mehmetyucel
Source Code Management

Source Code Management

https://github.com/baaa/akka-throttled

Download akka-throttled_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.mehmetyucel/akka-throttled_2.12/ -->
<dependency>
    <groupId>com.mehmetyucel</groupId>
    <artifactId>akka-throttled_2.12</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.mehmetyucel/akka-throttled_2.12/
implementation 'com.mehmetyucel:akka-throttled_2.12:0.0.2'
// https://jarcasting.com/artifacts/com.mehmetyucel/akka-throttled_2.12/
implementation ("com.mehmetyucel:akka-throttled_2.12:0.0.2")
'com.mehmetyucel:akka-throttled_2.12:jar:0.0.2'
<dependency org="com.mehmetyucel" name="akka-throttled_2.12" rev="0.0.2">
  <artifact name="akka-throttled_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.mehmetyucel', module='akka-throttled_2.12', version='0.0.2')
)
libraryDependencies += "com.mehmetyucel" % "akka-throttled_2.12" % "0.0.2"
[com.mehmetyucel/akka-throttled_2.12 "0.0.2"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.2
com.typesafe : config jar 1.3.1
com.github » bucket4j jar 1.3.1
com.typesafe.akka : akka-actor_2.12 jar 2.5.12

test (3)

Group / Artifact Type Version
com.typesafe.akka : akka-testkit_2.12 jar 2.5.12
org.scalactic : scalactic_2.12 jar 3.0.0
org.scalatest : scalatest_2.12 jar 3.0.0

Project Modules

There are no modules declared in this project.

akka-throttled

Vanilla akka actor for limiting the rate an actor processes messages.

example

class Foo extends ThrottledActor(
  ThrottleConfig().perSecond(1, 5) // rate limit to 1 message per 5 seconds
) {
  override def receive: Receive = {
    case Bar(m) => println(s"${LocalDateTime.now()} processing a bar with $m")
    case other => println(s"${LocalDateTime.now()} unknown message $other")
  }
}

object RunMe extends App {
  implicit lazy val system       = ActorSystem("demo-actor-system")
  implicit      val ex           = ExecutionContext.Implicits.global

  val thr = system.actorOf(Props[Foo].withDispatcher("throttled-dispatcher"))

  thr ! ThrottledMessage(Bar("1"))
  thr ! ThrottledMessage(Bar("2"))
  thr ! "3"
  thr ! ThrottledMessage(Bar("4"))
  thr ! Bar("5")
  thr ! Bar("6")
  thr ! ThrottledMessage(Bar("7"))
  thr ! Bar("8")
  thr ! Bar("9")
  thr ! Bar("10")
  thr ! "11"

  println("actually sent everything")
}

case class Bar(m:String)

will produce:

actually sent everything
2018-05-25T12:56:50.696 unknown message 3
2018-05-25T12:56:50.698 processing a bar with 5
2018-05-25T12:56:50.698 processing a bar with 6
2018-05-25T12:56:50.698 processing a bar with 8
2018-05-25T12:56:50.698 processing a bar with 9
2018-05-25T12:56:50.698 processing a bar with 10
2018-05-25T12:56:50.699 unknown message 11
2018-05-25T12:56:50.699 processing a bar with 1
2018-05-25T12:56:55.701 processing a bar with 2
2018-05-25T12:57:00.704 processing a bar with 4
2018-05-25T12:57:05.709 processing a bar with 7

It is possible to create multiple limits. If any of the created quotas for these limits are reached, actor won't process any further messages until new quota becomes available.

For example

  ThrottleConfig()
    .perSecond(100)
    .perHour(1000)  

above config will create two limits 100 messages per second and 1000 messages per hour which means if you process 100 messages for 10 consecutive seconds, no messages will be processed for the next 59 minutes 50 seconds because the hourly quota is reached.

message not wrapped with ThrottledMessage are not throttled and do not effect quotas.

Versions

Version
0.0.2