Acca streams - how to access the materialized cost of a stream

I learn to work with Akka flows and really love him, but part of the materialization is still a bit of a mystery to me.

Quote from http://doc.akka.io/docs/akka-stream-and-http-experimental/snapshot/scala/http/client-side/host-level.html#host-level-api

... cause an immediate shutdown of a specific pool by calling shutdown () on the HostConnectionPool instance that the pool client materializes to

How do I get an instance of HostConnectionPool?

More importantly, I would like to generally understand how to access the materialized value and perform some operation or get information from it.

Thanks in advance for any pointers or explanations of the documentation.

+4
source share
1 answer

This is done using a function Source.viaMat. Extending the sample code from the link in your question:

import akka.http.scaladsl.Http.HostConnectionPool
import akka.stream.scaladsl.Keep
import akka.stream.scaladsl.RunnableGraph

val poolClientFlow = Http().cachedHostConnectionPool[Int]("akka.io")

val graph: RunnableGraph[(HostConnectionPool, Future[(Try[HttpResponse], Int)])]  =
  Source.single(HttpRequest(uri = "/") -> 42)
        .viaMat(poolClientFlow)(Keep.right)
        .toMat(Sink.head)(Keep.both)

val (pool, fut) = graph.run()

pool.shutdown()

Since it Source.singlematerializes in Unit, it Keep.rightsays that it saves HostConnectionPoolinto which it enters poolClientFlow. The .toMatfunction Keep.bothsays to save both the left pool from the stream and the right Futurefrom Sink.

+7
source

Source: https://habr.com/ru/post/1625328/


All Articles