Skip to content

Instantly share code, notes, and snippets.

@mbarton
Created July 16, 2014 14:46
Show Gist options
  • Save mbarton/310dd3865f3581ae5be1 to your computer and use it in GitHub Desktop.
Save mbarton/310dd3865f3581ae5be1 to your computer and use it in GitHub Desktop.
Akka HTTP deadlock
import akka.actor.{Actor, ActorSystem, Props}
import akka.http.Http
import akka.http.model.HttpMethods.POST
import akka.http.model.{HttpRequest, HttpResponse}
import akka.io.IO
import akka.stream.scaladsl.Flow
import akka.stream.{FlowMaterializer, MaterializerSettings}
import scala.concurrent.Future
import scala.concurrent.duration._
class TestServer extends Actor {
implicit val execContext = context.dispatcher
val requestTimeout = 10.seconds
val materializer = FlowMaterializer(MaterializerSettings())
override def preStart() = {
IO(Http)(context.system) ! Http.Bind("0.0.0.0", 3030)
}
def handle(request: HttpRequest): Future[HttpResponse] = request match {
case req if req.method == POST =>
req.entity.toStrict(requestTimeout, materializer).map { entity =>
val data = Charset.forName("UTF-8").decode(entity.data.asByteBuffer)
println(s"-> $data")
HttpResponse(entity = entity.data)
}
// Uncomment to fix!
// Future { HttpResponse(entity = "hello") }
case _ =>
Future { HttpResponse(404) }
}
def receive = {
case Http.ServerBinding(_, incoming) =>
Flow(incoming).foreach { self ! _ }.consume(materializer)
case Http.IncomingConnection(remote, in, out) =>
Flow(in).mapFuture(handle).produceTo(materializer, out)
}
}
object TestServerApp extends App {
val sys = ActorSystem()
sys.actorOf(Props[TestServer])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment