Hi Raghav,

You cannot achieve this with the built in tools, but a simple custom
directive will do the trick. Something like this:

def createConcurrentLimiter(maxConcurrent: Int): Directive0 = {
  // thread safe counter for connections
  val concurrent = new AtomicInteger(0)

  pass.tflatMap[Unit] { _ =>
    if (concurrent.get() >= maxConcurrent) {
      complete(StatusCodes.ServiceUnavailable)
    } else {
      concurrent.incrementAndGet()
      mapResponse { response =>
        concurrent.getAndDecrement()
        response
      }
    }
  }
}

// important to create once and reuse, so that the same atomic int is used
val maxOneConcurrent = createConcurrentLimiter(1)

val route = get {
  extractRequest { req =>
    maxOneConcurrent {
      // never more than one in here
      complete("ok")
    }
  }
}


--
Johan
Akka Team

On Mon, Sep 5, 2016 at 3:56 PM, 'Raghav Narula' via Akka User List <
[email protected]> wrote:

> I'm using akka-http to build one of my services which returns a chunked
> response that could be 10s of MBs big depending on the client's query. I've
> noticed that the memory required is proportional to the number of
> concurrent streams rather than the number of connections from clients, as
> idle connections seem to use hardly any resources at all.
>
> Is there any way to limit the number of responses being rendered? I can
> only see the akka.http.server.max-connections config variable which seems
> to limit the total number of connections, rather than in flight responses.
> With a low max-connections value clients could be denied service because of
> idle clients holding connections. With a high max-connections I get out of
> memory errors as there is potential for all the connections to make large
> requests.
>
> Ideally I'd like to be able to keep connections open so that clients can
> avoid the overhead of making new connections, but still limit the number of
> in flight responses to protect memory usage. Then if the client tries to
> make a request which can't be served, the connection is dropped with a 409
> maybe?
>
> Or is the common way to deal with this to just have a fast timeout so
> clients can't hold open connections for too long, and make them re-open
> connections frequently?
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Akka Team
Lightbend <http://www.lightbend.com/> - Reactive apps on the JVM
Twitter: @akkateam

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to