How to set request throttling on a dispatch HTTP client

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

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)
}Code language: PHP (php)

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

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.3"Code language: JavaScript (javascript)
Scala Developer & Technical Lead