Skip to content
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

Websocket: Update To Unsafe Signal for Close Rather than Pure Signal #1631

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ package org.http4s
package blazecore
package websocket

import java.util.concurrent.atomic.AtomicReference

import cats.effect._
import cats.effect.implicits._
import cats.implicits._
import fs2._
import fs2.async.mutable.Signal
import org.http4s.{websocket => ws4s}
import org.http4s.blaze.pipeline.{Command, LeafBuilder, TailStage, TrunkBuilder}
import org.http4s.blaze.pipeline.stages.SerializingStage
import org.http4s.blaze.util.Execution.{directec, trampoline}
import org.http4s.websocket.WebsocketBits._

import scala.concurrent.ExecutionContext
import scala.util.{Failure, Success}
import cats.effect.IO

class Http4sWSStage[F[_]](ws: ws4s.Websocket[F])(implicit F: Effect[F], val ec: ExecutionContext)
extends TailStage[WebSocketFrame] {

def name: String = "Http4s WebSocket Stage"

private val deadSignal: F[Signal[F, Boolean]] = async.signalOf[F, Boolean](false)
private val deadSignal: AtomicReference[Boolean] = new AtomicReference(false)

//////////////////////// Source and Sink generators ////////////////////////

Expand All @@ -40,29 +42,22 @@ class Http4sWSStage[F[_]](ws: ws4s.Websocket[F])(implicit F: Effect[F], val ec:
channelRead().onComplete {
case Success(ws) =>
ws match {
case Close(_) =>
for {
t <- deadSignal.map(_.set(true))
} yield {
t.runAsync(_ => IO.unit).unsafeRunSync()
cb(Left(Command.EOF))
}

case c@Close(_) =>
deadSignal.set(true)
cb(Right(c)) // With Dead Signal Set, Return callback with the Close Frame
case Ping(d) =>
channelWrite(Pong(d)).onComplete {
case Success(_) => go()
case Failure(Command.EOF) => cb(Left(Command.EOF))
case Failure(t) => cb(Left(t))
}(trampoline)

case Pong(_) => go()
case f => cb(Right(f))
}

case Failure(Command.EOF) => cb(Left(Command.EOF))
case Failure(e) => cb(Left(e))
}(trampoline)

go()
}
Stream.repeatEval(t)
Expand All @@ -80,18 +75,18 @@ class Http4sWSStage[F[_]](ws: ws4s.Websocket[F])(implicit F: Effect[F], val ec:
val onStreamFinalize: F[Unit] =
for {
dec <- F.delay(count.decrementAndGet())
_ <- deadSignal.map(signal => if (dec == 0) signal.set(true))
_ <- if (dec == 0) F.delay(deadSignal.set(true)) else ().pure[F]
} yield ()

// Effect to send a close to the other endpoint
val sendClose: F[Unit] = F.delay(sendOutboundCommand(Command.Disconnect))

val wsStream = for {
dead <- deadSignal
in = inputstream.to(ws.receive).onFinalize(onStreamFinalize)
out = ws.send.onFinalize(onStreamFinalize).to(snk).drain
merged <- in.mergeHaltR(out).interruptWhen(dead).onFinalize(sendClose).compile.drain
} yield merged
val wsStream = inputstream.to(ws.receive).onFinalize(onStreamFinalize)
.mergeHaltR(ws.send.onFinalize(onStreamFinalize).to(snk).drain)
Copy link
Member

@SystemFw SystemFw Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foo.mergeHaltR(bar.drain) can probably be bar.concurrently(foo) now, but with the new merge on the way this should work as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually scratch that sorry, you nee the output of foo, but want to complete when bar is done

.interruptWhen(Stream.repeatEval(F.delay(deadSignal.get()))) // Check The Status of the deadSignal Atomic Ref
.onFinalize(sendClose)
.compile
.drain

async.unsafeRunAsync {
wsStream.attempt.flatMap {
Expand All @@ -108,7 +103,7 @@ class Http4sWSStage[F[_]](ws: ws4s.Websocket[F])(implicit F: Effect[F], val ec:
}

override protected def stageShutdown(): Unit = {
deadSignal.map(_.set(true)).runAsync(_ => IO.unit).unsafeRunSync()
deadSignal.set(true)
super.stageShutdown()
}
}
Expand Down