'How to set request throttling on a dispatch HTTP client' post illustration

How to set request throttling on a dispatch HTTP client

avatar

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.

CustomAsyncHttpClient.scala
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
package com.sysgears.example.http.async

import com.ning.http.client.Response
import com.ning.http.client.extra.ThrottleRequestFilter
import dispatch.{Http, Req}

import scala.concurrent.Future

object CustomAsyncHttpClient {

  /**
   * Maximum number of requests to be handled simultaneously.
   */
  private val limit = 10

  /**
   * Dispatch HTTP client.
   */
  private val client = Http.configure(
    _.addRequestFilter(new ThrottleRequestFilter(limit))
  )
  
  /**
   * Executes the request.
   *
   * @param req dispatch request to execute
   * @return dispatch http response, wrapped in a future
   */
  def executeRequest(req: Req): Future[Response] = client(req)
}

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.

build.sbt
1
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.3"

If you're looking for a developer or considering starting a new project,
we are always ready to help!