带有 Spray Akka 超线程的 Scala 服务器

Scala server with Spray Akka overthreading

我在我的 freebsd 1CPU/2GB 服务器上使用 spray.io 和 akka.io 我正面临 线程问题。当我遇到 OutOfMemory 异常时,我开始注意到它 因为 "can't create native thread".

我经常检查 Thread.activeCount(),我发现它增长很快。 目前我使用这些设置:

myapp-namespace-akka {
  akka {
    loggers = ["akka.event.Logging$DefaultLogger"]
    loglevel = "DEBUG"
    stdout-loglevel = "DEBUG"

    actor {
      deployment {
        default {
          dispatcher = "nio-dispatcher"
          router = "round-robin"
          nr-of-instances = 1
        }
      }

      debug {
        receive = on
        autoreceive = on
        lifecycle = on
      }

      nio-dispatcher {
        type = "Dispatcher"
        executor = "fork-join-executor"

        fork-join-executor {
          parallelism-min = 8
          parallelism-factor = 1.0
          parallelism-max = 16
          task-peeking-mode = "FIFO"
        }

        shutdown-timeout = 4s
        throughput = 4
        throughput-deadline-time = 0ms
        attempt-teamwork = off
        mailbox-requirement = ""
      }

      aside-dispatcher {
        type = "Dispatcher"
        executor = "fork-join-executor"

        fork-join-executor {
          parallelism-min = 8
          parallelism-factor = 1.0
          parallelism-max = 32
          task-peeking-mode = "FIFO"
        }

        shutdown-timeout = 4s
        throughput = 4
        throughput-deadline-time = 0ms
        attempt-teamwork = on
        mailbox-requirement = ""
      }
    }
  }
}

我希望 nio-dispatcher 成为我的默认非阻塞(比如说单线程) 调度员。我在 aside-dispatcher.

上执行我所有的期货(数据库、网络查询)

我通过我的应用程序获取我的上下文,如下所示:

trait Contexts {
  def system: ActorSystem
  def nio: ExecutionContext
  def aside: ExecutionContext
}

object Contexts {
  val Scope = "myapp-namespace-akka"
}

class ContextsImpl(settings: Config) extends Contexts {
  val System = "myapp-namespace-akka"
  val NioDispatcher = "akka.actor.nio-dispatcher"
  val AsideDispatcher = "akka.actor.aside-dispatcher"
  val Settings = settings.getConfig(Contexts.Scope)

  override val system: ActorSystem = ActorSystem(System, Settings)
  override val nio: ExecutionContext = system.dispatchers.lookup(NioDispatcher)
  override val aside: ExecutionContext = system.dispatchers.lookup(AsideDispatcher)
}

// Spray trait mixed to service actors
trait ImplicitAsideContext {
  this: EnvActor =>
  implicit val aside = env.contexts.aside
}

我认为我确实搞砸了配置或实现。帮我出去。 通常我现在在我的应用程序上看到数千个线程直到它崩溃(我设置 freebsd 预处理限制为 5000)。

如果您的应用程序确实启动了如此多的线程,这通常可以追溯到 ForkJoinPools 内部的阻塞行为(糟糕的坏事!),我在此处的答案中详细解释了这个问题:,所以你可能想在那里阅读它并验证在你的应用程序中创建了哪些线程以及为什么 - ForkJoinPool 没有静态上限。