Limit HTTP request rate for spray/akka-http
I think you could rewrite your limit function like this :

def limit[T](requestRateLimiter: RequestRateLimiter[T]) = {
  def remoteAddress = optionalHeaderValuePF { case `Remote-Address`(address) ⇒ address }
  def xForwardedFor = optionalHeaderValuePF { case `X-Forwarded-For`(Seq(address, _*)) ⇒ address }

  for {
    ctx &lt;- extract(identity)
    realIp &lt;- remoteAddress
    proxiedIp &lt;- xForwardedFor
  } yield requestRateLimiter.check(ctx, realIp, proxiedIp) match {
    case Pass(result) =&gt; provide(result)
    case Block(rejection) =&gt; reject(rejection)
  }
}


Here are the two main changes that I've done :


Changed your flatMaps for a "for comprehension" (which imho is more readable)
Removed the types on the declaration of the two variables.


Overall, I find it more readable.