是否使用源队列实现线程安全的akka​​-http中的连接池?

Niv*_*Nik 6 scala connection-pooling akka-stream akka-http

参考以下提到的实现:

http://doc.akka.io/docs/akka-http/10.0.5/scala/http/client-side/host-level.html

val poolClientFlow = Http().cachedHostConnectionPool[Promise[HttpResponse]]("akka.io")
val queue =
  Source.queue[(HttpRequest, Promise[HttpResponse])](QueueSize, OverflowStrategy.dropNew)
    .via(poolClientFlow)
    .toMat(Sink.foreach({
      case ((Success(resp), p)) => p.success(resp)
      case ((Failure(e), p))    => p.failure(e)
    }))(Keep.left)
    .run()
Run Code Online (Sandbox Code Playgroud)

从多个线程提供队列http请求是否可以安全线程?如果不是,那么实施此类要求的最佳方式是什么?也许使用一个专门的演员?

Fre*_* A. 2

不,根据api 文档,它不是线程安全的:SourceQueue that current source is materialized to is for single thread usage only.

一个专门的演员会很好地工作,但是,如果可以的话,使用Source.actorRefdoc link)而不是Source.queue会更容易。

一般来说, 的缺点Source.actorRef是缺乏背压,但当您使用 时OverflowStrategy.dropNew,很明显您不会期望背压。因此,您可以使用 获得相同的行为Source.actorRef