akka/akka-core

Support non-eager cancellation for alsoTo steam combinator

Open

#27,618 opened on Sep 3, 2019

View on GitHub
 (6 comments) (1 reaction) (0 assignees)Scala (3,547 forks)batch import
1 - triagedhelp wantedt:stream

Repository metrics

Stars
 (13,277 stars)
PR merge metrics
 (Avg merge 8d 19h) (10 merged PRs in 30d)

Description

Consider the following code

  val (f1, f2) = Source(1 to 100)
    .alsoToMat(Sink.head)(Keep.right)
    .toMat(Sink.last)(Keep.both)
    .run()

  for {
    head <- f1
    last <- f2
  } {
    println(s"head: $head , last: $last")
  }

This prints

head: 1 , last: 1

It appears that the behaviour of alsoTo and alsoToMat was changed as part of #24423 (a followup of #24291 ). For these cases this seams reasonable. However I commonly use alsoToMat to process one source with multiple sinks. I just found out that a sink such as Sink.head which cancels early can have unexpected effects on the other sinks.

This comment suggests adding an overload. However appearently using wireTap was suggested as an alternative. Unfortunately, wiretap will drop elements on backpressure which is not what I want in my usecase. Would it make sense to add an overloaded version of alsoTo and alsoToMat which includes an eagerCancel boolean?

Contributor guide