Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the timeout in composed futures?

Tags:

scala

akka

actor

In the example from the akka 1.1 documentation on composing futures, I'm wondering how it's possible to programmatically set the timeout of the generated future. I'm aware that I can adjust the global timeout in akka.conf, but I want to do it in place only for this piece of code.

The example code looks as follows

val f1 = actor1 !!! msg1
val f2 = actor2 !!! msg2

val f3 = for {
   a: Int    <- f1
   b: Int    <- f2
   c: String <- actor3 !!! (a + b)
} yield c

val result = f3.get()

Am I right that akka creates a total of four futures in this example?

  • One for each message send to actor1, 2 and 3
  • One to wrap these three futures

It's easy to change the timeouts in the first case, e.g.

val f1 = actor1 !!! (msg1, 15000)  // sets timeout to 15 seconds

but how can I Set the timeout for the wrapping future? Any ideas?

like image 255
Steffen Avatar asked Jul 13 '11 18:07

Steffen


1 Answers

There would actually be 6 futures total I believe, the 3 you got from the actors, and 1 for each flatMap/map method.

The timeout for the 3 actor futures are set just like you stated, but the 3 wrapper futures will inherit the timeout from the future on the right hand side of each section of the for-comprehension. However, the returned future will use the timeout of the first future in the for-comprehension.

For Akka 2.0 this will change to allow an implicit timeout to be specified, as long as no issues pop up during testing.

like image 181
derekjw Avatar answered Nov 08 '22 21:11

derekjw