Reusability of Sources in Akka Streams

The Akka Streams docs (Basics and working with Flows • Akka Documentation) mention that Sources can be reused (i.e., materialized multiple times), but I’ve seen cases where this doesn’t work — for example:

  • Source.fromIterator(() => it) fails if the same iterator is reused.
    val ii = Iterator(1L, 5L)
    val a  = Source.fromIterator(() => ii.iterator)

    println("------")
    await(a.runWith(Sink.foreach(println)))
    println("------")
    await(a.runWith(Sink.foreach(println)))
    println("------")

Output:

------
1
5
------
------

  • In Akka HTTP, response.entity.dataBytes can’t be consumed more than once. There would an exeption thrown that Substream cannot be materialzed multiple times.

Is the documentation inaccurate, or is there some nuance I’m missing?

Regards,
Kyrylo

Hello,

I will be very grateful, if I get some help regarding this site from you.

Best Regard,
Tim

The problem you describe arises when you inject some shared third party object into the stream that cannot safely be shared. Best is to avoid creating streams using such shared mutable objects, since they could also cause thread safety issues. In a few cases it might not be possible (a stream representing incoming bytes from a TCP socket for example).

It could be worth improving the docs clarifying that.