MartinHH commented on code in PR #1787:
URL: https://github.com/apache/pekko/pull/1787#discussion_r1985256204


##########
stream/src/main/scala/org/apache/pekko/stream/impl/fusing/Switch.scala:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pekko.stream.impl.fusing
+
+import org.apache.pekko
+import pekko.annotation.InternalApi
+import pekko.stream.Attributes
+import pekko.stream.FlowShape
+import pekko.stream.Graph
+import pekko.stream.Inlet
+import pekko.stream.Outlet
+import pekko.stream.SourceShape
+import pekko.stream.impl.Stages.DefaultAttributes
+import pekko.stream.scaladsl.Source
+import pekko.stream.stage.GraphStage
+import pekko.stream.stage.GraphStageLogic
+import pekko.stream.stage.InHandler
+import pekko.stream.stage.OutHandler
+import pekko.util.OptionVal
+
+/**
+ * INTERNAL API
+ */
+@InternalApi private[pekko] final class Switch[T, M]
+    extends GraphStage[FlowShape[Graph[SourceShape[T], M], T]] {
+  private val in = Inlet[Graph[SourceShape[T], M]]("switch.in")
+  private val out = Outlet[T]("switch.out")
+
+  override def initialAttributes = DefaultAttributes.switch
+
+  override val shape = FlowShape(in, out)
+
+  override def createLogic(enclosingAttributes: Attributes) =
+    new GraphStageLogic(shape) with InHandler with OutHandler {
+
+      var source = OptionVal.none[SubSinkInlet[T]]
+
+      override def preStart(): Unit = {
+        pull(in)
+        super.preStart()
+      }
+
+      override def onPush(): Unit = {
+        val source = grab(in)
+        setSource(source)
+        tryPull(in)
+      }
+
+      override def onUpstreamFinish(): Unit = if (source.isEmpty) 
completeStage()
+
+      override def onPull(): Unit = {
+        if (isAvailable(out)) tryPushOut()
+      }
+
+      setHandler(in, this)
+      setHandler(out, this)
+
+      def tryPushOut(): Unit = {
+        if (source.isDefined) {
+          val src = source.get
+          if (src.isAvailable) {
+            push(out, src.grab())
+            if (!src.isClosed) src.pull()
+            else removeCurrentSource(completeIfClosed = true)
+          }
+        }
+      }
+
+      def setSource(source: Graph[SourceShape[T], M]): Unit = {
+        cancelCurrentSource()
+        removeCurrentSource(completeIfClosed = false)
+        val sinkIn = new SubSinkInlet[T]("SwitchSink")
+        sinkIn.setHandler(new InHandler {
+          override def onPush(): Unit = {
+            if (isAvailable(out)) {
+              push(out, sinkIn.grab())
+              sinkIn.pull()
+            }
+          }
+
+          override def onUpstreamFinish(): Unit = {
+            if (!sinkIn.isAvailable) removeCurrentSource(completeIfClosed = 
true)
+          }
+        })
+        sinkIn.pull()
+        this.source = OptionVal.Some(sinkIn)
+        val graph = Source.fromGraph(source).to(sinkIn.sink)
+        subFusingMaterializer.materialize(graph, defaultAttributes = 
enclosingAttributes)
+      }
+
+      def removeCurrentSource(completeIfClosed: Boolean): Unit = {
+        source = OptionVal.none
+        if (completeIfClosed && isClosed(in)) completeStage()
+      }
+
+      private def cancelCurrentSource(): Unit = {
+        if (source.isDefined) {
+          source.get.cancel()
+        }
+      }
+
+      override def postStop(): Unit = cancelCurrentSource()

Review Comment:
   I'm not sure. I followed the example of `FlattenMerge` where the cancelling 
of substreams is done in `postStop()`: 
https://github.com/apache/pekko/blob/5f64ccbedd97602941b04472c0c72ff5308979c5/stream/src/main/scala/org/apache/pekko/stream/impl/fusing/StreamOfStreams.scala#L146
 (and `onDownStreamFinished` is not overriden).
   
   I'd expect that `postStop` will eventually be called in any case and don't 
see a risk of `cancelCurrentSource()` being called from here when it shouldn't, 
so doing it in this "catch-all" handler feels safer to me. (But maybe I'm just 
missing the significant difference?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org
For additional commands, e-mail: notifications-h...@pekko.apache.org

Reply via email to