如何使用Cats-effect IO在Play应用程序中实现多个线程池

时间:2018-10-19 16:11:29

标签: scala playframework scala-cats playframework-2.6 cats-effect

在我的Play应用程序中,我使用cats-effect的{​​{1}}而不是控制器中的IO来服务我的请求,如下所示(超级简化):

Future

然后在Play的默认线程池上处理请求(异步)。现在,我想实现多个线程池来处理不同种类的请求。如果我使用def handleServiceResult(serviceResult: ServiceResult): Result = ... def serviceMyRequest(request: Request): IO[ServiceResult] = ... def myAction = Action { request => handleServiceResult( serviceMyRequest(request).unsafeRunSync() ) } ,我可以这样做:

Future

但是我没有使用val myCustomExecutionContext: ExecutionContext = ... def serviceMyRequest(request: Request): Future[ServiceResult] = ... def myAction = Action.async { request => Future(serviceMyRequest(request))(myCustomExecutionContext) .map(handleServiceResult)(defaultExecutionContext) } ,我正在使用Future,并且我不确定实现它的正确方法。这看起来很有希望,但似乎有些笨拙:

IO

这是实现它的正确方法吗?这里有最佳实践吗?我搞砸了吗?谢谢。

1 个答案:

答案 0 :(得分:1)

好吧,由于这似乎不是很好的基础,所以我最终实现了这一点:

trait PlayIO { self: BaseControllerHelpers =>

  implicit class IOActionBuilder[A](actionBuilder: ActionBuilder[Request, A]) {

    def io(block: Request[A] => IO[Result]): Action[A] = {
      actionBuilder.apply(block.andThen(_.unsafeRunSync()))
    }

    def io(executionContext: ExecutionContext)(block: Request[A] => IO[Result]): Action[A] = {
      val shiftedBlock = block.andThen(IO.shift(executionContext) *> _ <* IO.shift(defaultExecutionContext))
      actionBuilder.apply(shiftedBlock.andThen(_.unsafeRunSync()))
    }

  }

}

然后(使用问题的框架)如果我将PlayIO混入控制器中,我可以做到这一点,

val myCustomExecutionContext: ExecutionContext = ...

def handleServiceResult(serviceResult: ServiceResult): Result = ...

def serviceMyRequest(request: Request): IO[ServiceResult] = ...

def myAction = Action.io(myCustomExecutionContext) { request =>
  serviceMyRequest(request).map(handleServiceResult)
}

使我在myCustomExecutionContext上执行操作的代码块,然后在完成后将线程转移回Play的默认执行上下文。

更新

这有点灵活:

trait PlayIO { self: BaseControllerHelpers =>

  implicit class IOActionBuilder[R[_], A](actionBuilder: ActionBuilder[R, A]) {

    def io(block: R[A] => IO[Result]): Action[A] = {
      actionBuilder.apply(block.andThen(_.unsafeRunSync()))
    }

    def io(executionContext: ExecutionContext)(block: R[A] => IO[Result]): Action[A] = {
      if (executionContext == defaultExecutionContext) io(block) else {
        val shiftedBlock = block.andThen(IO.shift(executionContext) *> _ <* IO.shift(defaultExecutionContext))
        io(shiftedBlock)
      }
    }

  }

}

Update2:

根据上面的评论,这将确保我们始终切换回默认线程池:

trait PlayIO { self: BaseControllerHelpers =>

  implicit class IOActionBuilder[R[_], A](actionBuilder: ActionBuilder[R, A]) {

    def io(block: R[A] => IO[Result]): Action[A] = {
      actionBuilder.apply(block.andThen(_.unsafeRunSync()))
    }

    def io(executionContext: ExecutionContext)(block: R[A] => IO[Result]): Action[A] = {
      if (executionContext == defaultExecutionContext) io(block) else {
        val shiftedBlock = block.andThen { ioResult =>
          IO.shift(executionContext).bracket(_ => ioResult)(_ => IO.shift(defaultExecutionContext))
        }
        io(shiftedBlock)
      }
    }

  }

}