All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spray.io.PreventHalfClosedConnections.scala Maven / Gradle / Ivy

Go to download

A suite of lightweight Scala libraries for building and consuming RESTful web services on top of Akka

There is a newer version: 1.3.3
Show newest version
package spray.io

import akka.io.Tcp

/**
 * A pipeline stage that prevents half-closed connections by actively closing this
 * side of the connection when a Tcp.PeerClosed command was received.
 *
 * It is only activated when SslTlsSupport is disabled because
 * SslTlsSupport has the same closing semantics as this stage.
 */
object PreventHalfClosedConnections {
  def apply(sslEnabled: Boolean): RawPipelineStage[SslTlsContext] =
    new OptionalPipelineStage[SslTlsContext] {
      def enabled(context: SslTlsContext): Boolean = !isSslTlsSupportEnabled(context)
      def isSslTlsSupportEnabled(context: SslTlsContext) =
        sslEnabled && context.sslEngine.isDefined

      def applyIfEnabled(context: SslTlsContext,
                         commandPL: CPL,
                         eventPL: EPL): Pipelines = new Pipelines with DynamicEventPipeline {
        def initialEventPipeline = connected
        def commandPipeline = commandPL

        def connected: EPL = {
          case Tcp.PeerClosed ⇒
            commandPL(Tcp.ConfirmedClose)
            eventPipeline.become(closingOurSide)

          case ev ⇒ eventPL(ev)
        }
        def closingOurSide: EPL = {
          case _: Tcp.ConnectionClosed ⇒ eventPL(Tcp.PeerClosed)
          case ev                      ⇒ eventPL(ev)
        }
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy