-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MQTT streaming: SourceQueue backpressure (#1577)
* mqtt streaming - introduce a mechanism to use SourceQueue backpressure When offering commands to the internal queue, a backpressure strategy is now used to ensure that elements are not dropped. Instead, we wait for consumption downstream before continuing. We want WatchedActorTerminatedException to complete stream processing as before, but not with a failure. A session shutting down isn’t in itself a failure.
- Loading branch information
1 parent
60d201a
commit ed5770c
Showing
8 changed files
with
356 additions
and
107 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
mqtt-streaming/src/main/mima-filters/1.0.2.backwards.excludes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# PR #1577 | ||
# https://github.com/akka/alpakka/pull/1577 | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.stream.alpakka.mqtt.streaming.MqttSessionSettings.this") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
mqtt-streaming/src/main/scala/akka/stream/alpakka/mqtt/streaming/impl/QueueOfferState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.mqtt.streaming.impl | ||
|
||
import akka.actor.typed.Behavior | ||
import akka.actor.typed.scaladsl.Behaviors | ||
import akka.stream.QueueOfferResult | ||
|
||
private[mqtt] object QueueOfferState { | ||
|
||
/** | ||
* A marker trait that holds a result for SourceQueue#offer | ||
*/ | ||
trait QueueOfferCompleted { | ||
def result: Either[Throwable, QueueOfferResult] | ||
} | ||
|
||
/** | ||
* A behavior that stashes messages until a response to the SourceQueue#offer | ||
* method is received. | ||
* | ||
* This is to be used only with SourceQueues that use backpressure. | ||
*/ | ||
def waitForQueueOfferCompleted[T](behavior: Behavior[T], stash: Seq[T]): Behavior[T] = | ||
Behaviors | ||
.receive[T] { | ||
case (context, completed: QueueOfferCompleted) => | ||
completed.result match { | ||
case Right(QueueOfferResult.Enqueued) => | ||
stash.foreach(context.self.tell) | ||
|
||
behavior | ||
|
||
case Right(other) => | ||
throw new IllegalStateException(s"Failed to offer to queue: $other") | ||
|
||
case Left(failure) => | ||
throw failure | ||
} | ||
|
||
case (_, other) => | ||
waitForQueueOfferCompleted(behavior, stash = stash :+ other) | ||
} | ||
.orElse(behavior) // handle signals immediately | ||
} |
Oops, something went wrong.