Skip to content

Commit

Permalink
Merge pull request #2252 from hongwei1/feature/addedFutureWithLimits
Browse files Browse the repository at this point in the history
Feature/added future with limits
  • Loading branch information
simonredfern committed Aug 25, 2023
2 parents cded099 + 6adb007 commit 3d9e7d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
24 changes: 8 additions & 16 deletions obp-api/src/main/scala/code/api/util/FutureUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,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
}
}

}
11 changes: 4 additions & 7 deletions obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 3d9e7d0

Please sign in to comment.