我一直在开发一个小型/中型web应用程序,大约有10个端点。它应该能够同时处理数百个并发请求。
由于我的公司政策,我必须为我的控制器使用javax.ws.rs,所以每个控制器方法都返回一个javax.ws.rs.core.Response
每个控制器都依赖于一个服务,它负责访问数据库( dynamoDb,使用v2非阻塞java dynamoDb sdk),通过http (使用org.asynhttpclient)从其他微服务获取数据,并创建表示我的响应的case类,因此我的控制器可以序列化它并将其作为javax.ws.rs.core.Response中的主体返回。
我的应用程序中的每个组件都是异步和非阻塞的,我的服务返回Future[Either[MyAppError, CaseClassRepresentingMyResponse]]。然后,在控制器中,我在最后等待未来的结果,并创建javax.ws.rs.core.Response
所以我的控制器看起来像这样:
class BarController(barService: BarService)(implicit ec: ExecutionContext) {
def getFoo(userId: BigInt, fooId: String): Response = withMetrics(Bar.ServiceName, Bar.GetFoo) {
val fooResponse = for {
_ <- EitherT(validateUserIdAndFooId(userId, fooId))
foo <- EitherT(barServiceService.getFoo(userId, fooId))
} yield foo
Await.result(fooResponse.value, 10 seconds) match {
case Success(Right(r)) => buildOkResponse(r)
case Success(Left(NotFound)) => HttpResponses.notFound
(.... a bunch of other cases ......)
}
}
}(我正在使用cats EitherT来处理Future[Either[E, A]]。)
我的应用程序中的每个组件都会收到一个隐式的ExecutionContext,并返回一个Future[Either[E,A]]。
现在,我刚刚完成了所有的编码和测试,我需要在我的配置中提供一个适当的ExecutionContext。
ExecutionContext.global就足够了吗?考虑到我的代码从不阻塞(因为我使用的是DynamoDb非阻塞sdk和org.asynchttpclient),或者我应该创建一个不同的ExecutionContext?也许是来自FixedThreadPool的?或者是ForkJoinPool?
发布于 2020-08-05 06:48:25
https://stackoverflow.com/questions/62997039
复制相似问题