You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now imagine an infinite chain of such flatMaps, as in
defloop():Future[Unit] = {
// using *our* flatMap defined above
flatMap(step()) { _ =>
loop()
}
}
defstep():Future[Unit] =???
loop() will create an infinite chain of Promises, thus leaking resources.
This problem does not occur with Future's flatMap method, because it has a specialized implementation that avoids building up a chain of promises by re-linking the listeners. That's something not available to client code like the Libretto implementation.
Now, Libretto uses constructs with Promises like the one in the above implementation of flatMap, but not exactly flatMaps.
Suggestion for a correct reference implementation
Avoid Scala Futures altogether. Instead, introduce our own primitive that gives control over listeners. Also, it need not be as general purpose as Future, but can exploit Libretto specifics, such as having exactly one listener.
The text was updated successfully, but these errors were encountered:
The problem with the current
Future
-based implementationFuture
s don't support removing listeners. This leads to a couple of problems.No support for cancellation
There is no way to let a
Future
know that we are no longer interested in its result, not to mention to cascade that information to upstreamFuture
s.Resource leaks
Some recursive programs may lead to arbitrarily long chains of
Promise
s.To illustrate the problem, let's try to define our own
flatMap
method onFuture
:Now imagine an infinite chain of such
flatMap
s, as inloop()
will create an infinite chain ofPromise
s, thus leaking resources.This problem does not occur with
Future
'sflatMap
method, because it has a specialized implementation that avoids building up a chain of promises by re-linking the listeners. That's something not available to client code like the Libretto implementation.Now, Libretto uses constructs with
Promise
s like the one in the above implementation offlatMap
, but not exactlyflatMap
s.Suggestion for a correct reference implementation
Avoid Scala
Future
s altogether. Instead, introduce our own primitive that gives control over listeners. Also, it need not be as general purpose asFuture
, but can exploit Libretto specifics, such as having exactly one listener.The text was updated successfully, but these errors were encountered: