-
Notifications
You must be signed in to change notification settings - Fork 3.5k
+str #15081 Rate detached ops #15244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
drewhk
merged 1 commit into
akka:release-2.3-dev
from
drewhk:wip-15081-rate-detach-ops-drewhk
May 23, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
akka-stream/src/main/scala/akka/stream/OverflowStrategy.scala
This file contains hidden or 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,55 @@ | ||
| /** | ||
| * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> | ||
| */ | ||
| package akka.stream | ||
|
|
||
| /** | ||
| * Represents a strategy that decides how to deal with a buffer that is full but is about to receive a new element. | ||
| */ | ||
| sealed abstract class OverflowStrategy | ||
|
|
||
| object OverflowStrategy { | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| private[akka] final case object DropHead extends OverflowStrategy | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| private[akka] final case object DropTail extends OverflowStrategy | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| private[akka] final case object DropBuffer extends OverflowStrategy | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| private[akka] final case object Backpressure extends OverflowStrategy | ||
|
|
||
| /** | ||
| * If the buffer is full when a new element arrives, drops the oldest element from the buffer to make space for | ||
| * the new element. | ||
| */ | ||
| def dropHead: OverflowStrategy = DropHead | ||
|
|
||
| /** | ||
| * If the buffer is full when a new element arrives, drops the youngest element from the buffer to make space for | ||
| * the new element. | ||
| */ | ||
| def dropTail: OverflowStrategy = DropTail | ||
|
|
||
| /** | ||
| * If the buffer is full when a new element arrives, drops all the buffered elements to make space for the new element. | ||
| */ | ||
| def dropBuffer: OverflowStrategy = DropBuffer | ||
|
|
||
| /** | ||
| * If the buffer is full when a new element is available this strategy backpressures the upstream producer until | ||
| * space becomes available in the buffer. | ||
| */ | ||
| def backpressure: OverflowStrategy = Backpressure | ||
| } |
This file contains hidden or 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 hidden or 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
83 changes: 83 additions & 0 deletions
83
akka-stream/src/main/scala/akka/stream/impl/FixedSizeBuffer.scala
This file contains hidden or 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,83 @@ | ||
| /** | ||
| * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> | ||
| */ | ||
| package akka.stream.impl | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| private[akka] object FixedSizeBuffer { | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| * | ||
| * Returns a fixed size buffer backed by an array. The buffer implementation DOES NOT check agains overflow or | ||
| * underflow, it is the responsibility of the user to track or check the capacity of the buffer before enqueueing | ||
| * dequeueing or dropping. | ||
| * | ||
| * Returns a specialized instance for power-of-two sized buffers. | ||
| */ | ||
| def apply(size: Int): FixedSizeBuffer = | ||
| if (((size - 1) & size) == 0) new PowerOfTwoFixedSizeBuffer(size) | ||
| else new ModuloFixedSizeBuffer(size) | ||
|
|
||
| sealed abstract class FixedSizeBuffer(val size: Int) { | ||
| protected var readIdx = 0 | ||
| protected var writeIdx = 0 | ||
| private var remainingCapacity = size | ||
| private val buffer = Array.ofDim[Any](size) | ||
|
|
||
| protected def incWriteIdx(): Unit | ||
| protected def decWriteIdx(): Unit | ||
| protected def incReadIdx(): Unit | ||
|
|
||
| def isFull: Boolean = remainingCapacity == 0 | ||
| def isEmpty: Boolean = remainingCapacity == size | ||
|
|
||
| def enqueue(elem: Any): Unit = { | ||
| buffer(writeIdx) = elem | ||
| incWriteIdx() | ||
| remainingCapacity -= 1 | ||
| } | ||
|
|
||
| def dequeue(): Any = { | ||
| val result = buffer(readIdx) | ||
| dropHead() | ||
| result | ||
| } | ||
|
|
||
| def clear(): Unit = { | ||
| java.util.Arrays.fill(buffer.asInstanceOf[Array[Object]], null) | ||
| readIdx = 0 | ||
| writeIdx = 0 | ||
| remainingCapacity = size | ||
| } | ||
|
|
||
| def dropHead(): Unit = { | ||
| buffer(readIdx) = null | ||
| incReadIdx() | ||
| remainingCapacity += 1 | ||
| } | ||
|
|
||
| def dropTail(): Unit = { | ||
| decWriteIdx() | ||
| //buffer(writeIdx) = null | ||
| remainingCapacity += 1 | ||
| } | ||
| } | ||
|
|
||
| private final class ModuloFixedSizeBuffer(_size: Int) extends FixedSizeBuffer(_size) { | ||
| override protected def incReadIdx(): Unit = readIdx = (readIdx + 1) % size | ||
| override protected def decWriteIdx(): Unit = writeIdx = (writeIdx + size - 1) % size | ||
| override protected def incWriteIdx(): Unit = writeIdx = (writeIdx + 1) % size | ||
| } | ||
|
|
||
| private final class PowerOfTwoFixedSizeBuffer(_size: Int) extends FixedSizeBuffer(_size) { | ||
| private val Mask = size - 1 | ||
| override protected def incReadIdx(): Unit = readIdx = (readIdx + 1) & Mask | ||
| override protected def decWriteIdx(): Unit = writeIdx = (writeIdx - 1) & Mask | ||
| override protected def incWriteIdx(): Unit = writeIdx = (writeIdx + 1) & Mask | ||
| } | ||
|
|
||
| } | ||
|
|
This file contains hidden or 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
92 changes: 92 additions & 0 deletions
92
akka-stream/src/main/scala/akka/stream/impl/RateDetachedProcessors.scala
This file contains hidden or 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,92 @@ | ||
| /** | ||
| * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> | ||
| */ | ||
| package akka.stream.impl | ||
|
|
||
| import akka.stream.{ OverflowStrategy, MaterializerSettings } | ||
|
|
||
| class ConflateImpl(_settings: MaterializerSettings, seed: Any ⇒ Any, aggregate: (Any, Any) ⇒ Any) extends ActorProcessorImpl(_settings) { | ||
| var conflated: Any = null | ||
|
|
||
| val waitNextZero: TransferPhase = TransferPhase(primaryInputs.NeedsInput) { () ⇒ | ||
| conflated = seed(primaryInputs.dequeueInputElement()) | ||
| nextPhase(conflateThenEmit) | ||
| } | ||
|
|
||
| val conflateThenEmit: TransferPhase = TransferPhase(primaryInputs.NeedsInput || primaryOutputs.NeedsDemand) { () ⇒ | ||
| if (primaryInputs.inputsAvailable) conflated = aggregate(conflated, primaryInputs.dequeueInputElement()) | ||
| if (primaryOutputs.demandAvailable) { | ||
| primaryOutputs.enqueueOutputElement(conflated) | ||
| conflated = null | ||
| nextPhase(waitNextZero) | ||
| } | ||
| } | ||
|
|
||
| nextPhase(waitNextZero) | ||
| } | ||
|
|
||
| class ExpandImpl(_settings: MaterializerSettings, seed: Any ⇒ Any, extrapolate: Any ⇒ (Any, Any)) extends ActorProcessorImpl(_settings) { | ||
| var extrapolateState: Any = null | ||
|
|
||
| val waitFirst: TransferPhase = TransferPhase(primaryInputs.NeedsInput) { () ⇒ | ||
| extrapolateState = seed(primaryInputs.dequeueInputElement()) | ||
| nextPhase(emitFirst) | ||
| } | ||
|
|
||
| val emitFirst: TransferPhase = TransferPhase(primaryOutputs.NeedsDemand) { () ⇒ | ||
| emitExtrapolate() | ||
| nextPhase(extrapolateOrReset) | ||
| } | ||
|
|
||
| val extrapolateOrReset: TransferPhase = TransferPhase(primaryInputs.NeedsInputOrComplete || primaryOutputs.NeedsDemand) { () ⇒ | ||
| if (primaryInputs.inputsDepleted) nextPhase(completedPhase) | ||
| else if (primaryInputs.inputsAvailable) { | ||
| extrapolateState = seed(primaryInputs.dequeueInputElement()) | ||
| nextPhase(emitFirst) | ||
| } else emitExtrapolate() | ||
| } | ||
|
|
||
| def emitExtrapolate(): Unit = { | ||
| val (emit, nextState) = extrapolate(extrapolateState) | ||
| primaryOutputs.enqueueOutputElement(emit) | ||
| extrapolateState = nextState | ||
| } | ||
|
|
||
| nextPhase(waitFirst) | ||
| } | ||
|
|
||
| class BufferImpl(_settings: MaterializerSettings, size: Int, overflowStrategy: OverflowStrategy) extends ActorProcessorImpl(_settings) { | ||
| import OverflowStrategy._ | ||
|
|
||
| val buffer = FixedSizeBuffer(size) | ||
|
|
||
| val dropAction: () ⇒ Unit = overflowStrategy match { | ||
| case DropHead ⇒ buffer.dropHead | ||
| case DropTail ⇒ buffer.dropTail | ||
| case DropBuffer ⇒ buffer.clear | ||
| case Backpressure ⇒ () ⇒ nextPhase(bufferFull) | ||
| } | ||
|
|
||
| val bufferEmpty: TransferPhase = TransferPhase(primaryInputs.NeedsInput) { () ⇒ | ||
| buffer.enqueue(primaryInputs.dequeueInputElement()) | ||
| nextPhase(bufferNonEmpty) | ||
| } | ||
|
|
||
| val bufferNonEmpty: TransferPhase = TransferPhase(primaryInputs.NeedsInput || primaryOutputs.NeedsDemand) { () ⇒ | ||
| if (primaryOutputs.demandAvailable) { | ||
| primaryOutputs.enqueueOutputElement(buffer.dequeue()) | ||
| if (buffer.isEmpty) nextPhase(bufferEmpty) | ||
| } else { | ||
| if (buffer.isFull) dropAction() | ||
| else buffer.enqueue(primaryInputs.dequeueInputElement()) | ||
| } | ||
| } | ||
|
|
||
| val bufferFull: TransferPhase = TransferPhase(primaryOutputs.NeedsDemand) { () ⇒ | ||
| primaryOutputs.enqueueOutputElement(buffer.dequeue()) | ||
| if (buffer.isEmpty) nextPhase(bufferEmpty) | ||
| else nextPhase(bufferNonEmpty) | ||
| } | ||
|
|
||
| nextPhase(bufferEmpty) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can leave out the
TransferPhasetype annotation for these internal vals, for readabiltyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compiler complains of recursive types if I do that