Permalink
Browse files

Optimize RequestHandler socket accept function: accept only up to the…

… number of current clients in order to avoid wasting time on accept() calls that may return EAGAIN.
  • Loading branch information...
1 parent ea68012 commit d3c07030b53abdbf9d776d14094f826151393f09 @FooBarWidget FooBarWidget committed Mar 16, 2013
Showing with 13 additions and 1 deletion.
  1. +13 −1 ext/common/agents/HelperAgent/RequestHandler.h
@@ -606,6 +606,17 @@ class RequestHandler {
disconnect(client);
}
+ template<typename Number>
+ static Number clamp(Number n, Number min, Number max) {
+ if (n < min) {
+ return min;
+ } else if (n > max) {
+ return max;
+ } else {
+ return n;
+ }
+ }
+
// GDB helper function, implemented in .cpp file to prevent inlining.
Client *getClientPointer(const ClientPtr &client);
@@ -1251,9 +1262,10 @@ class RequestHandler {
void onAcceptable(ev::io &io, int revents) {
bool endReached = false;
unsigned int count = 0;
+ unsigned int maxAcceptTries = clamp<unsigned int>(clients.size(), 1, 10);
ClientPtr acceptedClients[10];
- while (!endReached && count < 10) {
+ while (!endReached && count < maxAcceptTries) {
FileDescriptor fd = acceptNonBlockingSocket(requestSocket);
if (fd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {

0 comments on commit d3c0703

Please sign in to comment.