ExecutionContext with Rate and Concurrency Limits

213 Views Asked by At

Suppose I have a simple HTTP client with a naïve method like this:

def httpGet(url: String)(implicit ec: ExecutionContext): Future[String] = Future {
  io.Source.fromURL(url).mkString
}

Suppose also I call it against a server with a rate and concurrency limit. I think these limits should be implemented in ExecutionContext.

The concurrency limit is just the number of threads of the java.util.concurrent.Executor backing some ExecutionContext. The rate limiting should be part of the ExecutionContext too. So we can write a new class extending ExecutionContext to implement the rate and concurrency limits and use an instance of this class to invoke httpGet.

class ExecutionContextWithRateAndConcurrencyLimits(
  numThreads: Int, // for concurrency limit
  rate: Rate       // requests per time unit
) extends ExecutionContex { ... }


val ec = new ExecutionContextWithRateAndConcurrencyLimits(
  numThreads = 100,
  rate = Rate(1000, 1.sec)
)

val fut = httpGet(url)(ec)

Would you agree that rate and concurrency limits should be implemented in ExcecutionContext? Do you know any implementation like that ?

0

There are 0 best solutions below