Browse Source

Tidy up receiver loop

master
Con Kolivas 10 years ago
parent
commit
d42185a5eb
  1. 79
      src/connector.c

79
src/connector.c

@ -296,8 +296,7 @@ reparse:
void *receiver(void *arg) void *receiver(void *arg)
{ {
conn_instance_t *ci = (conn_instance_t *)arg; conn_instance_t *ci = (conn_instance_t *)arg;
struct epoll_event *events, event; struct epoll_event event;
client_instance_t *client;
bool maxconn = true; bool maxconn = true;
int ret, epfd; int ret, epfd;
@ -310,51 +309,51 @@ void *receiver(void *arg)
} }
event.data.fd = ci->serverfd; event.data.fd = ci->serverfd;
event.events = EPOLLIN; event.events = EPOLLIN;
events = ckalloc(sizeof(struct epoll_event));
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, ci->serverfd, &event); ret = epoll_ctl(epfd, EPOLL_CTL_ADD, ci->serverfd, &event);
if (ret < 0) { if (ret < 0) {
LOGEMERG("FATAL: Failed to add epfd %d to epoll_ctl", epfd); LOGEMERG("FATAL: Failed to add epfd %d to epoll_ctl", epfd);
goto out; return NULL;
}
repoll:
while (!ci->accept)
cksleep_ms(100);
ret = epoll_wait(epfd, events, 1, 1000);
if (unlikely(ret == -1)) {
LOGEMERG("FATAL: Failed to epoll_wait in receiver");
goto out;
} }
if (unlikely(!ret)) {
if (unlikely(maxconn)) { while (42) {
/* When we first start we listen to as many connections as client_instance_t *client;
* possible. Once we stop receiving connections we drop the
* listen to the minimum to effectively ratelimit how fast we while (unlikely(!ci->accept))
* can receive connections. */ cksleep_ms(100);
LOGDEBUG("Dropping listen backlog to 0"); ret = epoll_wait(epfd, &event, 1, 1000);
maxconn = false; if (unlikely(ret == -1)) {
listen(ci->serverfd, 0); LOGEMERG("FATAL: Failed to epoll_wait in receiver");
break;
} }
goto repoll; if (unlikely(!ret)) {
} if (unlikely(maxconn)) {
if (events->data.fd == ci->serverfd) { /* When we first start we listen to as many connections as
ret = accept_client(ci, epfd); * possible. Once we stop receiving connections we drop the
if (unlikely(ret < 0)) { * listen to the minimum to effectively ratelimit how fast we
LOGEMERG("FATAL: Failed to accept_client in receiver"); * can receive connections. */
goto out; LOGDEBUG("Dropping listen backlog to 0");
maxconn = false;
listen(ci->serverfd, 0);
}
continue;
} }
goto repoll; if (event.data.fd == ci->serverfd) {
} ret = accept_client(ci, epfd);
client = events->data.ptr; if (unlikely(ret < 0)) {
if ((events->events & EPOLLERR) || (events->events & EPOLLHUP)) { LOGEMERG("FATAL: Failed to accept_client in receiver");
/* Client disconnected */ break;
LOGDEBUG("Client fd %d HUP in epoll", client->fd); }
invalidate_client(ci->pi->ckp, ci, client); continue;
goto repoll; }
client = event.data.ptr;
if ((event.events & EPOLLERR) || (event.events & EPOLLHUP)) {
/* Client disconnected */
LOGDEBUG("Client fd %d HUP in epoll", client->fd);
invalidate_client(ci->pi->ckp, ci, client);
continue;
}
parse_client_msg(ci, client);
} }
parse_client_msg(ci, client);
goto repoll;
out:
free(events);
return NULL; return NULL;
} }

Loading…
Cancel
Save