【问题标题】:WSClient - Too Many Open FilesWSClient - 打开的文件太多
【发布时间】:2019-06-10 11:23:08
【问题描述】:

我在 CentOS 6 上使用 Play Framework 2.4,我的应用程序抛出了这个异常:

java.net.SocketException: Too many open files

我在 Stack Overflow 上搜索了很多主题并尝试了解决方案:

  • 将打开文件数增加到65535;
  • 更改 /etc/security/limits.conf 的硬限制和软限制;
  • 更改 /etc/sysctl.conf 上 fs.file-max 的值;
  • 减少了文件 /proc/sys/net/ipv4/tcp_fin_timeout 的超时时间;

错误不断发生。在另一个网站上,我发现有人面临同样的问题,因为他们没有从 WSClient 调用 close() 方法,但在我的情况下,我正在使用依赖注入:

@Singleton
class RabbitService @Inject()(ws:WSClient) {

  def myFunction() {
    ws.url(“url”).withHeaders(
      "Content-type" -> "application/json",
      "Authorization" -> ("Bearer " + authorization))
      .post(message)
      .map(r => {
      r.status match {
        case 201 => Logger.debug("It Rocks")
        case _ => Logger.error(s"It sucks")
      }
    })

  }

}

如果我改变我的实现来等待结果,它就像一个魅力,但性能很差 - 我想使用 map 函数而不是等待结果:

@Singleton
class RabbitService @Inject()(ws:WSClient) {

  def myFunction() {
    val response = ws.url("url")
      .withHeaders(
        "Content-type" -> "application/json",
        "Authorization" -> ("Bearer " + authorization))
      .post(message)

    Try(Await.result(response, 1 seconds)) match {
      case Success(r) =>
        if(r.status == 201) {
          Logger.debug(s"It rocks")
        } else {
          Logger.error(s"It sucks")
        }
      case Failure(e) => Logger.error(e.getMessage, e)
    }

  }

}

有人知道我该如何解决这个错误吗?我已经尝试了一切,但没有成功。

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

    如果有人遇到同样的问题,你需要在application.conf上配置WSClient——需要设置maxConnectionsTotalmaxConnectionsPerHost

    这就是我解决这个问题的方法。

    https://www.playframework.com/documentation/2.5.x/ScalaWS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 2011-07-18
      • 2019-10-30
      • 2011-01-03
      • 2011-08-05
      • 2014-03-31
      • 2017-03-03
      相关资源
      最近更新 更多