Oleg Yermolaiev , Scala Developer & Technical Lead
Let's consider the ability to limit the number of HTTP requests being handled by an application simultaneously.
For example, the throttling of HTTP requests being sent by the application could be enabled, among other, by the following reasons:
- Conserving system resources. Every open connection results in additional CPU load and memory usage. Thus, the smaller number of connections application maintains, the less system resources it uses.
- Preventing system from flooding external APIs. There are many services which have rate limit rules depending on running mode, subscription type, etc. Exceeding these limits may lead to various penalties during further APIs usage. Request throttling may help you to implement a solution that will be balancing the number of outgoing requests and the API quotas.
If you are using net.databinder.dispatch library then request throttling can be enabled by adding com.ning.http.client.extra.ThrottleRequestFilter request filter when configuring an HTTP executor. ThrottleRequestFilter blocks outgoing request if the maximum number of allowed connections is reached, waiting for the response to any active request before executing the next one from a queue.
The maximum number of active connections must be passed in as a constructor parameter.
The example of a simple dispatch-based asynchronous HTTP client is in the code snippet below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Also, please, ensure that an appropriate version of dispatch library is included in the application build before compiling code from the snippet above. Currently, the latest available build is 0.11.3 for scala 2.10 & 2.11.