I have Play! on Scala application which communicates with another server by sending http request. That system has limitation: only 5 http requests can be proceeded simultaniously for one token.
I've written this method:
import scala.concurrent.ExecutionContext.Implicits.global
private def sendApiRequest(method: String, params: Option[JsValue] = None)(implicit token: String): Future[JsObject] = {
if (concurrentRequests.get(token).isEmpty) {
concurrentRequests += token -> 1
} else {
concurrentRequests += token -> (concurrentRequests.get(token).get + 1)
}
println(s"$token: ${concurrentRequests.get(token).get}")
val request = WS.url(API_URL)
.withMethod("POST")
.withBody(Json.obj(
"application_id" -> clientId,
"method" -> method,
"token" -> token,
"param" -> params
))
request.execute().map(response => {
val result = response.json.as[JsObject]
if (!result.keys.contains("data")) {
throw new Exception(result.toString())
} else {
result
}
})
}
And there are actors which use this method and i get that exception after couple seconds.
My question is: How can i control number of features in 'RUNNING MODE'? May be i should use another execution context instead of default one? Explain me please or give good introduction for execution context, threads, etc
I want to get information from remote service as fast as possible not by sending one by one
Thank you!