简体   繁体   English

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

[英]How to implement multiple thread pools in a Play application using cats-effect IO

In my Play application, I service my requests usings cats-effect 's IO , instead of Future in the controller, like this (super-simplified): 在我的Play应用程序中,我使用cats-effectIO而不是控制器中的Future来服务我的请求,如下所示(超级简化):

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

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

def myAction = Action { request =>
  handleServiceResult(
    serviceMyRequest(request).unsafeRunSync()
  )
}

Requests are then processed (asynchronously) on Play's default thread pool. 然后在Play的默认线程池上(异步)处理请求。 Now, I want to implement multiple thread pools to handle different sorts of requests. 现在,我想实现多个线程池来处理不同种类的请求。 Were I using Future s, I could do this: 如果我使用Future ,我可以这样做:

val myCustomExecutionContext: ExecutionContext = ...

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

def myAction = Action.async { request =>
  Future(serviceMyRequest(request))(myCustomExecutionContext)
    .map(handleServiceResult)(defaultExecutionContext)
}

But I'm not using Future s, I'm using IO , and I'm not sure about the right way to go about implementing it. 但是我没有使用Future ,我正在使用IO ,并且我不确定实现它的正确方法。 This looks promising, but seems a bit clunky: 这看起来很有希望,但似乎有些笨拙:

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

def myAction = Action { request =>
  val ioServiceResult = for {
    _ <- IO.shift(myCustomExecutionContext)
    serviceResult <- serviceMyRequest(request)
    _ <- IO.shift(defaultExecutionContext)
  } yield {
    serviceResult
  }
  handleServiceResult(ioServiceResult.unsafeRunSync())
}

Is this the right way to implement it? 这是实现它的正确方法吗? Is there a best practice here? 这里有最佳实践吗? Am I screwing up badly? 我搞砸了吗? Thanks. 谢谢。

Ok, so since this doesn't seem to be well-trodden ground, this is what I ended up implementing: 好的,因为这似乎不是很好的基础,所以我最终实现了这一点:

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()))
    }

  }

}

Then (using the framework from the question) if I mix PlayIO into the controller, I can do this, 然后(使用问题中的框架)是否将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)
}

such that I execute the action's code block on myCustomExecutionContext and then, once complete, thread-shift back to Play's default execution context. 这样我就可以在myCustomExecutionContext上执行操作的代码块,然后在完成后将线程转移回Play的默认执行上下文。

Update: 更新:

This is a bit more flexible: 这有点灵活:

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: UPDATE2:

Per the comment above, this will ensure we always shift back to the default thread pool: 根据上面的评论,这将确保我们始终切换回默认线程池:

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)
      }
    }

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM