2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2014-05-20 16:02:09 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
package akka.stream
|
|
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
import OverflowStrategies._
|
2018-12-05 14:31:43 +01:00
|
|
|
import akka.annotation.{ DoNotInherit, InternalApi }
|
|
|
|
|
import akka.event.Logging
|
|
|
|
|
import akka.event.Logging.LogLevel
|
2016-01-16 12:17:19 -05:00
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
2018-06-09 17:42:56 +09:00
|
|
|
* Represents a strategy that decides how to deal with a buffer of time based operator
|
2016-01-16 12:17:19 -05:00
|
|
|
* that is full but is about to receive a new element.
|
2014-05-20 16:02:09 +02:00
|
|
|
*/
|
2017-10-23 18:49:07 +09:00
|
|
|
@DoNotInherit
|
2018-12-05 14:31:43 +01:00
|
|
|
sealed abstract class DelayOverflowStrategy extends Serializable {
|
|
|
|
|
/** INTERNAL API */
|
|
|
|
|
@InternalApi private[akka] def isBackpressure: Boolean
|
|
|
|
|
}
|
2014-05-20 16:02:09 +02:00
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
final case class BufferOverflowException(msg: String) extends RuntimeException(msg)
|
2017-10-23 18:49:07 +09:00
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
/**
|
|
|
|
|
* Represents a strategy that decides how to deal with a buffer that is full but is
|
|
|
|
|
* about to receive a new element.
|
|
|
|
|
*/
|
2017-10-23 18:49:07 +09:00
|
|
|
@DoNotInherit
|
2018-12-05 14:31:43 +01:00
|
|
|
sealed abstract class OverflowStrategy extends DelayOverflowStrategy {
|
|
|
|
|
/** INTERNAL API */
|
|
|
|
|
@InternalApi private[akka] def logLevel: LogLevel
|
|
|
|
|
def withLogLevel(logLevel: Logging.LogLevel): OverflowStrategy
|
|
|
|
|
}
|
2014-05-20 16:02:09 +02:00
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
private[akka] object OverflowStrategies {
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class DropHead(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): DropHead = DropHead(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class DropTail(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): DropTail = DropTail(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class DropBuffer(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): DropBuffer = DropBuffer(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2015-09-22 18:00:19 +01:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class DropNew(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): DropNew = DropNew(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class Backpressure(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): Backpressure = Backpressure(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = true
|
|
|
|
|
}
|
2014-10-22 11:05:38 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case class Fail(logLevel: LogLevel) extends OverflowStrategy {
|
|
|
|
|
override def withLogLevel(logLevel: LogLevel): Fail = Fail(logLevel)
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2016-01-16 12:17:19 -05:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
private[akka] case object EmitEarly extends DelayOverflowStrategy {
|
|
|
|
|
private[akka] override def isBackpressure: Boolean = false
|
|
|
|
|
}
|
2015-11-25 21:29:35 -05:00
|
|
|
}
|
2014-10-22 11:05:38 +02:00
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
object OverflowStrategy {
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the oldest element from the buffer to make space for
|
|
|
|
|
* the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropHead: OverflowStrategy = DropHead(Logging.DebugLevel)
|
2014-05-20 16:02:09 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the youngest element from the buffer to make space for
|
|
|
|
|
* the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropTail: OverflowStrategy = DropTail(Logging.DebugLevel)
|
2014-05-20 16:02:09 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops all the buffered elements to make space for the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropBuffer: OverflowStrategy = DropBuffer(Logging.DebugLevel)
|
2014-05-20 16:02:09 +02:00
|
|
|
|
2015-06-01 18:08:13 +03:00
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropNew: OverflowStrategy = DropNew(Logging.DebugLevel)
|
2015-06-01 18:08:13 +03:00
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
/**
|
2014-07-22 12:21:53 +02:00
|
|
|
* If the buffer is full when a new element is available this strategy backpressures the upstream publisher until
|
2014-05-20 16:02:09 +02:00
|
|
|
* space becomes available in the buffer.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def backpressure: OverflowStrategy = Backpressure(Logging.DebugLevel)
|
2014-10-22 11:05:38 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-01-30 10:30:56 +01:00
|
|
|
* If the buffer is full when a new element is available this strategy completes the stream with failure.
|
2014-10-22 11:05:38 +02:00
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def fail: OverflowStrategy = Fail(Logging.ErrorLevel)
|
2014-05-20 16:02:09 +02:00
|
|
|
}
|
2015-11-25 21:29:35 -05:00
|
|
|
|
2016-01-16 12:17:19 -05:00
|
|
|
object DelayOverflowStrategy {
|
2015-11-25 21:29:35 -05:00
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element is available this strategy send next element downstream without waiting
|
|
|
|
|
*/
|
|
|
|
|
def emitEarly: DelayOverflowStrategy = EmitEarly
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the oldest element from the buffer to make space for
|
|
|
|
|
* the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropHead: DelayOverflowStrategy = DropHead(Logging.DebugLevel)
|
2015-11-25 21:29:35 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the youngest element from the buffer to make space for
|
|
|
|
|
* the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropTail: DelayOverflowStrategy = DropTail(Logging.DebugLevel)
|
2015-11-25 21:29:35 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops all the buffered elements to make space for the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropBuffer: DelayOverflowStrategy = DropBuffer(Logging.DebugLevel)
|
2015-11-25 21:29:35 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element arrives, drops the new element.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def dropNew: DelayOverflowStrategy = DropNew(Logging.DebugLevel)
|
2015-11-25 21:29:35 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element is available this strategy backpressures the upstream publisher until
|
|
|
|
|
* space becomes available in the buffer.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def backpressure: DelayOverflowStrategy = Backpressure(Logging.DebugLevel)
|
2015-11-25 21:29:35 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the buffer is full when a new element is available this strategy completes the stream with failure.
|
|
|
|
|
*/
|
2018-12-05 14:31:43 +01:00
|
|
|
def fail: DelayOverflowStrategy = Fail(Logging.ErrorLevel)
|
2017-01-04 17:37:10 +01:00
|
|
|
}
|