0

I'd like to combine-latest with Akka Streams as described here.

I can't figure out how to do it - please help!

Thanks, Ryan.

Ryan Worsley
  • 655
  • 1
  • 4
  • 17
  • Could you give some more specifics? What is the input, what is the consumer. Here I have an interop benchmark that shows how RxJava 2 can work with Akka-Stream: https://github.com/akarnokd/akarnokd-misc/blob/master/src/jmh/java/hu/akarnokd/comparison/AkkaStreamsCrossMapPerf.java#L54 – akarnokd Oct 19 '16 at 16:15
  • What kind of specifics? I'd like to be able to take two streams exactly like the `zip` function would, but instead of zip semantics I'm after the combine-latest semantics that I linked. Checked you link and I'm not sure how it helps me? Perhaps we're talking at cross purposes? – Ryan Worsley Oct 19 '16 at 16:19
  • I thought you want to combine two Akka Streams but it lacks the operator so you'd want to reuse RxJava's combineLatest operator. – akarnokd Oct 19 '16 at 16:33
  • Oh no, I don't want a dependency on RxJava, I just want to achieve the same by extending Akka Streams, or ideally, by using existing functionality. – Ryan Worsley Oct 19 '16 at 16:44
  • Did u find the answer? – NieMaszNic Mar 30 '17 at 07:07
  • I had to write it for myself in the end as it didn't come out of the box and wasn't easily achieved using existing operators. Unfortunately I can't share the code as it belongs to my previous employer. I basically read through the documentation on this page http://doc.akka.io/docs/akka/2.4.17/scala/stream/stream-graphs.html Which eventually got me where I wanted, but it's a fairly steep learning curve. – Ryan Worsley Mar 30 '17 at 10:42

1 Answers1

2

I just implemented it quickly. Not sure if its bugless, but worth a try :) https://gist.github.com/tg44/2e75d45c234ca02d91cfdac35f41a5a2 Comments under the gist is welcomed!

As we spoke on the gitter channel, it can't achieved with build in stages, but you can write the functionality with a custom stage. You will need two input and one output (can be extend to N input), so it's a fan in shape.

I save the incoming elements to options, and whenever an input is ready (aka. send an element) I save the given element to the option. Whenever the output need an element (and we already have one element from both inputs) I give it the values from the options as a tupple. This is the backpressure aware approach.

For a backpressured approach (where you produce all the pairs) you need to handle the waiting for "other" output element then the last one, and need to handle the input pulls. I think my implementation still not handle the too fast producers with slow consumer case (we can miss one element, can be handle with emits), and can deadlocked if both input produces the same element multiple times (maybe emits can handle this too).

If you want to extend my code functionality or want to write other custom stages read this: http://doc.akka.io/docs/akka/2.5/scala/stream/stream-customize.html

tg44
  • 810
  • 1
  • 8
  • 21
  • Edited with the design thoughts, possible bugs, and mentioned documentation and 'before' context. (I don't think copy the whole code is a good idea...) – tg44 Apr 19 '17 at 07:42