o

akka.stream.contrib

PassThroughFlow

object PassThroughFlow

Given Flow[In, Out] and f: (In, Out) => Result, transforms original flow into a Flow[In, Result], i.e. for every element e signalled from upstream, emits f(e, flow(e)).

Has an overloaded factory that fixes f to be a tuple constructor, i.e. for every element e signalled from upstream, emits a tuple (e, flow(e)).

IMPORTANT! This flow combinator is guaranteed to work correctly on flows that have behavior of classic total functions, meaning that they should be a one-t-one functions that don't reorder, drop, inject etc new elements. In the future these restrictions may be lifted, for now please refer to the following resources for more: - https://github.com/akka/akka-stream-contrib/pull/142#discussion_r228875614 - https://github.com/akka/akka/issues/15957 and linked issues - https://discuss.lightbend.com/t/passing-stream-elements-into-and-over-flow/2536 and linked resources

Applies no internal buffering / flow control etc.

Emits when upstream emits an element

Backpressures when downstream backpressures

Completes when upstream completes

Cancels when downstream cancels

Examples:

1. Consuming from Kafka: Allows for busines logic to stay unaware of commits:

val logic: Flow[CommittableMessage[String, Array[Byte]], ProcessingResult] =
  Flow[CommittableMessage[String, Array[Byte]]]
    .map(m => process(m.record))

// Used like this:
Consumer
  .committableSource(settings, Subscriptions.topics("my-topic"))
  .via(PassThroughFlow(logic))
  .map { case (committableMessage, processingResult) =>
    // decide to commit or not based on `processingResult`
  }

// or:

Consumer
  .committableSource(settings, Subscriptions.topics("my-topic"))
  .via(PassThroughFlow(logic, Keep.left)) // process messages but return original elements
  .mapAsync(1)(_.commitScalaDsl())

2. Logging HTTP request-response based on some rule

// assuming Akka HTTP entities:
val route: Route = ???

// Route has an implicit conversion to Flow[HttpRequest, HttpResponse]

Flow[HttpRequest]
  .map { r =>
    // don't log web crawlers' requests
    if (userAgent(r) != "google-bot") {
      logRequest(r)
    }
    r
  }
  .via(PassThroughFlow(route)) // req => (req, resp)
  .map { case (req, resp) =>
    // don't log responses to web crawlers
    if (userAgent(req) != "google-bot") {
      logResponse(resp)
    }
    resp
  }
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. PassThroughFlow
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def apply[I, O, R](processingFlow: Flow[I, O, NotUsed], output: (I, O) ⇒ R): Graph[FlowShape[I, R], NotUsed]
  5. def apply[I, O](processingFlow: Flow[I, O, NotUsed]): Graph[FlowShape[I, (I, O)], NotUsed]
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  12. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  13. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  15. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  17. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  18. def toString(): String
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped