From 7fc63dc4118e8cc76842599354868845e8dc5309 Mon Sep 17 00:00:00 2001 From: hongwei Date: Thu, 24 Aug 2023 21:15:03 +0800 Subject: [PATCH 1/2] feature/added futureWithLimits function - remove Promise for futureWithLimits --- .../main/scala/code/api/util/FutureUtil.scala | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/FutureUtil.scala b/obp-api/src/main/scala/code/api/util/FutureUtil.scala index 9c0ceaeed5..e0ee8bf216 100644 --- a/obp-api/src/main/scala/code/api/util/FutureUtil.scala +++ b/obp-api/src/main/scala/code/api/util/FutureUtil.scala @@ -74,25 +74,17 @@ object FutureUtil { } def futureWithLimits[T](future: Future[T], serviceName: String)(implicit ec: ExecutionContext): Future[T] = { - incrementFutureCounter(serviceName) - - // Promise will be fulfilled with either the callers Future - val p = Promise[T] - - future.map { - result => - if (p.trySuccess(result)) { + future + .map( + value => { decrementFutureCounter(serviceName) - } - }.recover { - case e: Exception => - if (p.tryFailure(e)) { + value + }).recover{ + case exception: Throwable => decrementFutureCounter(serviceName) - } - } - - p.future + throw exception + } } } From 6adb00730bcca1066e33216271b619ed96c30b90 Mon Sep 17 00:00:00 2001 From: hongwei Date: Thu, 24 Aug 2023 21:33:51 +0800 Subject: [PATCH 2/2] feature/added futureWithLimits function - use 503 for ServiceIsTooBusy --- .../main/scala/code/api/v3_0_0/APIMethods300.scala | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala b/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala index 1b6923e809..7a88ffd970 100644 --- a/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala +++ b/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala @@ -25,7 +25,7 @@ import code.scope.Scope import code.search.elasticsearchWarehouse import code.users.Users import code.util.Helper -import code.util.Helper.booleanToBox +import code.util.Helper.{booleanToBox, booleanToFuture} import code.views.Views import code.views.system.ViewDefinition import com.github.dwickern.macros.NameOf.nameOf @@ -2378,13 +2378,10 @@ trait APIMethods300 { cc => for { (_, callContext) <- anonymousAccess(cc) - (banks, callContext) <- if(canOpenFuture("NewStyle.function.getBanks")) { - FutureUtil.futureWithLimits(NewStyle.function.getBanks(callContext), "NewStyle.function.getBanks") - } else { - Future { - throw new RuntimeException(ServiceIsTooBusy +"Current Service(NewStyle.function.getBanks) ") - } + _ <- booleanToFuture(ServiceIsTooBusy +"Current Service(NewStyle.function.getBanks)", 503, callContext) { + canOpenFuture("NewStyle.function.getBanks") } + (banks, callContext) <- FutureUtil.futureWithLimits(NewStyle.function.getBanks(callContext), "NewStyle.function.getBanks") } yield (JSONFactory300.createBanksJson(banks), HttpCode.`200`(callContext)) }