Wakeup/delay fixes, and actually add retries for incoming and outgoing.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "wakeup.h"
|
||||
#include "outgoing.h"
|
||||
|
||||
struct outgoing {
|
||||
@@ -18,6 +19,7 @@ struct outgoing {
|
||||
char *service;
|
||||
struct addrinfo *addrs;
|
||||
struct addrinfo *addr;
|
||||
uint32_t attempt;
|
||||
outgoing_connection_handler handler;
|
||||
void *passthrough;
|
||||
struct outgoing *next;
|
||||
@@ -27,12 +29,20 @@ static struct outgoing *outgoing_head = NULL;
|
||||
|
||||
static void outgoing_connect_result(struct outgoing *, int);
|
||||
static void outgoing_resolve(struct outgoing *);
|
||||
static void outgoing_resolve_wrapper(struct peer *);
|
||||
|
||||
static void outgoing_retry(struct outgoing *outgoing) {
|
||||
uint32_t delay = retry_get_delay_ms(outgoing->attempt++);
|
||||
fprintf(stderr, "O %s: Will retry in %ds\n", outgoing->id, delay / 1000);
|
||||
outgoing->peer.event_handler = outgoing_resolve_wrapper;
|
||||
wakeup_add((struct peer *) outgoing, delay);
|
||||
}
|
||||
|
||||
static void outgoing_connect_next(struct outgoing *outgoing) {
|
||||
if (outgoing->addr == NULL) {
|
||||
freeaddrinfo(outgoing->addrs);
|
||||
fprintf(stderr, "O %s: Can't connect to any addresses of %s/%s\n", outgoing->id, outgoing->node, outgoing->service);
|
||||
// TODO: timed retry
|
||||
outgoing_retry(outgoing);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -73,6 +83,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) {
|
||||
case 0:
|
||||
fprintf(stderr, "O %s: Connected to %s/%s\n", outgoing->id, hbuf, sbuf);
|
||||
freeaddrinfo(outgoing->addrs);
|
||||
outgoing->attempt = 0;
|
||||
|
||||
// We listen for just hangup on this fd. We pass a duplicate to the handler, so our epoll setings are independent.
|
||||
outgoing->peer.event_handler = outgoing_disconnect_handler;
|
||||
@@ -107,13 +118,17 @@ static void outgoing_resolve(struct outgoing *outgoing) {
|
||||
int gai_err = getaddrinfo(outgoing->node, outgoing->service, &hints, &outgoing->addrs);
|
||||
if (gai_err) {
|
||||
fprintf(stderr, "O %s: Failed to resolve %s/%s: %s\n", outgoing->id, outgoing->node, outgoing->service, gai_strerror(gai_err));
|
||||
// TODO: timed retry
|
||||
outgoing_retry(outgoing);
|
||||
return;
|
||||
}
|
||||
outgoing->addr = outgoing->addrs;
|
||||
outgoing_connect_next(outgoing);
|
||||
}
|
||||
|
||||
static void outgoing_resolve_wrapper(struct peer *peer) {
|
||||
outgoing_resolve((struct outgoing *) peer);
|
||||
}
|
||||
|
||||
static void outgoing_del(struct outgoing *outgoing) {
|
||||
assert(!close(outgoing->peer.fd));
|
||||
free(outgoing->node);
|
||||
@@ -135,6 +150,7 @@ void outgoing_new(char *node, char *service, outgoing_connection_handler handler
|
||||
uuid_gen(outgoing->id);
|
||||
outgoing->node = strdup(node);
|
||||
outgoing->service = strdup(service);
|
||||
outgoing->attempt = 0;
|
||||
outgoing->handler = handler;
|
||||
outgoing->passthrough = passthrough;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user