Spray's `detach` Directive

后端 未结 2 1324
闹比i
闹比i 2021-01-15 20:32

Given the following Spray code:

object Main extends App with SimpleRoutingApp {

  implicit val system = ActorSystem(\"my-system\")

  val pipeline: HttpRequ         


        
相关标签:
2条回答
  • 2021-01-15 20:46

    detach is usually needed because routing runs synchronously in an actor. This means that while an HttpRequest is routed, the actor cannot process any other messages at the same time.

    However, routing bits that are asynchronous like completing with a Future or using one of the FutureDirectives will also free the original routing actor for new requests.

    So, in cases where routing itself is the bottleneck or you complete a request synchronously, adding detach may help. In your case above, you already complete with a Future and have a relatively simple routing structure in which case adding detach won't help much (or may even introduce a tiny bit of latency).

    Also, detach comes with some inconsistencies you can read about here:

    • https://github.com/spray/spray/issues/717
    • https://github.com/spray/spray/issues/872

    An alternative to using detach is using per-request-actors.

    In akka-http, routing is implemented on top of Futures to be as asynchronous as possible and not confined to an actor any more so that detach isn't needed and was removed therefore.

    0 讨论(0)
  • 2021-01-15 21:02

    Without detach spray will process all requests one by one, while with detach it'll process them parallel. If you can process this requests in parallel, you'd better use detach for better performance.

    0 讨论(0)
提交回复
热议问题