akka.stream.stage.GraphStage Scala Examples
The following examples show how to use akka.stream.stage.GraphStage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ExecuteAfterResponse.scala From opencensus-scala with Apache License 2.0 | 6 votes |
package io.opencensus.scala.akka.http.utils import akka.NotUsed import akka.http.scaladsl.model.{HttpEntity, HttpResponse} import akka.stream.scaladsl.Flow import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} object ExecuteAfterResponse { private class AfterResponseFlow[Element]( onFinish: () => Unit, onFailure: Throwable => Unit ) extends GraphStage[FlowShape[Element, Element]] { private val in = Inlet[Element]("in") private val out = Outlet[Element]("out") override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { def onPush(): Unit = push(out, grab(in)) def onPull(): Unit = pull(in) setHandler(in, this) setHandler(out, this) override def onUpstreamFinish(): Unit = { onFinish() super.onUpstreamFinish() } override def onUpstreamFailure(ex: Throwable): Unit = { onFailure(ex) super.onUpstreamFailure(ex) } } override val shape = FlowShape(in, out) } private object AfterResponseFlow { def apply[Element]( onFinish: () => Unit, onFailure: Throwable => Unit ): Flow[Element, Element, NotUsed] = Flow.fromGraph(new AfterResponseFlow(onFinish, onFailure)) } def onComplete( response: HttpResponse, onFinish: () => Unit, onFailure: Throwable => Unit ): HttpResponse = { response.copy( entity = if (response.status.allowsEntity) { response.entity.transformDataBytes( AfterResponseFlow(onFinish, onFailure) ) } else { onFinish() HttpEntity.Empty } ) } }
Example 2
Source File: TerminateFlowStage.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.common.http import akka.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } import akka.stream.{ Attributes, FlowShape, Inlet, Outlet } class TerminateFlowStage[T](pred: T ⇒ Boolean, forwardTerminatingMessage: Boolean = false, terminate: Boolean = true) extends GraphStage[FlowShape[T, T]] { val in = Inlet[T]("TerminateFlowStage.in") val out = Outlet[T]("TerminateFlowStage.out") override val shape = FlowShape.of(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { setHandlers(in, out, new InHandler with OutHandler { override def onPull(): Unit = { pull(in) } override def onPush(): Unit = { val chunk = grab(in) if (pred(chunk)) { if (forwardTerminatingMessage) push(out, chunk) if (terminate) failStage(new RuntimeException("Flow terminated by TerminateFlowStage")) else completeStage() } else push(out, chunk) } }) } }
Example 3
Source File: DebugStage.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.dc.stream.akkautils import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import com.typesafe.scalalogging.LazyLogging object DebugStage { def apply[A](name: String): DebugStage[A] = new DebugStage(name) } class DebugStage[A](name: String) extends GraphStage[FlowShape[A, A]] with LazyLogging { val in = Inlet[A]("DebugStage.in") val out = Outlet[A]("DebugStage.out") override val shape = FlowShape.of(in, out) override def createLogic(attr: Attributes): GraphStageLogic = new GraphStageLogic(shape) { setHandler( in, new InHandler { override def onPush(): Unit = { logger.info(s"[$name]: grabbing element") val elem = grab(in) logger.info(s"[$name]: pushing the grabbed element $elem") push(out, elem) } override def onUpstreamFinish(): Unit = { logger.info(s"[$name]: onUpstreamFinish") super.onUpstreamFinish() } override def onUpstreamFailure(ex: Throwable): Unit = { logger.info(s"[$name]: onUpstreamFailure") super.onUpstreamFailure(ex) } } ) setHandler( out, new OutHandler { override def onPull(): Unit = { logger.info(s"[$name]: pulling element") pull(in) } override def onDownstreamFinish(): Unit = { logger.info(s"[$name]: onDownstreamFinish") super.onDownstreamFinish() } } ) } }
Example 4
Source File: CrawlerRatePrinter.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.crawler import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import com.typesafe.scalalogging.Logger object CrawlerRatePrinter { def apply(crawlerId: String, printFrequency: Int, maxPrintRateMillis: Int)(logger: Logger): CrawlerRatePrinter = new CrawlerRatePrinter(crawlerId, printFrequency, maxPrintRateMillis)(logger) } class CrawlerRatePrinter(crawlerId: String, printFrequency: Int, maxPrintRateMillis: Int)(logger: Logger) extends GraphStage[FlowShape[Long, Long]] { val in = Inlet[Long]("CrawlerRatePrinter.in") val out = Outlet[Long]("CrawlerRatePrinter.out") override val shape = FlowShape.of(in, out) override def createLogic(attr: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private val startTime: Double = System.currentTimeMillis private var totalElementsGot: Long = 0L private var printedAtElementNo: Long = 0L private var printedAtTime: Double = startTime private var localStartTime: Double = startTime private var localTotalElementsGot: Long = 0L private var localRate: Double = 0 setHandler( in, new InHandler { override def onPush(): Unit = { val elem = grab(in) totalElementsGot += 1 localTotalElementsGot += 1 if (totalElementsGot - printedAtElementNo >= printFrequency) { val currentTime = System.currentTimeMillis if (currentTime - printedAtTime > maxPrintRateMillis) { val rate = totalElementsGot / (currentTime - startTime) * 1000 if (currentTime - localStartTime > 15000) { localRate = localTotalElementsGot / (currentTime - localStartTime) * 1000 localTotalElementsGot = 0 localStartTime = currentTime } logger.info(s"$crawlerId Current offset is $elem. Total $totalElementsGot offsets already processed. " + s"Read rate: avg: ${rate.formatted("%.2f")} current: ${localRate.formatted("%.2f")} offsets/second") printedAtElementNo = totalElementsGot printedAtTime = currentTime } } push(out, elem) } } ) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) } }
Example 5
Source File: CancellationBarrierGraphStage.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.internal import akka.stream.{ Attributes, FlowShape, Inlet, Outlet } import akka.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } class CancellationBarrierGraphStage[T] extends GraphStage[FlowShape[T, T]] { val in: Inlet[T] = Inlet("CancellationBarrier") val out: Outlet[T] = Outlet("CancellationBarrier") override val shape: FlowShape[T, T] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { setHandler( in, new InHandler { override def onPush(): Unit = emit(out, grab(in)) }) setHandler( out, new OutHandler { override def onPull(): Unit = pull(in) override def onDownstreamFinish(): Unit = { if (!hasBeenPulled(in)) pull(in) setHandler( in, new InHandler { override def onPush(): Unit = { grab(in) pull(in) } }) } }) } }
Example 6
Source File: PromptFlow.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat.cli import akka.stream._ import akka.stream.scaladsl.{ Flow, Source, GraphDSL } import akka.stream.stage.{ InHandler, GraphStageLogic, GraphStage } import scala.concurrent.ExecutionContext object Prompt { ⇒ collectedString = collectedString.dropRight(1) prompt() case x ⇒ //println(s"Got ${x.toInt}") collectedString += x print(x) pull(characterInput) } } }) setHandler(outputLinesIn, new InHandler { def onPush(): Unit = { print(s"$RESTORE$ERASE_LINE${grab(outputLinesIn)}\n$SAVE$promptLine") pull(outputLinesIn) } }) setHandler(readLinesOut, eagerTerminateOutput) override def preStart(): Unit = { pull(outputLinesIn) print(SAVE) // to make sure we don't jump back to former SAVE position in the terminal prompt() } def promptLine = s"$RESTORE$ERASE_LINE$SAVE> $collectedString" def prompt(): Unit = { print(promptLine) pull(characterInput) } } }
Example 7
Source File: ConsoleInput.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat.cli import akka.stream.stage.{ OutHandler, GraphStageLogic, GraphStage } import akka.stream._ import scala.annotation.tailrec import scala.concurrent.{ Future, ExecutionContext } import scala.util.control.NoStackTrace class ConsoleInput(implicit ec: ExecutionContext) extends GraphStage[SourceShape[Char]] { val out = Outlet[Char]("consoleOut") val shape: SourceShape[Char] = SourceShape(out) def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { TTY.noEchoStty() @volatile var cancelled = false def getOne(): Unit = { val callback = getAsyncCallback[Char](push(out, _)) Future { @tailrec def read(): Unit = if (cancelled) throw new Exception with NoStackTrace else if (System.in.available() > 0) callback.invoke(System.in.read().toChar) else { Thread.sleep(10) read() } read() } } setHandler(out, new OutHandler { def onPull(): Unit = getOne() override def onDownstreamFinish(cause: Throwable): Unit = { cancelled = true super.onDownstreamFinish(cause) } }) override def postStop(): Unit = TTY.saneStty() } }
Example 8
Source File: PersistentBufferBase.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.pattern.stream import java.io.File import akka.actor.{ActorSystem, Props} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import com.typesafe.config.Config import com.typesafe.scalalogging.Logger import org.slf4j.LoggerFactory abstract class PersistentBufferBase[T, S] (private[stream] val queue: PersistentQueue[T], onPushCallback: () => Unit = () => {}) (implicit serializer: QueueSerializer[T], system: ActorSystem) extends GraphStage[FlowShape[T, S]] { def this(config: Config)(implicit serializer: QueueSerializer[T], system: ActorSystem) = this(new PersistentQueue[T](config)) def this(persistDir: File)(implicit serializer: QueueSerializer[T], system: ActorSystem) = this(new PersistentQueue[T](persistDir)) private[stream] val in = Inlet[T]("PersistentBuffer.in") private[stream] val out = Outlet[S]("PersistentBuffer.out") val shape: FlowShape[T, S] = FlowShape.of(in, out) val defaultOutputPort = 0 @volatile protected var upstreamFailed = false @volatile protected var upstreamFinished = false protected val queueCloserActor = system.actorOf(Props(classOf[PersistentQueueCloserActor[T]], queue)) protected def elementOut(e: Event[T]): S protected def autoCommit(index: Long) = {} def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private var lastPushed = 0L var downstreamWaiting = false override def preStart(): Unit = { // Start upstream demand pull(in) } setHandler(in, new InHandler { override def onPush(): Unit = { val element = grab(in) queue.enqueue(element) onPushCallback() if (downstreamWaiting) { queue.dequeue() foreach { element => push(out, elementOut(element)) downstreamWaiting = false lastPushed = element.index autoCommit(element.index) } } pull(in) } override def onUpstreamFinish(): Unit = { upstreamFinished = true if (downstreamWaiting) { queueCloserActor ! PushedAndCommitted(defaultOutputPort, lastPushed, queue.read(defaultOutputPort)) queueCloserActor ! UpstreamFinished completeStage() } } override def onUpstreamFailure(ex: Throwable): Unit = { val logger = Logger(LoggerFactory.getLogger(this.getClass)) logger.error("Received upstream failure signal: " + ex) upstreamFailed = true queueCloserActor ! UpstreamFailed completeStage() } }) setHandler(out, new OutHandler { override def onPull(): Unit = { queue.dequeue() match { case Some(element) => push(out, elementOut(element)) lastPushed = element.index autoCommit(element.index) case None => if (upstreamFinished) { queueCloserActor ! PushedAndCommitted(defaultOutputPort, lastPushed, queue.read(defaultOutputPort)) queueCloserActor ! UpstreamFinished completeStage() } else downstreamWaiting = true } } }) } }
Example 9
Source File: TriggerMerge.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import akka.stream._ import akka.stream.scaladsl.{GraphDSL, Source} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} final class TriggerMerge[T](eagerComplete: Boolean = false) extends GraphStage[FanInShape2[T, TriggerEvent, T]] { override val shape: FanInShape2[T, TriggerEvent, T] = new FanInShape2[T, TriggerEvent, T]("TriggerMerge") val in: Inlet[T] = shape.in0 val trigger: Inlet[TriggerEvent] = shape.in1 override def initialAttributes = Attributes.name("TriggerMerge") override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { var flowEnabled, requestPending, triggerCompleted, willShutDown = false setHandler(in, new InHandler { override def onPush(): Unit = { push(out, grab(in)) if (willShutDown) completeStage() } override def onUpstreamFinish(): Unit = { if (!isAvailable(in)) completeStage() willShutDown = true } }) setHandler(trigger, new InHandler { override def onPush(): Unit = { val triggerEvent = grab(trigger) if (triggerEvent.value != flowEnabled) { if (triggerEvent.value && requestPending) { requestPending = false tryPull(in) } flowEnabled = triggerEvent.value } if (triggerCompleted) cancelTrigger() if (!hasBeenPulled(trigger)) tryPull(trigger) } override def onUpstreamFinish(): Unit = { if (!isAvailable(trigger)) cancelTrigger() triggerCompleted = true } def cancelTrigger(): Unit = if (eagerComplete) completeStage() else cancel(trigger) }) setHandler(out, new OutHandler { override def onPull(): Unit = { if (willShutDown) completeStage() else { if (flowEnabled) tryPull(in) else { requestPending = true if (!hasBeenPulled(trigger)) tryPull(trigger) } } } }) } def out: Outlet[T] = shape.out override def toString = "TriggerMerge" } class Trigger[T, M1, M2](eagerComplete: Boolean = false) { import GraphDSL.Implicits._ private[stream] val source = (in: Graph[SourceShape[T], M1], trigger: Graph[SourceShape[TriggerEvent], M2]) => Source.fromGraph( GraphDSL.create(in, trigger)((_, _)) { implicit builder => (sIn, sTrigger) => val merge = builder.add(new TriggerMerge[T](eagerComplete)) sIn ~> merge.in0 sTrigger ~> merge.in1 SourceShape(merge.out) }) // for Java def source(in: javadsl.Source[T, M1], trigger: javadsl.Source[TriggerEvent, M2]): javadsl.Source[T, akka.japi.Pair[M1, M2]] = { source(in.asScala, trigger.asScala).mapMaterializedValue { case (m1, m2) => akka.japi.Pair(m1, m2) }.asJava } }
Example 10
Source File: Deduplicate.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.streams import akka.event.Logging import akka.stream.ActorAttributes.SupervisionStrategy import akka.stream._ import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import scala.compat.java8.FunctionConverters._ import scala.util.control.NonFatal object Deduplicate { def apply[T, U](key: T => U, duplicateCount: Long) = new Deduplicate[T, U](key, duplicateCount, new java.util.HashMap[U, MutableLong]()) def apply[T, U](key: T => U, duplicateCount: Long, registry: java.util.Map[U, MutableLong]) = new Deduplicate[T, U](key, duplicateCount, registry) def apply[T](duplicateCount: Long = Long.MaxValue, registry: java.util.Map[T, MutableLong] = new java.util.HashMap[T, MutableLong]()): Deduplicate[T, T] = Deduplicate(t => t, duplicateCount, registry) } case class MutableLong(var value: Long = 0L) { def increment() = { value += 1 value } }
Example 11
Source File: MinimumChunk.scala From akka-xml-parser with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.akka.xml import akka.NotUsed import akka.stream.scaladsl.Flow import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.util.ByteString @deprecated("Use FastParsingStage instead","akka-xml-parser 1.0.0") object MinimumChunk { def parser(minimumChunkSize: Int): Flow[ByteString, ByteString, NotUsed] = { Flow.fromGraph(new StreamingXmlParser(minimumChunkSize)) } private class StreamingXmlParser(minimumChunkSize: Int) extends GraphStage[FlowShape[ByteString, ByteString]] with StreamHelper with ParsingDataFunctions { val in: Inlet[ByteString] = Inlet("Chunking.in") val out: Outlet[ByteString] = Outlet("Chunking.out") override val shape: FlowShape[ByteString, ByteString] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private var buffer = ByteString.empty setHandler(in, new InHandler { override def onPush(): Unit = { val elem = grab(in) buffer ++= elem emitChunk() } override def onUpstreamFinish(): Unit = { emit(out, buffer) completeStage() } }) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) private def emitChunk(): Unit = { if (buffer.size > minimumChunkSize) { push(out, buffer) buffer = ByteString.empty } else { pull(in) } } } } }
Example 12
Source File: RatePrinter.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.dc.stream import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import cmwell.dc.LazyLogging import cmwell.dc.stream.MessagesTypesAndExceptions.{DcInfo, DcInfoKey} object RatePrinter { def apply[A](dcKey: DcInfoKey, elementCount: A => Double, elementText: String, elementsText: String, printFrequency: Int): RatePrinter[A] = new RatePrinter(dcKey, elementCount, elementText, elementsText, printFrequency) } class RatePrinter[A](dcKey: DcInfoKey, elementCount: A => Double, elementText: String, elementsText: String, printFrequency: Int) extends GraphStage[FlowShape[A, A]] with LazyLogging { val in = Inlet[A]("RatePrinter.in") val out = Outlet[A]("RatePrinter.out") override val shape = FlowShape.of(in, out) override def createLogic(attr: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private val startTime: Double = System.currentTimeMillis private var totalElementsGot: Double = 0 private var printedAtElementNo: Double = 0 private var localStartTime: Double = startTime private var localTotalElementsGot: Double = 0 private var localRate: Double = 0 setHandler( in, new InHandler { override def onPush(): Unit = { val elem = grab(in) val currentTime = System.currentTimeMillis val currentElementsGot = elementCount(elem) totalElementsGot += currentElementsGot localTotalElementsGot += currentElementsGot if (totalElementsGot - printedAtElementNo >= printFrequency) { val rate = totalElementsGot / (currentTime - startTime) * 1000 logger.info( s"Sync $dcKey: Got ${currentElementsGot.formatted("%.2f")} $elementText. Total ${totalElementsGot .formatted("%.2f")} $elementsText. Read rate: avg: ${rate.formatted("%.2f")} current: ${localRate .formatted("%.2f")} $elementText/second" ) if (currentTime - localStartTime > 15000) { localRate = localTotalElementsGot / (currentTime - localStartTime) * 1000 localTotalElementsGot = 0 localStartTime = currentTime } printedAtElementNo = totalElementsGot } push(out, elem) } } ) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) } }
Example 13
Source File: JdbcSourceStage.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.connector.sql import java.sql.{ Connection, PreparedStatement, ResultSet } import akka.stream.stage.{ GraphStage, GraphStageLogic, OutHandler } import akka.stream.{ Attributes, Outlet, SourceShape } import javax.sql.DataSource import fusion.jdbc.ConnectionPreparedStatementCreator import fusion.jdbc.util.JdbcUtils import scala.util.control.NonFatal class JdbcSourceStage(dataSource: DataSource, creator: ConnectionPreparedStatementCreator, fetchRowSize: Int) extends GraphStage[SourceShape[ResultSet]] { private val out: Outlet[ResultSet] = Outlet("JdbcSource.out") override def shape: SourceShape[ResultSet] = SourceShape(out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler { var maybeConn = Option.empty[(Connection, Boolean, PreparedStatement, ResultSet)] setHandler(out, this) override def onPull(): Unit = maybeConn match { case Some((_, _, _, rs)) if rs.next() => push(out, rs) case Some(_) => completeStage() case None => () // doing nothing, waiting for in preStart() to be completed } override def preStart(): Unit = try { val conn = dataSource.getConnection val autoCommit = conn.getAutoCommit conn.setAutoCommit(false) val stmt = creator(conn) val rs = stmt.executeQuery() // rs.setFetchDirection(ResultSet.TYPE_FORWARD_ONLY) rs.setFetchSize(fetchRowSize) maybeConn = Option((conn, autoCommit, stmt, rs)) } catch { case NonFatal(e) => failStage(e) } override def postStop(): Unit = for { (conn, autoCommit, stmt, rs) <- maybeConn } { JdbcUtils.closeResultSet(rs) JdbcUtils.closeStatement(stmt) conn.setAutoCommit(autoCommit) JdbcUtils.closeConnection(conn) } } }
Example 14
Source File: Switch.scala From AckCord with MIT License | 5 votes |
package ackcord.util import java.util.concurrent.atomic.AtomicBoolean import scala.collection.immutable import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FanInShape2, Inlet, Outlet} //TODO: Maybe use a third inlet to determine where to listen to class Switch[A](ref: AtomicBoolean, emitChangeTrue: immutable.Seq[A], emitChangeFalse: immutable.Seq[A]) extends GraphStage[FanInShape2[A, A, A]] { override val shape: FanInShape2[A, A, A] = new FanInShape2[A, A, A]("Switch") val in1: Inlet[A] = shape.in0 val in2: Inlet[A] = shape.in1 val out: Outlet[A] = shape.out override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler { private var lastState: Boolean = ref.get() private var waitingOther: A = _ private def activeIn(): Inlet[A] = { val newState = ref.get() val newIn = if (newState) in1 else in2 if (lastState != newState) { lastState = newState emitMultiple(out, if (newState) emitChangeTrue else emitChangeFalse) if (waitingOther != null) { emit(out, waitingOther) waitingOther = null.asInstanceOf[A] } tryPull(newIn) } newIn } private def setInHandler(in: Inlet[A]): Unit = { setHandler( in, new InHandler { override def onPush(): Unit = { if (activeIn() == in) { emit(out, grab(in)) } else { require(waitingOther == null, "Pushed other when a waiting other was already defined") waitingOther = grab(in) } } } ) } setInHandler(in1) setInHandler(in2) setHandler(out, this) override def onPull(): Unit = pull(activeIn()) } }
Example 15
Source File: LavaplayerSource.scala From AckCord with MIT License | 5 votes |
package ackcord.lavaplayer import scala.concurrent.duration._ import akka.NotUsed import akka.stream.scaladsl.Source import akka.stream.stage.{GraphStage, GraphStageLogic, OutHandler, TimerGraphStageLogicWithLogging} import akka.stream.{Attributes, Outlet, SourceShape} import akka.util.ByteString import com.sedmelluq.discord.lavaplayer.player.AudioPlayer class LavaplayerSource(player: AudioPlayer) extends GraphStage[SourceShape[ByteString]] { val out: Outlet[ByteString] = Outlet("LavaplayerSource.out") override def shape: SourceShape[ByteString] = SourceShape(out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new TimerGraphStageLogicWithLogging(shape) with OutHandler { override def onPull(): Unit = tryPushFrame() override protected def onTimer(timerKey: Any): Unit = timerKey match { case "RetryProvide" => tryPushFrame() } def tryPushFrame(): Unit = { val frame = player.provide() if (frame != null) { push(out, ByteString.fromArray(frame.getData)) //log.debug("Sending data") } else { //log.debug("Scheduling attempt to provide frame") scheduleOnce("RetryProvide", 20.millis) } } setHandler(out, this) } } object LavaplayerSource { def source(player: AudioPlayer): Source[ByteString, NotUsed] = Source.fromGraph(new LavaplayerSource(player)) }
Example 16
Source File: RepeatLast.scala From AckCord with MIT License | 5 votes |
package ackcord.util import akka.NotUsed import akka.stream.scaladsl.Flow import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} class RepeatLast[A] extends GraphStage[FlowShape[A, A]] { val in: Inlet[A] = Inlet("RepeatLast.in") val out: Outlet[A] = Outlet("RepeatLast.out") override def shape: FlowShape[A, A] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { var elem: A = _ override def onPush(): Unit = { elem = grab(in) push(out, elem) } override def onPull(): Unit = { if (elem != null && isAvailable(out)) push(out, elem) if (!hasBeenPulled(in)) pull(in) } setHandlers(in, out, this) } } object RepeatLast { def flow[A]: Flow[A, A, NotUsed] = Flow.fromGraph(new RepeatLast[A]) }
Example 17
Source File: akkaStreams.scala From sangria-akka-streams with Apache License 2.0 | 5 votes |
package sangria.streaming import scala.language.higherKinds import akka.NotUsed import akka.event.Logging import akka.stream.ActorAttributes.SupervisionStrategy import akka.stream._ import akka.stream.scaladsl.{Merge, Sink, Source} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import scala.concurrent.Future object akkaStreams { type AkkaSource[+T] = Source[T, NotUsed] abstract class SimpleLinearGraphStage[T] extends GraphStage[FlowShape[T, T]] { val in = Inlet[T](Logging.simpleName(this) + ".in") val out = Outlet[T](Logging.simpleName(this) + ".out") override val shape = FlowShape(in, out) } class AkkaStreamsSubscriptionStream(implicit materializer: Materializer) extends SubscriptionStream[AkkaSource] { def supported[T[_]](other: SubscriptionStream[T]) = other.isInstanceOf[AkkaStreamsSubscriptionStream] def map[A, B](source: AkkaSource[A])(fn: A => B) = source.map(fn) def singleFuture[T](value: Future[T]) = Source.fromFuture(value) def single[T](value: T) = Source.single(value) def mapFuture[A, B](source: AkkaSource[A])(fn: A => Future[B]) = source.mapAsync(1)(fn) def first[T](s: AkkaSource[T]) = s.runWith(Sink.head) def failed[T](e: Throwable) = Source.failed(e).asInstanceOf[AkkaSource[T]] def onComplete[Ctx, Res](result: AkkaSource[Res])(op: => Unit) = result .via(OnComplete(() => op)) .recover {case e => op; throw e} .asInstanceOf[AkkaSource[Res]] def flatMapFuture[Ctx, Res, T](future: Future[T])(resultFn: T => AkkaSource[Res]) = Source.fromFuture(future).flatMapMerge(1, resultFn) def merge[T](streams: Vector[AkkaSource[T]]) = { if (streams.size > 1) Source.combine(streams(0), streams(1), streams.drop(2): _*)(Merge(_)) else if (streams.nonEmpty) streams.head else throw new IllegalStateException("No streams produced!") } def recover[T](stream: AkkaSource[T])(fn: Throwable => T) = stream recover {case e => fn(e)} } implicit def akkaSubscriptionStream(implicit materializer: Materializer): SubscriptionStream[AkkaSource] = new AkkaStreamsSubscriptionStream implicit def akkaStreamIsValidSubscriptionStream[A[_, _], Ctx, Res, Out](implicit materializer: Materializer, ev1: ValidOutStreamType[Res, Out]): SubscriptionStreamLike[Source[A[Ctx, Res], NotUsed], A, Ctx, Res, Out] = new SubscriptionStreamLike[Source[A[Ctx, Res], NotUsed], A, Ctx, Res, Out] { type StreamSource[X] = AkkaSource[X] val subscriptionStream = new AkkaStreamsSubscriptionStream } private final case class OnComplete[T](op: () => Unit) extends SimpleLinearGraphStage[T] { override def toString: String = "OnComplete" override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler with InHandler { def decider = inheritedAttributes.get[SupervisionStrategy].map(_.decider).getOrElse(Supervision.stoppingDecider) override def onPush(): Unit = { push(out, grab(in)) } override def onPull(): Unit = pull(in) override def onDownstreamFinish() = { op() super.onDownstreamFinish() } override def onUpstreamFinish() = { op() super.onUpstreamFinish() } setHandlers(in, out, this) } } }
Example 18
Source File: ConfigManager.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.lwc import akka.stream.Attributes import akka.stream.FlowShape import akka.stream.Inlet import akka.stream.Outlet import akka.stream.stage.GraphStage import akka.stream.stage.GraphStageLogic import akka.stream.stage.InHandler import akka.stream.stage.OutHandler import com.netflix.iep.lwc.ForwardingService.Message import com.netflix.iep.lwc.fwd.cw.ClusterConfig import com.typesafe.scalalogging.StrictLogging class ConfigManager extends GraphStage[FlowShape[Message, Map[String, ClusterConfig]]] with StrictLogging { private val in = Inlet[Message]("ConfigManager.in") private val out = Outlet[Map[String, ClusterConfig]]("ConfigManager.out") override val shape: FlowShape[Message, Map[String, ClusterConfig]] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = { new GraphStageLogic(shape) with InHandler with OutHandler { private val configs = scala.collection.mutable.AnyRefMap.empty[String, ClusterConfig] override def onPush(): Unit = { val msg = grab(in) if (msg.response.isUpdate) { val cluster = msg.cluster try { configs += cluster -> msg.response.clusterConfig logger.info(s"updated configuration for cluster $cluster") } catch { case e: Exception => logger.warn(s"invalid config for cluster $cluster", e) } } else { configs -= msg.cluster logger.info(s"deleted configuration for cluster ${msg.cluster}") } push(out, configs.toMap) } override def onPull(): Unit = { pull(in) } override def onUpstreamFinish(): Unit = { completeStage() } setHandlers(in, out, this) } } }
Example 19
Source File: RollingFileFlow.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.atlas.persistence import java.nio.file.Files import java.nio.file.Paths import akka.NotUsed import akka.stream.Attributes import akka.stream.FlowShape import akka.stream.Inlet import akka.stream.Outlet import akka.stream.stage.GraphStage import akka.stream.stage.GraphStageLogic import akka.stream.stage.InHandler import akka.stream.stage.OutHandler import akka.stream.stage.TimerGraphStageLogic import com.netflix.atlas.core.model.Datapoint import com.netflix.spectator.api.Registry import com.typesafe.scalalogging.StrictLogging import scala.concurrent.duration._ class RollingFileFlow( val dataDir: String, val rollingConf: RollingConfig, val registry: Registry ) extends GraphStage[FlowShape[Datapoint, NotUsed]] with StrictLogging { private val in = Inlet[Datapoint]("RollingFileSink.in") private val out = Outlet[NotUsed]("RollingFileSink.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = { new TimerGraphStageLogic(shape) with InHandler with OutHandler { private var hourlyWriter: HourlyRollingWriter = _ override def preStart(): Unit = { logger.info(s"creating sink directory: $dataDir") Files.createDirectories(Paths.get(dataDir)) hourlyWriter = new HourlyRollingWriter(dataDir, rollingConf, registry) hourlyWriter.initialize // This is to trigger rollover check when writer is idle for long time: e.g. in most cases // file writer will be idle while hour has ended but it is still waiting for late events schedulePeriodically(None, 5.seconds) } override def onPush(): Unit = { hourlyWriter.write(grab(in)) pull(in) } override protected def onTimer(timerKey: Any): Unit = { hourlyWriter.write(RollingFileWriter.RolloverCheckDatapoint) } override def onUpstreamFinish(): Unit = { hourlyWriter.close() completeStage() } override def onUpstreamFailure(ex: Throwable): Unit = { hourlyWriter.close() failStage(ex) } setHandlers(in, out, this) override def onPull(): Unit = { // Nothing to emit pull(in) } } } }
Example 20
Source File: SaveOffsetOnPull.scala From toketi-iothubreact with MIT License | 5 votes |
// Copyright (c) Microsoft. All rights reserved. package com.microsoft.azure.iot.iothubreact.checkpointing import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import com.microsoft.azure.iot.iothubreact.MessageFromDevice import com.microsoft.azure.iot.iothubreact.checkpointing.CheckpointService.UpdateOffset private[iothubreact] class SaveOffsetOnPull(cpconfig: ICPConfiguration, partition: Int) extends GraphStage[FlowShape[MessageFromDevice, MessageFromDevice]] { val in = Inlet[MessageFromDevice]("Checkpoint.Flow.in") val out = Outlet[MessageFromDevice]("Checkpoint.Flow.out") val none = "" override val shape = FlowShape.of(in, out) // All state MUST be inside the GraphStageLogic, never inside the enclosing // GraphStage. This state is safe to read/write from all the callbacks // provided by GraphStageLogic and the registered handlers. override def createLogic(attr: Attributes): GraphStageLogic = { new GraphStageLogic(shape) { val checkpointService = CheckpointActorSystem(cpconfig).getCheckpointService(partition) var lastOffsetSent = none // when a message enters the stage we safe its offset setHandler(in, new InHandler { override def onPush(): Unit = { val message: MessageFromDevice = grab(in) if (!message.isKeepAlive) lastOffsetSent = message.offset push(out, message) } }) // when asked for more data we consider the saved offset processed and save it setHandler(out, new OutHandler { override def onPull(): Unit = { if (lastOffsetSent != none) checkpointService ! UpdateOffset(lastOffsetSent) pull(in) } }) } } }
Example 21
Source File: Slf4JLogger.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.client.binding.util import akka.stream._ import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import org.slf4j.Logger final case class Slf4JLogger[T, U]( logger: Logger, prefix: String, project: T => U, logDemand: Boolean = false) extends GraphStage[FlowShape[T, T]] { override def toString = "Slf4JLog" val in: Inlet[T] = Inlet[T]("in") val out: Outlet[T] = Outlet[T]("out") override def shape: FlowShape[T, T] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler with InHandler { override def onPush(): Unit = { val elem = grab(in) if (logger.isDebugEnabled) logger.debug("[{}] Element: {}", prefix, project(elem)) push(out, elem) } override def onPull(): Unit = { if (logDemand) logger.debug("[{}] Demand", prefix) pull(in) } override def onUpstreamFailure(cause: Throwable): Unit = { logger.warn(s"[$prefix] Upstream failed", cause) super.onUpstreamFailure(cause) } override def onUpstreamFinish(): Unit = { logger.debug("[{}] Upstream finished.", prefix) super.onUpstreamFinish() } override def onDownstreamFinish(cause: Throwable): Unit = { logger.debug("[{}] Downstream finished.", prefix) super.onDownstreamFinish(cause) } setHandlers(in, out, this) } } object Slf4JLogger { def apply[T](logger: Logger, prefix: String): Slf4JLogger[T, T] = new Slf4JLogger(logger, prefix, identity) }
Example 22
Source File: SqsSourceShape.scala From akka-stream-sqs with Apache License 2.0 | 5 votes |
package me.snov.akka.sqs.shape import akka.stream._ import akka.stream.stage.GraphStage import com.amazonaws.services.sqs.model.Message import me.snov.akka.sqs.client.{SqsClient, SqsSettings} object SqsSourceShape { def apply(settings: SqsSettings): SqsSourceShape = apply(SqsClient(settings)) def apply(client: SqsClient): SqsSourceShape = new SqsSourceShape(client) } class SqsSourceShape(client: SqsClient) extends GraphStage[SourceShape[Message]] { // Define the (sole) output port of this stage val out: Outlet[Message] = Outlet("SqsSourceShape.out") // Define the shape of this stage, which is SourceShape with the port we defined above override val shape: SourceShape[Message] = SourceShape(out) // This is where the actual (possibly stateful) logic will live override def createLogic(inheritedAttributes: Attributes) = new SqsSourceGraphStageLogic(client, out, shape) }
Example 23
Source File: AckedFlow.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.actor.ActorRef import akka.camel.CamelMessage import akka.stream._ import akka.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } import scala.concurrent.{ ExecutionContext, Future, Promise } private[activemq] class AckedFlow[A, B](implicit ec: ExecutionContext) extends GraphStage[FlowShape[(ActorRef, B), AckTup[A, B]]] { val in = Inlet[(ActorRef, B)]("AckedFlow.in") val out = Outlet[AckTup[A, B]]("AckedFlow.out") override val shape: FlowShape[(ActorRef, B), AckTup[A, B]] = FlowShape.of(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { var promises = Vector.empty[(Promise[A], Future[A])] setHandler(in, new InHandler { override def onPush(): Unit = { val (ref, b) = grab(in) val p = Promise[A]() val eventualResponse = p.future eventualResponse.onSuccess(successResponse(ref)) eventualResponse.onFailure { case cause: Throwable => ref ! akka.actor.Status.Failure(cause) } promises = promises.filterNot(_._1.isCompleted) :+ (p -> eventualResponse) push(out, p -> b) } }) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) } def successResponse(source: ActorRef): PartialFunction[A, Unit] = { case _ => source ! akka.camel.Ack } } class AckedResponseFlow[A, B](implicit ec: ExecutionContext, builder: MessageBuilder[A, CamelMessage]) extends AckedFlow[A, B] { override def successResponse(source: ActorRef): PartialFunction[A, Unit] = { case msg => source ! builder.build(msg) } }
Example 24
Source File: GrpcSinkStage.scala From grpcakkastream with MIT License | 5 votes |
package grpc.akkastreams import akka.stream.{Attributes, Inlet, SinkShape} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler} import io.grpc.stub.CallStreamObserver class GrpcSinkStage[I](observer: CallStreamObserver[I]) extends GraphStage[SinkShape[I]] { val in = Inlet[I]("grpc.in") override val shape: SinkShape[I] = SinkShape.of(in) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with Runnable { var element: Option[I] = None override def run(): Unit = getAsyncCallback((_: Unit) => { element match { case Some(value) if observer.isReady => observer.onNext(value) tryPull(in) case _ => () } }).invoke(()) override def onPush(): Unit = { val value = grab(in) if (observer.isReady) { observer.onNext(value) pull(in) } else element = Some(value) } override def onUpstreamFinish(): Unit = observer.onCompleted() override def onUpstreamFailure(t: Throwable): Unit = observer.onError(t) override def preStart(): Unit = pull(in) observer.setOnReadyHandler(this) setHandler(in, this) } }
Example 25
Source File: GrpcGraphStage.scala From grpcakkastream with MIT License | 5 votes |
package grpc.akkastreams import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import io.grpc.stub.{ClientCallStreamObserver, ClientResponseObserver} class GrpcGraphStage[I, O](operator: GrpcOperator[I, O]) extends GraphStage[FlowShape[I, O]] { val in = Inlet[I]("grpc.in") val out = Outlet[O]("grpc.out") override val shape: FlowShape[I, O] = FlowShape.of(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { var requestStream = new AtomicReference[Option[ClientCallStreamObserver[I]]](None) val element = new AtomicReference[Option[I]](None) val requested = new AtomicBoolean(false) val outObs = new ClientResponseObserver[I, O] with Runnable { override def beforeStart(reqStream: ClientCallStreamObserver[I]): Unit = { requestStream.set(Some(reqStream)) reqStream.disableAutoInboundFlowControl() reqStream.setOnReadyHandler(this) } override def onError(t: Throwable) = getAsyncCallback((t: Throwable) => fail(out, t)).invoke(t) override def onCompleted() = getAsyncCallback((_: Unit) => complete(out)).invoke(()) override def onNext(value: O) = getAsyncCallback((value: O) => push(out, value)).invoke(value) override def run(): Unit = requestStream.get().foreach { reqStream => if (requested.compareAndSet(true, false)) reqStream.request(1) if (reqStream.isReady) { element.getAndSet(None).foreach { value => reqStream.onNext(value) tryPull(in) } } } } val inObs = operator(outObs) override def onPush(): Unit = { val value = grab(in) requestStream.get() match { case Some(reqStream) if reqStream.isReady() => reqStream.onNext(value) pull(in) case _ => element.compareAndSet(None, Some(value)) } } override def onUpstreamFinish(): Unit = inObs.onCompleted() override def onUpstreamFailure(t: Throwable): Unit = inObs.onError(t) override def onPull(): Unit = requestStream.get() match { case Some(reqStream) => reqStream.request(1) case _ => requested.compareAndSet(false, true) } override def preStart(): Unit = pull(in) setHandler(in, this) setHandler(out, this) } }
Example 26
Source File: StatefulCounterFlow.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter8 import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import com.packt.chapter8.WorkingWithGraphsApplication.GenericMsg class StatefulCounterFlow extends GraphStage[FlowShape[Seq[GenericMsg], Int]] { val in: Inlet[Seq[GenericMsg]] = Inlet("IncomingGenericMsg") val out: Outlet[Int] = Outlet("OutgoingCount") override val shape: FlowShape[Seq[GenericMsg], Int] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { var count = 0 setHandler(in, new InHandler { override def onPush() = { val elem = grab(in) count += elem.size push(out, count) } }) setHandler(out, new OutHandler { override def onPull() = { pull(in) } }) } }
Example 27
Source File: GraphStageForwarder.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.util import akka.actor.ActorRef import akka.stream.{Attributes, Outlet, SourceShape} import akka.stream.stage.{GraphStage, GraphStageLogic} import scala.reflect.runtime.universe._ class GraphStageForwarder[T: TypeTag, U: TypeTag]( outletName: String, coordinator: ActorRef, graphId: U ) extends GraphStage[SourceShape[T]] { val out: Outlet[T] = Outlet(outletName) override val shape = SourceShape[T](out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogicForwarder(out, shape, coordinator, graphId) }
Example 28
Source File: StreamManager.scala From toketi-iothubreact with MIT License | 5 votes |
// Copyright (c) Microsoft. All rights reserved. package com.microsoft.azure.iot.iothubreact import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} private[iothubreact] class StreamManager extends GraphStage[FlowShape[MessageFromDevice, MessageFromDevice]] { private[this] val in = Inlet[MessageFromDevice]("StreamCanceller.Flow.in") private[this] val out = Outlet[MessageFromDevice]("StreamCanceller.Flow.out") private[this] var closeSignal = false override val shape = FlowShape.of(in, out) def close(): Unit = closeSignal = true override def createLogic(attr: Attributes): GraphStageLogic = { new GraphStageLogic(shape) { setHandler(in, new InHandler { override def onPush(): Unit = { val message: MessageFromDevice = grab(in) push(out, message) } }) setHandler(out, new OutHandler { override def onPull(): Unit = { if (closeSignal) { cancel(in) } else { pull(in) } } }) } } }
Example 29
Source File: DropRepeated.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.server.api import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} object DropRepeated { def apply[T](): GraphStage[FlowShape[T, T]] = new DropRepeated } final class DropRepeated[T] extends GraphStage[FlowShape[T, T]] { private val in = Inlet[T]("input") private val out = Outlet[T]("DropRepeated output") override def shape: FlowShape[T, T] = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private var currentValue: Option[T] = None setHandler( in, new InHandler { override def onPush(): Unit = { val element = grab(in) if (currentValue.contains(element)) { pull(in) } else { currentValue = Some(element) push(out, element) } } } ) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) } }
Example 30
Source File: AugmentWith.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.utils import akka.stream.stage.{ GraphStage, GraphStageLogic } import akka.stream.{ Attributes, FanInShape2, Inlet, Outlet } final class AugmentWith[T, U](augmentFunction: (T, U) ⇒ Either[T, U]) extends GraphStage[FanInShape2[T, U, T]] { private val left = Inlet[T]("left") private val right = Inlet[U]("right") private val out = Outlet[T]("out") override val shape = new FanInShape2(left, right, out) override def createLogic(attr: Attributes) = new GraphStageLogic(shape) { setHandler(left, eagerTerminateInput) setHandler(right, ignoreTerminateInput) setHandler(out, eagerTerminateOutput) var retainedL: T = _ var retainedR: U = _ def dispatch(l: T, r: U): Unit = { augmentFunction(l, r).fold( augmented ⇒ { retainedR = r emit(out, augmented, readL) }, _ ⇒ { retainedL = l readR() }) } val dispatchR = dispatch(retainedL, _: U) val dispatchL = dispatch(_: T, retainedR) val passL = () ⇒ emit(out, retainedL, () ⇒ { passAlong(left, out, doPull = true) }) val readR = () ⇒ read(right)(dispatchR, passL) val readL = () ⇒ read(left)(dispatchL, readR) override def preStart(): Unit = { // all fan-in stages need to eagerly pull all inputs to get cycles started pull(right) read(left)(l ⇒ { retainedL = l readR() }, () ⇒ { abortReading(right) }) } } }
Example 31
Source File: SourceAugmenter.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.utils import akka.NotUsed import akka.stream.scaladsl.{ GraphDSL, Source } import akka.stream.stage.GraphStage import akka.stream.{ FanInShape2, SourceShape } class SourceAugmenter { def augment[T, U](source: Source[T, NotUsed], extrasSource: Source[U, NotUsed], augmentFunction: (T, U) ⇒ Either[T, U]): Source[T, NotUsed] = { augmentSource(new AugmentWith(augmentFunction), source, extrasSource) { (_, _) => NotUsed } } private def augmentSource[T, U, MatIn0, MatIn1, Mat]( combinator: GraphStage[FanInShape2[T, U, T]], s0: Source[T, MatIn0], s1: Source[U, MatIn1])(combineMat: (MatIn0, MatIn1) => Mat): Source[T, Mat] = Source.fromGraph(GraphDSL.create(s0, s1)(combineMat) { implicit builder => (s0, s1) => import GraphDSL.Implicits._ val merge = builder.add(combinator) s0 ~> merge.in0 s1 ~> merge.in1 SourceShape(merge.out) }) }
Example 32
Source File: SourceMergeSorter.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.utils import akka.NotUsed import akka.stream.scaladsl.{ GraphDSL, MergeSorted, Source } import akka.stream.stage.GraphStage import akka.stream.{ FanInShape2, SourceShape } import scala.annotation.tailrec class SourceMergeSorter { def mergeWithSorter[A](originSources: Seq[Source[A, NotUsed]])(implicit ordering: Ordering[A]): Source[A, NotUsed] = merge(originSources, sorter[A]) private def merge[A](originSources: Seq[Source[A, NotUsed]], f: (Source[A, NotUsed], Source[A, NotUsed]) => Source[A, NotUsed]): Source[A, NotUsed] = originSources match { case Nil => Source.empty[A] case sources => @tailrec def reducePairs(sources: Seq[Source[A, NotUsed]]): Source[A, NotUsed] = sources match { case Seq(s) => s case _ => reducePairs(sources.grouped(2).map { case Seq(a) => a case Seq(a, b) => f(a, b) }.toSeq) } reducePairs(sources) } private def sorter[A](s1: Source[A, NotUsed], s2: Source[A, NotUsed])(implicit ord: Ordering[A]): Source[A, NotUsed] = combineSources(new MergeSorted[A], s1, s2) { (_, _) => NotUsed } private def combineSources[A, MatIn0, MatIn1, Mat]( combinator: GraphStage[FanInShape2[A, A, A]], s0: Source[A, MatIn0], s1: Source[A, MatIn1])(combineMat: (MatIn0, MatIn1) => Mat): Source[A, Mat] = Source.fromGraph(GraphDSL.create(s0, s1)(combineMat) { implicit builder => (s0, s1) => import GraphDSL.Implicits._ val merge = builder.add(combinator) s0 ~> merge.in0 s1 ~> merge.in1 SourceShape(merge.out) }) }
Example 33
Source File: TakeWeighted.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.stream import akka.stream.ActorAttributes.SupervisionStrategy import akka.stream._ import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import scala.util.control.NonFatal case class TakeWeighted[T](maxWeight: Long, inclusive: Boolean = true, costFn: T => Long) extends GraphStage[FlowShape[T, T]] { val in: Inlet[T] = Inlet[T]("TakeWeighted.in") val out: Outlet[T] = Outlet[T]("TakeWeighted.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler with InHandler { private[this] var sum = 0L private def decider = inheritedAttributes.get[SupervisionStrategy].map(_.decider).getOrElse(Supervision.stoppingDecider) override def onPull(): Unit = pull(in) override def onPush(): Unit = { try { val elem = grab(in) val cost = costFn(elem) if (cost + sum < maxWeight) { sum += cost push(out, elem) } else { if (inclusive || cost + sum == maxWeight) { push(out, elem) } completeStage() } } catch { case NonFatal(ex) ⇒ decider(ex) match { case Supervision.Stop ⇒ failStage(ex) case _ ⇒ pull(in) } } } setHandlers(in, out, this) } }
Example 34
Source File: MapInitAndLast.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.stream import akka.stream.ActorAttributes.SupervisionStrategy import akka.stream._ import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import scala.util.control.NonFatal class MapInitAndLast[In, Out](init: In ⇒ Out, last: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in: Inlet[In] = Inlet[In]("MapInitAndLast.in") val out: Outlet[Out] = Outlet[Out]("MapInitAndLast.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { private var pending: In = null.asInstanceOf[In] private def decider = inheritedAttributes.get[SupervisionStrategy].map(_.decider).getOrElse(Supervision.stoppingDecider) override def preStart(): Unit = tryPull(in) def pushWith(elem: In, f: In ⇒ Out): Unit = try { push(out, f(elem)) } catch { case NonFatal(ex) ⇒ decider(ex) match { case Supervision.Stop ⇒ failStage(ex) case _ ⇒ pull(in) } } setHandler( in, new InHandler { override def onPush(): Unit = { val elem = grab(in) if (pending != null) pushWith(pending, init) else if (isAvailable(out)) pull(in) pending = elem } override def onUpstreamFinish(): Unit = { if (pending == null) completeStage() else { if (isAvailable(out)) { pushWith(pending, last) completeStage() } } } } ) setHandler( out, new OutHandler { override def onPull(): Unit = { if (!isClosed(in)) { if (!hasBeenPulled(in)) { pull(in) } } else { if (pending != null) { pushWith(pending, last) } completeStage() } } } ) } }
Example 35
Source File: StreamEventInspector.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.stream import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} // format: off class StreamEventInspector[Elem](onUpstreamFinishInspection: () => Unit = () => {}, onUpstreamFailureInspection: Throwable => Unit = _ => {}, onDownstreamFinishInspection: () => Unit = () => {}, onPushInspection: Elem => Unit = (_: Elem) => {}, onPullInspection: () => Unit = () => {}) extends GraphStage[FlowShape[Elem, Elem]] { // format: on private val in = Inlet[Elem]("StreamEventInspector.in") private val out = Outlet[Elem]("StreamEventInspector.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { setHandler( in, new InHandler { override def onPush(): Unit = { val elem = grab(in) onPushInspection(elem) push(out, elem) } override def onUpstreamFailure(ex: Throwable): Unit = { onUpstreamFailureInspection(ex) super.onUpstreamFailure(ex) } override def onUpstreamFinish(): Unit = { onUpstreamFinishInspection() super.onUpstreamFinish() } } ) setHandler( out, new OutHandler { override def onPull(): Unit = { onPullInspection() pull(in) } override def onDownstreamFinish(): Unit = { onDownstreamFinishInspection() super.onDownstreamFinish() } } ) } }
Example 36
Source File: TitForTatThrottle.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.tools.data.utils.akka import akka.stream.ThrottleMode.{Enforcing, Shaping} import akka.stream.stage.GraphStage import akka.stream.stage._ import akka.stream.{Attributes, RateExceededException, ThrottleMode, _} import akka.util.NanoTimeTokenBucket import scala.concurrent.duration.{FiniteDuration, _} class TitForTatThrottle[T] extends GraphStage[FlowShape[T, T]] { val in = Inlet[T]("TitForTatThrottle.in") val out = Outlet[T]("TitForTatThrottle.out") override val shape = FlowShape(in, out) // There is some loss of precision here because of rounding, but this only happens if nanosBetweenTokens is very // small which is usually at rates where that precision is highly unlikely anyway as the overhead of this stage // is likely higher than the required accuracy interval. // private val nanosBetweenTokens = per.toNanos / cost private val timerName: String = "ThrottleTimer" override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new TimerGraphStageLogic(shape) { // private val tokenBucket = new NanoTimeTokenBucket(maximumBurst, nanosBetweenTokens) var willStop = false var currentElement: T = _ // This scope is here just to not retain an extra reference to the handler below. // We can't put this code into preRestart() because setHandler() must be called before that. { val handler = new InHandler with OutHandler { var timeOfPreviousElmement = System.currentTimeMillis() override def onUpstreamFinish(): Unit = if (isAvailable(out) && isTimerActive(timerName)) willStop = true else completeStage() override def onPush(): Unit = { val elem = grab(in) val now = System.currentTimeMillis() val delayMillis = now - timeOfPreviousElmement timeOfPreviousElmement = now if (delayMillis == 0L) push(out, elem) else { currentElement = elem System.err.println(s"scheduled push in ${delayMillis.milliseconds}") scheduleOnce(timerName, delayMillis.milliseconds) } } override def onPull(): Unit = pull(in) } setHandler(in, handler) setHandler(out, handler) // After this point, we no longer need the `handler` so it can just fall out of scope. } override protected def onTimer(key: Any): Unit = { push(out, currentElement) currentElement = null.asInstanceOf[T] if (willStop) completeStage() } } override def toString = "Throttle" }