Streams Cookbook
Loading

Streams Cookbook

Introduction

This is a collection of patterns to demonstrate various usage of the Akka Streams API by solving small targeted problems in the format of "recipes". The purpose of this page is to give inspiration and ideas how to approach various small tasks involving streams. The recipes in this page can be used directly as-is, but they are most powerful as starting points: customization of the code snippets is warmly encouraged.

This part also serves as supplementary material for the main body of documentation. It is a good idea to have this page open while reading the manual and look for examples demonstrating various streaming concepts as they appear in the main body of documentation.

If you need a quick reference of the available processing stages used in the recipes see Overview of built-in stages and their semantics.

Working with Flows

In this collection we show simple recipes that involve linear flows. The recipes in this section are rather general, more targeted recipes are available as separate sections (Buffers and working with rate, Working with streaming IO).

Logging elements of a stream

Situation: During development it is sometimes helpful to see what happens in a particular section of a stream.

The simplest solution is to simply use a map operation and use println to print the elements received to the console. While this recipe is rather simplistic, it is often suitable for a quick debug session.

mySource.map(elem -> {
  System.out.println(elem);
  return elem;
});

Another approach to logging is to use log() operation which allows configuring logging for elements flowing through the stream as well as completion and erroring.

// customise log levels
mySource.log("before-map")
  .withAttributes(Attributes.createLogLevels(onElement, onFinish, onFailure))
  .map(i -> analyse(i));

// or provide custom logging adapter
final LoggingAdapter adapter = Logging.getLogger(system, "customLogger");
mySource.log("custom", adapter);

Flattening a stream of sequences

Situation: A stream is given as a stream of sequence of elements, but a stream of elements needed instead, streaming all the nested elements inside the sequences separately.

The mapConcat operation can be used to implement a one-to-many transformation of elements using a mapper function in the form of In -> List<Out>. In this case we want to map a List of elements to the elements in the collection itself, so we can just call mapConcat(l -> l).

Source<List<Message>, BoxedUnit> myData = someDataSource;
Source<Message, BoxedUnit> flattened = myData.mapConcat(i -> i);

Draining a stream to a strict collection

Situation: A finite sequence of elements is given as a stream, but a scala collection is needed instead.

In this recipe we will use the grouped stream operation that groups incoming elements into a stream of limited size collections (it can be seen as the almost opposite version of the "Flattening a stream of sequences" recipe we showed before). By using a grouped(MAX_ALLOWED_SIZE) we create a stream of groups with maximum size of MaxAllowedSeqSize and then we take the first element of this stream by attaching a Sink.head(). What we get is a Future containing a sequence with all the elements of the original up to MAX_ALLOWED_SIZE size (further elements are dropped).

final Future<List<String>> strings = myData
  .grouped(MAX_ALLOWED_SIZE).runWith(Sink.head(), mat);

Calculating the digest of a ByteString stream

Situation: A stream of bytes is given as a stream of ByteStrings and we want to calculate the cryptographic digest of the stream.

This recipe uses a PushPullStage to host a mutable MessageDigest class (part of the Java Cryptography API) and update it with the bytes arriving from the stream. When the stream starts, the onPull handler of the stage is called, which just bubbles up the pull event to its upstream. As a response to this pull, a ByteString chunk will arrive (onPush) which we use to update the digest, then it will pull for the next chunk.

Eventually the stream of ByteStrings depletes and we get a notification about this event via onUpstreamFinish. At this point we want to emit the digest value, but we cannot do it in this handler directly. Instead we call ctx.absorbTermination() signalling to our context that we do not yet want to finish. When the environment decides that we can emit further elements onPull is called again, and we see ctx.isFinishing() returning true (since the upstream source has been depleted already). Since we only want to emit a final element it is enough to call ctx.pushAndFinish passing the digest ByteString to be emitted.

public PushPullStage<ByteString, ByteString> digestCalculator(String algorithm)
    throws NoSuchAlgorithmException {
  return new PushPullStage<ByteString, ByteString>() {
    final MessageDigest digest = MessageDigest.getInstance(algorithm);

    @Override
    public SyncDirective onPush(ByteString chunk, Context<ByteString> ctx) {
      digest.update(chunk.toArray());
      return ctx.pull();
    }

    @Override
    public SyncDirective onPull(Context<ByteString> ctx) {
      if (ctx.isFinishing()) {
        return ctx.pushAndFinish(ByteString.fromArray(digest.digest()));
      } else {
        return ctx.pull();
      }
    }

    @Override
    public TerminationDirective onUpstreamFinish(Context<ByteString> ctx) {
      // If the stream is finished, we need to emit the last element in the onPull block.
      // It is not allowed to directly emit elements from a termination block
      // (onUpstreamFinish or onUpstreamFailure)
      return ctx.absorbTermination();
    }
  };
}
final Source<ByteString, BoxedUnit> digest = data
  .transform(() -> digestCalculator("SHA-256"));

Parsing lines from a stream of ByteStrings

Situation: A stream of bytes is given as a stream of ByteStrings containing lines terminated by line ending characters (or, alternatively, containing binary frames delimited by a special delimiter byte sequence) which needs to be parsed.

The Framing helper class contains a convenience method to parse messages from a stream of ByteStrings:

final Source<String, BoxedUnit> lines = rawData
  .via(Framing.delimiter(ByteString.fromString("\r\n"), 100, true))
  .map(b -> b.utf8String());

Implementing reduce-by-key

Situation: Given a stream of elements, we want to calculate some aggregated value on different subgroups of the elements.

The "hello world" of reduce-by-key style operations is wordcount which we demonstrate below. Given a stream of words we first create a new stream that groups the words according to the i -> i function, i.e. now we have a stream of streams, where every substream will serve identical words.

To count the words, we need to process the stream of streams (the actual groups containing identical words). groupBy returns a SubSource, which means that we transform the resulting substreams directly. In this case we use the fold combinator to aggregate the word itself and the number of its occurrences within a Pair. Each substream will then emit one final value—precisely such a pair—when the overall input completes. As a last step we merge back these values from the substreams into one single output stream.

One noteworthy detail pertains to the MAXIMUM_DISTINCT_WORDS parameter: this defines the breadth of the merge operation. Akka Streams is focused on bounded resource consumption and the number of concurrently open inputs to the merge operator describes the amount of resources needed by the merge itself. Therefore only a finite number of substreams can be active at any given time. If the groupBy operator encounters more keys than this number then the stream cannot continue without violating its resource bound, in this case groupBy will terminate with a failure.

final int MAXIMUM_DISTINCT_WORDS = 1000;

final Source<Pair<String, Integer>, BoxedUnit> counts = words
    // split the words into separate streams first
  .groupBy(MAXIMUM_DISTINCT_WORDS, i -> i)
  // add counting logic to the streams
  .fold(new Pair<>("", 0), (pair, elem) -> new Pair<>(elem, pair.second() + 1))
  // get a stream of word counts
  .mergeSubstreams();

By extracting the parts specific to wordcount into

  • a groupKey function that defines the groups
  • a foldZero that defines the zero element used by the fold on the substream given the group key
  • a fold function that does the actual reduction

we get a generalized version below:

static public <In, K, Out> Flow<In, Pair<K, Out>, BoxedUnit> reduceByKey(
    int maximumGroupSize,
    Function<In, K> groupKey,
    Function<K, Out> foldZero,
    Function2<Out, In, Out> fold,
    Materializer mat) {

  return Flow.<In> create()
    .groupBy(maximumGroupSize, i -> i)
    .fold((Pair<K, Out>) null, (pair, elem) -> {
      final K key = groupKey.apply(elem);
      if (pair == null) return new Pair<>(key, fold.apply(foldZero.apply(key), elem));
      else return new Pair<>(key, fold.apply(pair.second(), elem));
    })
    .mergeSubstreams();
}
final int MAXIMUM_DISTINCT_WORDS = 1000;

Source<Pair<String, Integer>, BoxedUnit> counts = words.via(reduceByKey(
  MAXIMUM_DISTINCT_WORDS,
  word -> word,
  key -> 0,
  (count, elem) -> count + 1,
  mat));

Note

Please note that the reduce-by-key version we discussed above is sequential in reading the overall input stream, in other words it is NOT a parallelization pattern like MapReduce and similar frameworks.

Sorting elements to multiple groups with groupBy

Situation: The groupBy operation strictly partitions incoming elements, each element belongs to exactly one group. Sometimes we want to map elements into multiple groups simultaneously.

To achieve the desired result, we attack the problem in two steps:

  • first, using a function topicMapper that gives a list of topics (groups) a message belongs to, we transform our stream of Message to a stream of Pair<Message, Topic>` where for each topic the message belongs to a separate pair will be emitted. This is achieved by using mapConcat
  • Then we take this new stream of message topic pairs (containing a separate pair for each topic a given message belongs to) and feed it into groupBy, using the topic as the group key.
final Function<Message, List<Topic>> topicMapper = m -> extractTopics(m);

final Source<Pair<Message, Topic>, BoxedUnit> messageAndTopic = elems
  .mapConcat((Message msg) -> {
    List<Topic> topicsForMessage = topicMapper.apply(msg);
    // Create a (Msg, Topic) pair for each of the topics

    // the message belongs to
    return topicsForMessage
      .stream()
      .map(topic -> new Pair<Message, Topic>(msg, topic))
      .collect(toList());
});

SubSource<Pair<Message, Topic>, BoxedUnit> multiGroups = messageAndTopic
  .groupBy(2, pair -> pair.second())
  .map(pair -> {
    Message message = pair.first();
    Topic topic = pair.second();

    // do what needs to be done
  });

Working with Graphs

In this collection we show recipes that use stream graph elements to achieve various goals.

Triggering the flow of elements programmatically

Situation: Given a stream of elements we want to control the emission of those elements according to a trigger signal. In other words, even if the stream would be able to flow (not being backpressured) we want to hold back elements until a trigger signal arrives.

This recipe solves the problem by simply zipping the stream of Message elments with the stream of Trigger signals. Since Zip produces pairs, we simply map the output stream selecting the first element of the pair.

final RunnableGraph<Pair<TestPublisher.Probe<Trigger>, TestSubscriber.Probe<Message>>> g =
  RunnableGraph.<Pair<TestPublisher.Probe<Trigger>, TestSubscriber.Probe<Message>>>fromGraph(
    GraphDSL.create(
      triggerSource,
      messageSink,
      (p, s) -> new Pair<>(p, s),
      (builder, source, sink) -> {
        SourceShape<Message> elements =
          builder.add(Source.from(Arrays.asList("1", "2", "3", "4")).map(t -> new Message(t)));
        FlowShape<Pair<Message, Trigger>, Message> takeMessage =
          builder.add(Flow.<Pair<Message, Trigger>>create().map(p -> p.first()));
        final FanInShape2<Message, Trigger, Pair<Message, Trigger>> zip =
          builder.add(Zip.create());
        builder.from(elements).toInlet(zip.in0());
        builder.from(source).toInlet(zip.in1());
        builder.from(zip.out()).via(takeMessage).to(sink);
        return ClosedShape.getInstance();
      }
    )
  );

Alternatively, instead of using a Zip, and then using map to get the first element of the pairs, we can avoid creating the pairs in the first place by using ZipWith which takes a two argument function to produce the output element. If this function would return a pair of the two argument it would be exactly the behavior of Zip so ZipWith is a generalization of zipping.

final RunnableGraph<Pair<TestPublisher.Probe<Trigger>, TestSubscriber.Probe<Message>>> g =
  RunnableGraph.<Pair<TestPublisher.Probe<Trigger>, TestSubscriber.Probe<Message>>>fromGraph(
    GraphDSL.create(
      triggerSource,
      messageSink,
      (p, s) -> new Pair<>(p, s),
      (builder, source, sink) -> {
        final SourceShape<Message> elements =
          builder.add(Source.from(Arrays.asList("1", "2", "3", "4")).map(t -> new Message(t)));
        final FanInShape2<Message, Trigger, Message> zipWith =
          builder.add(ZipWith.create((msg, trigger) -> msg));
        builder.from(elements).toInlet(zipWith.in0());
        builder.from(source).toInlet(zipWith.in1());
        builder.from(zipWith.out()).to(sink);
        return ClosedShape.getInstance();
      }
    )
  );

Balancing jobs to a fixed pool of workers

Situation: Given a stream of jobs and a worker process expressed as a Flow create a pool of workers that automatically balances incoming jobs to available workers, then merges the results.

We will express our solution as a function that takes a worker flow and the number of workers to be allocated and gives a flow that internally contains a pool of these workers. To achieve the desired result we will create a Flow from a graph.

The graph consists of a Balance node which is a special fan-out operation that tries to route elements to available downstream consumers. In a for loop we wire all of our desired workers as outputs of this balancer element, then we wire the outputs of these workers to a Merge element that will collect the results from the workers.

public static <In, Out> Flow<In, Out, BoxedUnit> balancer(
    Flow<In, Out, BoxedUnit> worker, int workerCount) {
  return Flow.fromGraph(GraphDSL.create(b -> {
      boolean waitForAllDownstreams = true;
      final UniformFanOutShape<In, In> balance =
              b.add(Balance.<In>create(workerCount, waitForAllDownstreams));
      final UniformFanInShape<Out, Out> merge =
              b.add(Merge.<Out>create(workerCount));

      for (int i = 0; i < workerCount; i++) {
          b.from(balance.out(i)).via(b.add(worker)).toInlet(merge.in(i));
      }

      return FlowShape.of(balance.in(), merge.out());
  }));
}
Flow<Message, Message, BoxedUnit> balancer = balancer(worker, 3);
Source<Message, BoxedUnit> processedJobs = data.via(balancer);

Working with rate

This collection of recipes demonstrate various patterns where rate differences between upstream and downstream needs to be handled by other strategies than simple backpressure.

Dropping elements

Situation: Given a fast producer and a slow consumer, we want to drop elements if necessary to not slow down the producer too much.

This can be solved by using the most versatile rate-transforming operation, conflate. Conflate can be thought as a special fold operation that collapses multiple upstream elements into one aggregate element if needed to keep the speed of the upstream unaffected by the downstream.

When the upstream is faster, the fold process of the conflate starts. This folding needs a zero element, which is given by a seed function that takes the current element and produces a zero for the folding process. In our case this is i -> i so our folding state starts form the message itself. The folder function is also special: given the aggregate value (the last message) and the new element (the freshest element) our aggregate state becomes simply the freshest element. This choice of functions results in a simple dropping operation.

final Flow<Message, Message, BoxedUnit> droppyStream =
  Flow.of(Message.class).conflate(i -> i, (lastMessage, newMessage) -> newMessage);

Dropping broadcast

Situation: The default Broadcast graph element is properly backpressured, but that means that a slow downstream consumer can hold back the other downstream consumers resulting in lowered throughput. In other words the rate of Broadcast is the rate of its slowest downstream consumer. In certain cases it is desirable to allow faster consumers to progress independently of their slower siblings by dropping elements if necessary.

One solution to this problem is to append a buffer element in front of all of the downstream consumers defining a dropping strategy instead of the default Backpressure. This allows small temporary rate differences between the different consumers (the buffer smooths out small rate variances), but also allows faster consumers to progress by dropping from the buffer of the slow consumers if necessary.

// Makes a sink drop elements if too slow
public <T> Sink<T, Future<BoxedUnit>> droppySink(Sink<T, Future<BoxedUnit>> sink, int size) {
  return Flow.<T> create()
    .buffer(size, OverflowStrategy.dropHead())
    .toMat(sink, Keep.right());
}
RunnableGraph.fromGraph(GraphDSL.create(builder -> {
  final int outputCount = 3;
  final UniformFanOutShape<Integer, Integer> bcast =
    builder.add(Broadcast.create(outputCount));
  builder.from(builder.add(myData)).toFanOut(bcast);
  builder.from(bcast).to(builder.add(droppySink(mySink1, 10)));
  builder.from(bcast).to(builder.add(droppySink(mySink2, 10)));
  builder.from(bcast).to(builder.add(droppySink(mySink3, 10)));
  return ClosedShape.getInstance();
}));

Collecting missed ticks

Situation: Given a regular (stream) source of ticks, instead of trying to backpressure the producer of the ticks we want to keep a counter of the missed ticks instead and pass it down when possible.

We will use conflate to solve the problem. Conflate takes two functions:

  • A seed function that produces the zero element for the folding process that happens when the upstream is faster than the downstream. In our case the seed function is a constant function that returns 0 since there were no missed ticks at that point.
  • A fold function that is invoked when multiple upstream messages needs to be collapsed to an aggregate value due to the insufficient processing rate of the downstream. Our folding function simply increments the currently stored count of the missed ticks so far.

As a result, we have a flow of Int where the number represents the missed ticks. A number 0 means that we were able to consume the tick fast enough (i.e. zero means: 1 non-missed tick + 0 missed ticks)

final Flow<Tick, Integer, BoxedUnit> missedTicks =
  Flow.of(Tick.class).conflate(tick -> 0, (missed, tick) -> missed + 1);

Create a stream processor that repeats the last element seen

Situation: Given a producer and consumer, where the rate of neither is known in advance, we want to ensure that none of them is slowing down the other by dropping earlier unconsumed elements from the upstream if necessary, and repeating the last value for the downstream if necessary.

We have two options to implement this feature. In both cases we will use DetachedStage to build our custom element (DetachedStage is specifically designed for rate translating elements just like conflate, expand or buffer). In the first version we will use a provided initial value initial that will be used to feed the downstream if no upstream element is ready yet. In the onPush() handler we just overwrite the currentValue variable and immediately relieve the upstream by calling pull() (remember, implementations of DetachedStage are not allowed to call push() as a response to onPush() or call pull() as a response of onPull()). The downstream onPull handler is very similar, we immediately relieve the downstream by emitting currentValue.

class HoldWithInitial<T> extends DetachedStage<T, T> {
  private T currentValue;

  public HoldWithInitial(T initial) {
    currentValue = initial;
  }

  @Override
  public UpstreamDirective onPush(T elem, DetachedContext<T> ctx) {
    currentValue = elem;
    return ctx.pull();
  }

  @Override
  public DownstreamDirective onPull(DetachedContext<T> ctx) {
    return ctx.push(currentValue);
  }
}

While it is relatively simple, the drawback of the first version is that it needs an arbitrary initial element which is not always possible to provide. Hence, we create a second version where the downstream might need to wait in one single case: if the very first element is not yet available.

We introduce a boolean variable waitingFirstValue to denote whether the first element has been provided or not (alternatively an Optional can be used for currentValue or if the element type is a subclass of Object a null can be used with the same purpose). In the downstream onPull() handler the difference from the previous version is that we call holdDownstream() if the first element is not yet available and thus blocking our downstream. The upstream onPush() handler sets waitingFirstValue to false, and after checking if holdDownstream() has been called it either releaves the upstream producer, or both the upstream producer and downstream consumer by calling pushAndPull()

class HoldWithWait<T> extends DetachedStage<T, T> {
  private T currentValue = null;
  private boolean waitingFirstValue = true;

  @Override
  public UpstreamDirective onPush(T elem, DetachedContext<T> ctx) {
    currentValue = elem;
    waitingFirstValue = false;
    if (ctx.isHoldingDownstream()) {
      return ctx.pushAndPull(currentValue);
    } else {
      return ctx.pull();
    }
  }

  @Override
  public DownstreamDirective onPull(DetachedContext<T> ctx) {
    if (waitingFirstValue) {
      return ctx.holdDownstream();
    } else {
      return ctx.push(currentValue);
    }
  }
}

Globally limiting the rate of a set of streams

Situation: Given a set of independent streams that we cannot merge, we want to globally limit the aggregate throughput of the set of streams.

One possible solution uses a shared actor as the global limiter combined with mapAsync to create a reusable Flow that can be plugged into a stream to limit its rate.

As the first step we define an actor that will do the accounting for the global rate limit. The actor maintains a timer, a counter for pending permit tokens and a queue for possibly waiting participants. The actor has an open and closed state. The actor is in the open state while it has still pending permits. Whenever a request for permit arrives as a WantToPass message to the actor the number of available permits is decremented and we notify the sender that it can pass by answering with a MayPass message. If the amount of permits reaches zero, the actor transitions to the closed state. In this state requests are not immediately answered, instead the reference of the sender is added to a queue. Once the timer for replenishing the pending permits fires by sending a ReplenishTokens message, we increment the pending permits counter and send a reply to each of the waiting senders. If there are more waiting senders than permits available we will stay in the closed state.

public class Limiter extends AbstractActor {

  public static class WantToPass {}
  public static final WantToPass WANT_TO_PASS = new WantToPass();

  public static class MayPass {}
  public static final MayPass MAY_PASS = new MayPass();

  public static class ReplenishTokens {}
  public static final ReplenishTokens REPLENISH_TOKENS = new ReplenishTokens();

  private final int maxAvailableTokens;
  private final FiniteDuration tokenRefreshPeriod;
  private final int tokenRefreshAmount;

  private final List<ActorRef> waitQueue = new ArrayList<>();
  private final Cancellable replenishTimer;

  private int permitTokens;

  public static Props props(int maxAvailableTokens, FiniteDuration tokenRefreshPeriod,
      int tokenRefreshAmount) {
    return Props.create(Limiter.class, maxAvailableTokens, tokenRefreshPeriod,
      tokenRefreshAmount);
  }

  private Limiter(int maxAvailableTokens, FiniteDuration tokenRefreshPeriod,
      int tokenRefreshAmount) {
    this.maxAvailableTokens = maxAvailableTokens;
    this.tokenRefreshPeriod = tokenRefreshPeriod;
    this.tokenRefreshAmount = tokenRefreshAmount;
    this.permitTokens = maxAvailableTokens;

    this.replenishTimer = system.scheduler().schedule(
      this.tokenRefreshPeriod,
      this.tokenRefreshPeriod,
      self(),
      REPLENISH_TOKENS,
      context().system().dispatcher(),
      self());

    receive(open());
  }

  PartialFunction<Object, BoxedUnit> open() {
    return ReceiveBuilder
      .match(ReplenishTokens.class, rt -> {
        permitTokens = Math.min(permitTokens + tokenRefreshAmount, maxAvailableTokens);
      })
      .match(WantToPass.class, wtp -> {
        permitTokens -= 1;
        sender().tell(MAY_PASS, self());
        if (permitTokens == 0) {
          context().become(closed());
        }
      }).build();
  }

  PartialFunction<Object, BoxedUnit> closed() {
    return ReceiveBuilder
      .match(ReplenishTokens.class, rt -> {
        permitTokens = Math.min(permitTokens + tokenRefreshAmount, maxAvailableTokens);
        releaseWaiting();
      })
      .match(WantToPass.class, wtp -> {
        waitQueue.add(sender());
      })
      .build();
  }

  private void releaseWaiting() {
    final List<ActorRef> toBeReleased = new ArrayList<>(permitTokens);
    for (int i = 0; i < permitTokens && i < waitQueue.size(); i++) {
      toBeReleased.add(waitQueue.remove(i));
    }

    permitTokens -= toBeReleased.size();
    toBeReleased.stream().forEach(ref -> ref.tell(MAY_PASS, self()));
    if (permitTokens > 0) {
      context().become(open());
    }
  }

  @Override
  public void postStop() {
    replenishTimer.cancel();
    waitQueue.stream().forEach(ref -> {
      ref.tell(new Status.Failure(new IllegalStateException("limiter stopped")), self());
    });
  }
}

To create a Flow that uses this global limiter actor we use the mapAsync function with the combination of the ask pattern. We also define a timeout, so if a reply is not received during the configured maximum wait period the returned future from ask will fail, which will fail the corresponding stream as well.

public <T> Flow<T, T, BoxedUnit> limitGlobal(ActorRef limiter, FiniteDuration maxAllowedWait) {
  final int parallelism = 4;
  final Flow<T, T, BoxedUnit> f = Flow.create();

  return f.mapAsync(parallelism, element -> {
    final Timeout triggerTimeout = new Timeout(maxAllowedWait);
    final Future<Object> limiterTriggerFuture =
      Patterns.ask(limiter, Limiter.WANT_TO_PASS, triggerTimeout);
    return limiterTriggerFuture.map(new Mapper<Object, T>() {
      @Override
      public T apply(Object parameter) {
        return element;
      }
    }, system.dispatcher());
  });
}

Note

The global actor used for limiting introduces a global bottleneck. You might want to assign a dedicated dispatcher for this actor.

Working with IO

Chunking up a stream of ByteStrings into limited size ByteStrings

Situation: Given a stream of ByteStrings we want to produce a stream of ByteStrings containing the same bytes in the same sequence, but capping the size of ByteStrings. In other words we want to slice up ByteStrings into smaller chunks if they exceed a size threshold.

This can be achieved with a single PushPullStage. The main logic of our stage is in emitChunkOrPull() which implements the following logic:

  • if the buffer is empty, we pull for more bytes
  • if the buffer is nonEmpty, we split it according to the chunkSize. This will give a next chunk that we will emit, and an empty or nonempty remaining buffer.

Both onPush() and onPull() calls emitChunkOrPull() the only difference is that the push handler also stores the incoming chunk by appending to the end of the buffer.

class Chunker extends PushPullStage<ByteString, ByteString> {
  private final int chunkSize;
  private ByteString buffer = ByteString.empty();

  public Chunker(int chunkSize) {
    this.chunkSize = chunkSize;
  }

  @Override
  public SyncDirective onPush(ByteString elem, Context<ByteString> ctx) {
    buffer = buffer.concat(elem);
    return emitChunkOrPull(ctx);
  }

  @Override
  public SyncDirective onPull(Context<ByteString> ctx) {
    return emitChunkOrPull(ctx);
  }

  public SyncDirective emitChunkOrPull(Context<ByteString> ctx) {
    if (buffer.isEmpty()) {
      return ctx.pull();
    } else {
      Tuple2<ByteString, ByteString> split = buffer.splitAt(chunkSize);
      ByteString emit = split._1();
      buffer = split._2();
      return ctx.push(emit);
    }
  }
}
Source<ByteString, BoxedUnit> chunksStream =
  rawBytes.transform(() -> new Chunker(CHUNK_LIMIT));

Limit the number of bytes passing through a stream of ByteStrings

Situation: Given a stream of ByteStrings we want to fail the stream if more than a given maximum of bytes has been consumed.

This recipe uses a PushStage to implement the desired feature. In the only handler we override, onPush() we just update a counter and see if it gets larger than maximumBytes. If a violation happens we signal failure, otherwise we forward the chunk we have received.

class ByteLimiter extends PushStage<ByteString, ByteString> {
  final long maximumBytes;
  private int count = 0;

  public ByteLimiter(long maximumBytes) {
    this.maximumBytes = maximumBytes;
  }

  @Override
  public SyncDirective onPush(ByteString chunk, Context<ByteString> ctx) {
    count += chunk.size();
    if (count > maximumBytes) {
      return ctx.fail(new IllegalStateException("Too much bytes"));
    } else {
      return ctx.push(chunk);
    }
  }
}
Flow<ByteString, ByteString, BoxedUnit> limiter =
  Flow.of(ByteString.class).transform(() -> new ByteLimiter(SIZE_LIMIT));

Compact ByteStrings in a stream of ByteStrings

Situation: After a long stream of transformations, due to their immutable, structural sharing nature ByteStrings may refer to multiple original ByteString instances unnecessarily retaining memory. As the final step of a transformation chain we want to have clean copies that are no longer referencing the original ByteStrings.

The recipe is a simple use of map, calling the compact() method of the ByteString elements. This does copying of the underlying arrays, so this should be the last element of a long chain if used.

Source<ByteString, BoxedUnit> compacted = rawBytes.map(bs -> bs.compact());

Injecting keep-alive messages into a stream of ByteStrings

Situation: Given a communication channel expressed as a stream of ByteStrings we want to inject keep-alive messages but only if this does not interfere with normal traffic.

There is a built-in operation that allows to do this directly:

Flow<ByteString, ByteString, BoxedUnit> keepAliveInject =
  Flow.of(ByteString.class).keepAlive(
      scala.concurrent.duration.Duration.create(1, TimeUnit.SECONDS),
      () -> keepAliveMessage);

Contents