Switch to epoll for checking connection completion.

This commit is contained in:
Ian Gulliver
2016-03-04 21:33:39 -08:00
parent a507b09332
commit bb58f31ef4

View File

@@ -21,6 +21,8 @@
#include "list.h" #include "list.h"
#include "rand.h" #include "rand.h"
#pragma GCC diagnostic ignored "-Wvla"
static struct { static struct {
char *blob_dir; char *blob_dir;
char *node; char *node;
@@ -277,17 +279,24 @@ static void conn_check(struct conn *conn) {
int error; int error;
socklen_t len = sizeof(error); socklen_t len = sizeof(error);
assert(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) == 0); assert(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) == 0);
if (!error) { if (error) {
list_del(&conn->conn_list); fprintf(stderr, "Connection failed: %s\n", strerror(error));
list_add(&conn->conn_list, &conn_open_head); shutdown_flag = true;
assert(!epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn->fd, NULL));
return; return;
} }
if (error == EINPROGRESS) { list_del(&conn->conn_list);
return; list_add(&conn->conn_list, &conn_open_head);
assert(!epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn->fd, NULL));
}
static void conn_check_all() {
struct epoll_event evs[config.num_conns];
int nfds = epoll_wait(epoll_fd, evs, (int) config.num_conns, 0);
assert(nfds >= 0);
for (int i = 0; i < nfds; i++) {
struct conn *conn = evs[i].data.ptr;
conn_check(conn);
} }
fprintf(stderr, "Connection failed: %s\n", strerror(error));
shutdown_flag = true;
} }
static void conn_cycle() { static void conn_cycle() {
@@ -295,9 +304,7 @@ static void conn_cycle() {
list_for_each_entry_safe(iter, next, &conn_open_head, conn_list) { list_for_each_entry_safe(iter, next, &conn_open_head, conn_list) {
conn_send_message(iter); conn_send_message(iter);
} }
list_for_each_entry_safe(iter, next, &conn_pending_head, conn_list) { conn_check_all();
conn_check(iter);
}
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {