Browse Source

[core] accept in network_server_handle_fdevent()

merge connection_accept() into network_server_handle_fdevent()

(possible since connection_accepted() was split out from
 connection_accept() a long time ago)
master
Glenn Strauss 7 months ago
parent
commit
302d82a59c
  1. 33
      src/connections.c
  2. 1
      src/connections.h
  3. 66
      src/network.c

33
src/connections.c

@ -841,39 +841,6 @@ static handler_t connection_handle_fdevent(void * const context, const int reven
}
connection *connection_accept(server *srv, server_socket *srv_socket) {
int cnt;
sock_addr cnt_addr;
size_t cnt_len = sizeof(cnt_addr); /*(size_t intentional; not socklen_t)*/
cnt = fdevent_accept_listenfd(srv_socket->fd, (struct sockaddr *) &cnt_addr, &cnt_len);
if (-1 == cnt) {
switch (errno) {
case EAGAIN:
#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
case EINTR:
/* we were stopped _before_ we had a connection */
case ECONNABORTED: /* this is a FreeBSD thingy */
/* we were stopped _after_ we had a connection */
break;
case EMFILE:
/* out of fds */
break;
default:
log_perror(srv->errh, __FILE__, __LINE__, "accept failed");
}
return NULL;
} else {
if (sock_addr_get_family(&cnt_addr) != AF_UNIX) {
network_accept_tcp_nagle_disable(cnt);
}
return connection_accepted(srv, srv_socket, &cnt_addr, cnt);
}
}
__attribute_cold__
static int connection_read_cq_err(connection *con) {
request_st * const r = &con->request;

1
src/connections.h

@ -16,7 +16,6 @@ void connection_periodic_maint (server *srv, time_t cur_ts);
int connection_send_1xx (request_st *r, connection *con);
connection * connection_accept(server *srv, struct server_socket *srv_sock);
connection * connection_accepted(server *srv, const struct server_socket *srv_socket, sock_addr *cnt_addr, int cnt);
void connection_state_machine(connection *con);

66
src/network.c

@ -44,28 +44,58 @@ network_accept_tcp_nagle_disable (const int fd)
}
static handler_t network_server_handle_fdevent(void *context, int revents) {
server_socket * const srv_socket = (server_socket *)context;
server * const srv = srv_socket->srv;
connection *con;
int loops;
const server_socket * const srv_socket = (server_socket *)context;
server * const srv = srv_socket->srv;
if (0 == (revents & FDEVENT_IN)) {
log_error(srv->errh, __FILE__, __LINE__,
"strange event for server socket %d %d", srv_socket->fd, revents);
return HANDLER_ERROR;
}
if (0 == (revents & FDEVENT_IN)) {
log_error(srv->errh, __FILE__, __LINE__,
"strange event for server socket %d %d", srv_socket->fd, revents);
return HANDLER_ERROR;
}
/* accept()s at most 100 new connections before
* jumping out to process events on other connections */
int loops = (int)(srv->max_conns - srv->conns.used);
if (loops > 100)
loops = 100;
else if (loops <= 0)
return HANDLER_GO_ON;
/* accept()s at most 100 connections directly
*
* we jump out after 100 to give the waiting connections a chance */
if (srv->conns.used >= srv->max_conns) return HANDLER_GO_ON;
loops = (int)(srv->max_conns - srv->conns.used + 1);
if (loops > 100) loops = 101;
const int nagle_disable =
(sock_addr_get_family(&srv_socket->addr) != AF_UNIX);
while (--loops && NULL != (con = connection_accept(srv, srv_socket)))
connection_state_machine(con);
sock_addr addr;
size_t addrlen; /*(size_t intentional; not socklen_t)*/
do {
addrlen = sizeof(addr);
int fd = fdevent_accept_listenfd(srv_socket->fd,
(struct sockaddr *)&addr, &addrlen);
if (-1 == fd) break;
if (nagle_disable)
network_accept_tcp_nagle_disable(fd);
connection *con = connection_accepted(srv, srv_socket, &addr, fd);
if (__builtin_expect( (!con), 0)) return HANDLER_GO_ON;
connection_state_machine(con);
} while (--loops);
if (loops) {
switch (errno) {
case EAGAIN:
#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
case EINTR:
case ECONNABORTED:
case EMFILE:
break;
default:
log_perror(srv->errh, __FILE__, __LINE__, "accept()");
}
}
return HANDLER_GO_ON;
return HANDLER_GO_ON;
}
static void network_host_normalize_addr_str(buffer *host, sock_addr *addr) {

Loading…
Cancel
Save