|
|
@ -29,12 +29,17 @@ struct client_instance { |
|
|
|
/* For clients hashtable */ |
|
|
|
/* For clients hashtable */ |
|
|
|
UT_hash_handle hh; |
|
|
|
UT_hash_handle hh; |
|
|
|
int64_t id; |
|
|
|
int64_t id; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* fd cannot be changed while a ref is held */ |
|
|
|
int fd; |
|
|
|
int fd; |
|
|
|
|
|
|
|
|
|
|
|
/* Reference count for when this instance is used outside of the
|
|
|
|
/* Reference count for when this instance is used outside of the
|
|
|
|
* connector_data lock */ |
|
|
|
* connector_data lock */ |
|
|
|
int ref; |
|
|
|
int ref; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Have we disabled this client to be removed when there are no refs? */ |
|
|
|
|
|
|
|
bool invalid; |
|
|
|
|
|
|
|
|
|
|
|
/* For dead_clients list */ |
|
|
|
/* For dead_clients list */ |
|
|
|
client_instance_t *next; |
|
|
|
client_instance_t *next; |
|
|
|
client_instance_t *prev; |
|
|
|
client_instance_t *prev; |
|
|
@ -58,6 +63,7 @@ struct sender_send { |
|
|
|
client_instance_t *client; |
|
|
|
client_instance_t *client; |
|
|
|
char *buf; |
|
|
|
char *buf; |
|
|
|
int len; |
|
|
|
int len; |
|
|
|
|
|
|
|
int ofs; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
typedef struct sender_send sender_send_t; |
|
|
|
typedef struct sender_send sender_send_t; |
|
|
@ -68,6 +74,8 @@ struct connector_data { |
|
|
|
cklock_t lock; |
|
|
|
cklock_t lock; |
|
|
|
proc_instance_t *pi; |
|
|
|
proc_instance_t *pi; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time_t start_time; |
|
|
|
|
|
|
|
|
|
|
|
/* Array of server fds */ |
|
|
|
/* Array of server fds */ |
|
|
|
int *serverfd; |
|
|
|
int *serverfd; |
|
|
|
/* All time count of clients connected */ |
|
|
|
/* All time count of clients connected */ |
|
|
@ -93,10 +101,11 @@ struct connector_data { |
|
|
|
|
|
|
|
|
|
|
|
/* For the linked list of pending sends */ |
|
|
|
/* For the linked list of pending sends */ |
|
|
|
sender_send_t *sender_sends; |
|
|
|
sender_send_t *sender_sends; |
|
|
|
sender_send_t *delayed_sends; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int64_t sends_generated; |
|
|
|
int64_t sends_generated; |
|
|
|
int64_t sends_delayed; |
|
|
|
int64_t sends_delayed; |
|
|
|
|
|
|
|
int64_t sends_queued; |
|
|
|
|
|
|
|
int64_t sends_size; |
|
|
|
|
|
|
|
|
|
|
|
/* For protecting the pending sends list */ |
|
|
|
/* For protecting the pending sends list */ |
|
|
|
mutex_t sender_lock; |
|
|
|
mutex_t sender_lock; |
|
|
@ -219,6 +228,7 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
keep_sockalive(fd); |
|
|
|
keep_sockalive(fd); |
|
|
|
|
|
|
|
noblock_socket(fd); |
|
|
|
|
|
|
|
|
|
|
|
LOGINFO("Connected new client %d on socket %d to %d active clients from %s:%d", |
|
|
|
LOGINFO("Connected new client %d on socket %d to %d active clients from %s:%d", |
|
|
|
cdata->nfds, fd, no_clients, client->address_name, port); |
|
|
|
cdata->nfds, fd, no_clients, client->address_name, port); |
|
|
@ -229,7 +239,6 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) |
|
|
|
cdata->nfds++; |
|
|
|
cdata->nfds++; |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
|
|
|
|
|
|
|
|
client->fd = fd; |
|
|
|
|
|
|
|
event.data.u64 = client->id; |
|
|
|
event.data.u64 = client->id; |
|
|
|
event.events = EPOLLIN | EPOLLRDHUP; |
|
|
|
event.events = EPOLLIN | EPOLLRDHUP; |
|
|
|
if (unlikely(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0)) { |
|
|
|
if (unlikely(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0)) { |
|
|
@ -241,6 +250,7 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) |
|
|
|
* to it. We drop that reference when the socket is closed which |
|
|
|
* to it. We drop that reference when the socket is closed which |
|
|
|
* removes it automatically from the epoll list. */ |
|
|
|
* removes it automatically from the epoll list. */ |
|
|
|
__inc_instance_ref(client); |
|
|
|
__inc_instance_ref(client); |
|
|
|
|
|
|
|
client->fd = fd; |
|
|
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
@ -249,16 +259,14 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) |
|
|
|
static int drop_client(cdata_t *cdata, client_instance_t *client) |
|
|
|
static int drop_client(cdata_t *cdata, client_instance_t *client) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int64_t client_id = 0; |
|
|
|
int64_t client_id = 0; |
|
|
|
int fd; |
|
|
|
int fd = -1; |
|
|
|
|
|
|
|
|
|
|
|
ck_wlock(&cdata->lock); |
|
|
|
ck_wlock(&cdata->lock); |
|
|
|
fd = client->fd; |
|
|
|
if (!client->invalid) { |
|
|
|
if (fd != -1) { |
|
|
|
client->invalid = true; |
|
|
|
client_id = client->id; |
|
|
|
client_id = client->id; |
|
|
|
|
|
|
|
fd = client->fd; |
|
|
|
epoll_ctl(cdata->epfd, EPOLL_CTL_DEL, fd, NULL); |
|
|
|
epoll_ctl(cdata->epfd, EPOLL_CTL_DEL, fd, NULL); |
|
|
|
nolinger_socket(fd); |
|
|
|
|
|
|
|
Close(client->fd); |
|
|
|
|
|
|
|
HASH_DEL(cdata->clients, client); |
|
|
|
HASH_DEL(cdata->clients, client); |
|
|
|
DL_APPEND(cdata->dead_clients, client); |
|
|
|
DL_APPEND(cdata->dead_clients, client); |
|
|
|
/* This is the reference to this client's presence in the
|
|
|
|
/* This is the reference to this client's presence in the
|
|
|
@ -274,7 +282,22 @@ static int drop_client(cdata_t *cdata, client_instance_t *client) |
|
|
|
return fd; |
|
|
|
return fd; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void stratifier_drop_client(ckpool_t *ckp, int64_t id) |
|
|
|
/* For sending the drop command to the upstream pool in passthrough mode */ |
|
|
|
|
|
|
|
static void generator_drop_client(ckpool_t *ckp, const client_instance_t *client) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
json_t *val; |
|
|
|
|
|
|
|
char *s; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSON_CPACK(val, "{si,sI:ss:si:ss:s[]}", "id", 42, "client_id", client->id, "address", |
|
|
|
|
|
|
|
client->address_name, "server", client->server, "method", "mining.term", |
|
|
|
|
|
|
|
"params"); |
|
|
|
|
|
|
|
s = json_dumps(val, 0); |
|
|
|
|
|
|
|
json_decref(val); |
|
|
|
|
|
|
|
send_proc(ckp->generator, s); |
|
|
|
|
|
|
|
free(s); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void stratifier_drop_id(ckpool_t *ckp, const int64_t id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char buf[256]; |
|
|
|
char buf[256]; |
|
|
|
|
|
|
|
|
|
|
@ -282,6 +305,11 @@ static void stratifier_drop_client(ckpool_t *ckp, int64_t id) |
|
|
|
send_proc(ckp->stratifier, buf); |
|
|
|
send_proc(ckp->stratifier, buf); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void stratifier_drop_client(ckpool_t *ckp, const client_instance_t *client) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
stratifier_drop_id(ckp, client->id); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Invalidate this instance. Remove them from the hashtables we look up
|
|
|
|
/* Invalidate this instance. Remove them from the hashtables we look up
|
|
|
|
* regularly but keep the instances in a linked list until their ref count |
|
|
|
* regularly but keep the instances in a linked list until their ref count |
|
|
|
* drops to zero when we can remove them lazily. Client must hold a reference |
|
|
|
* drops to zero when we can remove them lazily. Client must hold a reference |
|
|
@ -292,9 +320,10 @@ static int invalidate_client(ckpool_t *ckp, cdata_t *cdata, client_instance_t *c |
|
|
|
int ret; |
|
|
|
int ret; |
|
|
|
|
|
|
|
|
|
|
|
ret = drop_client(cdata, client); |
|
|
|
ret = drop_client(cdata, client); |
|
|
|
if (ckp->passthrough) |
|
|
|
if (!ckp->passthrough && !client->passthrough) |
|
|
|
goto out; |
|
|
|
stratifier_drop_client(ckp, client); |
|
|
|
stratifier_drop_client(ckp, client->id); |
|
|
|
else if (ckp->passthrough) |
|
|
|
|
|
|
|
generator_drop_client(ckp, client); |
|
|
|
|
|
|
|
|
|
|
|
/* Cull old unused clients lazily when there are no more reference
|
|
|
|
/* Cull old unused clients lazily when there are no more reference
|
|
|
|
* counts for them. */ |
|
|
|
* counts for them. */ |
|
|
@ -303,12 +332,16 @@ static int invalidate_client(ckpool_t *ckp, cdata_t *cdata, client_instance_t *c |
|
|
|
if (!client->ref) { |
|
|
|
if (!client->ref) { |
|
|
|
DL_DELETE(cdata->dead_clients, client); |
|
|
|
DL_DELETE(cdata->dead_clients, client); |
|
|
|
LOGINFO("Connector recycling client %"PRId64, client->id); |
|
|
|
LOGINFO("Connector recycling client %"PRId64, client->id); |
|
|
|
|
|
|
|
/* We only close the client fd once we're sure there
|
|
|
|
|
|
|
|
* are no references to it left to prevent fds being |
|
|
|
|
|
|
|
* reused on new and old clients. */ |
|
|
|
|
|
|
|
nolinger_socket(client->fd); |
|
|
|
|
|
|
|
Close(client->fd); |
|
|
|
__recycle_client(cdata, client); |
|
|
|
__recycle_client(cdata, client); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
|
|
|
|
|
|
|
|
out: |
|
|
|
|
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -323,41 +356,33 @@ static void parse_client_msg(cdata_t *cdata, client_instance_t *client) |
|
|
|
json_t *val; |
|
|
|
json_t *val; |
|
|
|
|
|
|
|
|
|
|
|
retry: |
|
|
|
retry: |
|
|
|
ret = wait_read_select(client->fd, 0); |
|
|
|
if (unlikely(client->bufofs > MAX_MSGSIZE)) { |
|
|
|
if (ret < 1) { |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d overloaded buffer without EOL, disconnecting", |
|
|
|
if (!ret) |
|
|
|
client->id, client->fd); |
|
|
|
return; |
|
|
|
|
|
|
|
LOGINFO("Client fd %d disconnected - select fail with bufofs %d ret %d errno %d %s", |
|
|
|
|
|
|
|
client->fd, client->bufofs, ret, errno, ret && errno ? strerror(errno) : ""); |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
buflen = PAGESIZE - client->bufofs; |
|
|
|
buflen = PAGESIZE - client->bufofs; |
|
|
|
ret = recv(client->fd, client->buf + client->bufofs, buflen, 0); |
|
|
|
/* This read call is non-blocking since the socket is set to O_NOBLOCK */ |
|
|
|
|
|
|
|
ret = read(client->fd, client->buf + client->bufofs, buflen); |
|
|
|
if (ret < 1) { |
|
|
|
if (ret < 1) { |
|
|
|
if (!ret) |
|
|
|
if (likely(errno == EAGAIN || errno == EWOULDBLOCK || !ret)) |
|
|
|
return; |
|
|
|
return; |
|
|
|
LOGINFO("Client fd %d disconnected - recv fail with bufofs %d ret %d errno %d %s", |
|
|
|
LOGINFO("Client id %"PRId64" fd %d disconnected - recv fail with bufofs %d ret %d errno %d %s", |
|
|
|
client->fd, client->bufofs, ret, errno, ret && errno ? strerror(errno) : ""); |
|
|
|
client->id, client->fd, client->bufofs, ret, errno, ret && errno ? strerror(errno) : ""); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
client->bufofs += ret; |
|
|
|
client->bufofs += ret; |
|
|
|
reparse: |
|
|
|
reparse: |
|
|
|
eol = memchr(client->buf, '\n', client->bufofs); |
|
|
|
eol = memchr(client->buf, '\n', client->bufofs); |
|
|
|
if (!eol) { |
|
|
|
if (!eol) |
|
|
|
if (unlikely(client->bufofs > MAX_MSGSIZE)) { |
|
|
|
|
|
|
|
LOGWARNING("Client fd %d overloaded buffer without EOL, disconnecting", client->fd); |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
goto retry; |
|
|
|
goto retry; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Do something useful with this message now */ |
|
|
|
/* Do something useful with this message now */ |
|
|
|
buflen = eol - client->buf + 1; |
|
|
|
buflen = eol - client->buf + 1; |
|
|
|
if (unlikely(buflen > MAX_MSGSIZE)) { |
|
|
|
if (unlikely(buflen > MAX_MSGSIZE)) { |
|
|
|
LOGWARNING("Client fd %d message oversize, disconnecting", client->fd); |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d message oversize, disconnecting", client->id, client->fd); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -382,16 +407,17 @@ reparse: |
|
|
|
json_object_del(val, "client_id"); |
|
|
|
json_object_del(val, "client_id"); |
|
|
|
passthrough_id = (client->id << 32) | passthrough_id; |
|
|
|
passthrough_id = (client->id << 32) | passthrough_id; |
|
|
|
json_object_set_new_nocheck(val, "client_id", json_integer(passthrough_id)); |
|
|
|
json_object_set_new_nocheck(val, "client_id", json_integer(passthrough_id)); |
|
|
|
} else |
|
|
|
} else { |
|
|
|
json_object_set_new_nocheck(val, "client_id", json_integer(client->id)); |
|
|
|
json_object_set_new_nocheck(val, "client_id", json_integer(client->id)); |
|
|
|
json_object_set_new_nocheck(val, "address", json_string(client->address_name)); |
|
|
|
json_object_set_new_nocheck(val, "address", json_string(client->address_name)); |
|
|
|
|
|
|
|
} |
|
|
|
json_object_set_new_nocheck(val, "server", json_integer(client->server)); |
|
|
|
json_object_set_new_nocheck(val, "server", json_integer(client->server)); |
|
|
|
s = json_dumps(val, 0); |
|
|
|
s = json_dumps(val, 0); |
|
|
|
|
|
|
|
|
|
|
|
/* Do not send messages of clients we've already dropped. We
|
|
|
|
/* Do not send messages of clients we've already dropped. We
|
|
|
|
* do this unlocked as the occasional false negative can be |
|
|
|
* do this unlocked as the occasional false negative can be |
|
|
|
* filtered by the stratifier. */ |
|
|
|
* filtered by the stratifier. */ |
|
|
|
if (likely(client->fd != -1)) { |
|
|
|
if (likely(!client->invalid)) { |
|
|
|
if (ckp->passthrough) |
|
|
|
if (ckp->passthrough) |
|
|
|
send_proc(ckp->generator, s); |
|
|
|
send_proc(ckp->generator, s); |
|
|
|
else |
|
|
|
else |
|
|
@ -413,8 +439,12 @@ static client_instance_t *ref_client_by_id(cdata_t *cdata, int64_t id) |
|
|
|
|
|
|
|
|
|
|
|
ck_wlock(&cdata->lock); |
|
|
|
ck_wlock(&cdata->lock); |
|
|
|
HASH_FIND_I64(cdata->clients, &id, client); |
|
|
|
HASH_FIND_I64(cdata->clients, &id, client); |
|
|
|
if (client) |
|
|
|
if (client) { |
|
|
|
__inc_instance_ref(client); |
|
|
|
if (!client->invalid) |
|
|
|
|
|
|
|
__inc_instance_ref(client); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
client = NULL; |
|
|
|
|
|
|
|
} |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
|
|
|
|
|
|
|
|
return client; |
|
|
|
return client; |
|
|
@ -425,10 +455,8 @@ static client_instance_t *ref_client_by_id(cdata_t *cdata, int64_t id) |
|
|
|
void *receiver(void *arg) |
|
|
|
void *receiver(void *arg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
cdata_t *cdata = (cdata_t *)arg; |
|
|
|
cdata_t *cdata = (cdata_t *)arg; |
|
|
|
bool dropped_backlog = false; |
|
|
|
|
|
|
|
struct epoll_event event; |
|
|
|
struct epoll_event event; |
|
|
|
uint64_t serverfds, i; |
|
|
|
uint64_t serverfds, i; |
|
|
|
time_t start_t; |
|
|
|
|
|
|
|
int ret, epfd; |
|
|
|
int ret, epfd; |
|
|
|
|
|
|
|
|
|
|
|
rename_proc("creceiver"); |
|
|
|
rename_proc("creceiver"); |
|
|
@ -436,7 +464,7 @@ void *receiver(void *arg) |
|
|
|
epfd = cdata->epfd = epoll_create1(EPOLL_CLOEXEC); |
|
|
|
epfd = cdata->epfd = epoll_create1(EPOLL_CLOEXEC); |
|
|
|
if (epfd < 0) { |
|
|
|
if (epfd < 0) { |
|
|
|
LOGEMERG("FATAL: Failed to create epoll in receiver"); |
|
|
|
LOGEMERG("FATAL: Failed to create epoll in receiver"); |
|
|
|
return NULL; |
|
|
|
goto out; |
|
|
|
} |
|
|
|
} |
|
|
|
serverfds = cdata->ckp->serverurls; |
|
|
|
serverfds = cdata->ckp->serverurls; |
|
|
|
/* Add all the serverfds to the epoll */ |
|
|
|
/* Add all the serverfds to the epoll */ |
|
|
@ -447,27 +475,16 @@ void *receiver(void *arg) |
|
|
|
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cdata->serverfd[i], &event); |
|
|
|
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cdata->serverfd[i], &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); |
|
|
|
return NULL; |
|
|
|
goto out; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (!cdata->accept) |
|
|
|
while (!cdata->accept) |
|
|
|
cksleep_ms(1); |
|
|
|
cksleep_ms(1); |
|
|
|
start_t = time(NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (42) { |
|
|
|
while (42) { |
|
|
|
client_instance_t *client; |
|
|
|
client_instance_t *client; |
|
|
|
|
|
|
|
|
|
|
|
if (unlikely(!dropped_backlog && time(NULL) - start_t > 90)) { |
|
|
|
|
|
|
|
/* When we first start we listen to as many connections
|
|
|
|
|
|
|
|
* as possible. After the first minute we drop the |
|
|
|
|
|
|
|
* listen to the minimum to effectively ratelimit how |
|
|
|
|
|
|
|
* fast we can receive new connections. */ |
|
|
|
|
|
|
|
dropped_backlog = true; |
|
|
|
|
|
|
|
LOGNOTICE("Dropping server listen backlog to 0"); |
|
|
|
|
|
|
|
for (i = 0; i < serverfds; i++) |
|
|
|
|
|
|
|
listen(cdata->serverfd[i], 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
while (unlikely(!cdata->accept)) |
|
|
|
while (unlikely(!cdata->accept)) |
|
|
|
cksleep_ms(10); |
|
|
|
cksleep_ms(10); |
|
|
|
ret = epoll_wait(epfd, &event, 1, 1000); |
|
|
|
ret = epoll_wait(epfd, &event, 1, 1000); |
|
|
@ -489,41 +506,115 @@ void *receiver(void *arg) |
|
|
|
} |
|
|
|
} |
|
|
|
client = ref_client_by_id(cdata, event.data.u64); |
|
|
|
client = ref_client_by_id(cdata, event.data.u64); |
|
|
|
if (unlikely(!client)) { |
|
|
|
if (unlikely(!client)) { |
|
|
|
LOGWARNING("Failed to find client by id in receiver!"); |
|
|
|
LOGNOTICE("Failed to find client by id %"PRId64" in receiver!", event.data.u64); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
if (event.events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { |
|
|
|
if (unlikely(client->invalid)) |
|
|
|
/* Client disconnected */ |
|
|
|
goto noparse; |
|
|
|
LOGDEBUG("Client fd %d HUP in epoll", client->fd); |
|
|
|
/* We can have both messages and read hang ups so process the
|
|
|
|
invalidate_client(cdata->pi->ckp, cdata, client); |
|
|
|
* message first. */ |
|
|
|
} else |
|
|
|
if (likely(event.events & EPOLLIN)) |
|
|
|
parse_client_msg(cdata, client); |
|
|
|
parse_client_msg(cdata, client); |
|
|
|
|
|
|
|
if (unlikely(client->invalid)) |
|
|
|
|
|
|
|
goto noparse; |
|
|
|
|
|
|
|
if (unlikely(event.events & EPOLLERR)) { |
|
|
|
|
|
|
|
socklen_t errlen = sizeof(int); |
|
|
|
|
|
|
|
int error = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* See what type of error this is and raise the log
|
|
|
|
|
|
|
|
* level of the message if it's unexpected. */ |
|
|
|
|
|
|
|
getsockopt(client->fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen); |
|
|
|
|
|
|
|
if (error != 104) { |
|
|
|
|
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d epollerr HUP in epoll with errno %d: %s", |
|
|
|
|
|
|
|
client->id, client->fd, error, strerror(error)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d epollerr HUP in epoll with errno %d: %s", |
|
|
|
|
|
|
|
client->id, client->fd, error, strerror(error)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
invalidate_client(cdata->pi->ckp, cdata, client); |
|
|
|
|
|
|
|
} else if (unlikely(event.events & EPOLLHUP)) { |
|
|
|
|
|
|
|
/* Client connection reset by peer */ |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d HUP in epoll", client->id, client->fd); |
|
|
|
|
|
|
|
invalidate_client(cdata->pi->ckp, cdata, client); |
|
|
|
|
|
|
|
} else if (unlikely(event.events & EPOLLRDHUP)) { |
|
|
|
|
|
|
|
/* Client disconnected by peer */ |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d RDHUP in epoll", client->id, client->fd); |
|
|
|
|
|
|
|
invalidate_client(cdata->pi->ckp, cdata, client); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
noparse: |
|
|
|
dec_instance_ref(cdata, client); |
|
|
|
dec_instance_ref(cdata, client); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
out: |
|
|
|
|
|
|
|
/* We shouldn't get here unless there's an error */ |
|
|
|
|
|
|
|
childsighandler(15); |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Use a thread to send queued messages, using select() to only send to sockets
|
|
|
|
/* Send a sender_send message and return true if we've finished sending it or
|
|
|
|
* ready for writing immediately to not delay other messages. */ |
|
|
|
* are unable to send any more. */ |
|
|
|
void *sender(void *arg) |
|
|
|
static bool send_sender_send(ckpool_t *ckp, cdata_t *cdata, sender_send_t *sender_send) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
client_instance_t *client = sender_send->client; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (unlikely(client->invalid)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (sender_send->len) { |
|
|
|
|
|
|
|
int ret = write(client->fd, sender_send->buf + sender_send->ofs, sender_send->len); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (unlikely(ret < 1)) { |
|
|
|
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK || !ret) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d disconnected with write errno %d:%s", |
|
|
|
|
|
|
|
client->id, client->fd, errno, strerror(errno)); |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sender_send->ofs += ret; |
|
|
|
|
|
|
|
sender_send->len -= ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void clear_sender_send(sender_send_t *sender_send, cdata_t *cdata) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
dec_instance_ref(cdata, sender_send->client); |
|
|
|
|
|
|
|
free(sender_send->buf); |
|
|
|
|
|
|
|
free(sender_send); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Use a thread to send queued messages, appending them to the sends list and
|
|
|
|
|
|
|
|
* iterating over all of them, attempting to send them all non-blocking to |
|
|
|
|
|
|
|
* only send to those clients ready to receive data. */ |
|
|
|
|
|
|
|
static void *sender(void *arg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
cdata_t *cdata = (cdata_t *)arg; |
|
|
|
cdata_t *cdata = (cdata_t *)arg; |
|
|
|
|
|
|
|
sender_send_t *sends = NULL; |
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
bool sent = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rename_proc("csender"); |
|
|
|
rename_proc("csender"); |
|
|
|
|
|
|
|
|
|
|
|
while (42) { |
|
|
|
while (42) { |
|
|
|
sender_send_t *sender_send, *delayed; |
|
|
|
int64_t sends_queued = 0, sends_size = 0; |
|
|
|
client_instance_t *client; |
|
|
|
sender_send_t *sending, *tmp; |
|
|
|
int ret = 0, fd, ofs = 0; |
|
|
|
|
|
|
|
|
|
|
|
/* Check all sends to see if they can be written out */ |
|
|
|
|
|
|
|
DL_FOREACH_SAFE(sends, sending, tmp) { |
|
|
|
|
|
|
|
if (send_sender_send(ckp, cdata, sending)) { |
|
|
|
|
|
|
|
DL_DELETE(sends, sending); |
|
|
|
|
|
|
|
clear_sender_send(sending, cdata); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sends_queued++; |
|
|
|
|
|
|
|
sends_size += sizeof(sender_send_t) + sending->len + 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mutex_lock(&cdata->sender_lock); |
|
|
|
mutex_lock(&cdata->sender_lock); |
|
|
|
/* Poll every 10ms if there are no new sends. Re-examine
|
|
|
|
cdata->sends_delayed += sends_queued; |
|
|
|
* delayed sends immediately after a successful send in case |
|
|
|
cdata->sends_queued = sends_queued; |
|
|
|
* endless new sends more frequently end up starving the |
|
|
|
cdata->sends_size = sends_size; |
|
|
|
* delayed sends. */ |
|
|
|
/* Poll every 10ms if there are no new sends. */ |
|
|
|
if (!cdata->sender_sends && !sent) { |
|
|
|
if (!cdata->sender_sends) { |
|
|
|
const ts_t polltime = {0, 10000000}; |
|
|
|
const ts_t polltime = {0, 10000000}; |
|
|
|
ts_t timeout_ts; |
|
|
|
ts_t timeout_ts; |
|
|
|
|
|
|
|
|
|
|
@ -531,86 +622,25 @@ void *sender(void *arg) |
|
|
|
timeraddspec(&timeout_ts, &polltime); |
|
|
|
timeraddspec(&timeout_ts, &polltime); |
|
|
|
cond_timedwait(&cdata->sender_cond, &cdata->sender_lock, &timeout_ts); |
|
|
|
cond_timedwait(&cdata->sender_cond, &cdata->sender_lock, &timeout_ts); |
|
|
|
} |
|
|
|
} |
|
|
|
sender_send = cdata->sender_sends; |
|
|
|
if (cdata->sender_sends) { |
|
|
|
if (sender_send) |
|
|
|
DL_CONCAT(sends, cdata->sender_sends); |
|
|
|
DL_DELETE(cdata->sender_sends, sender_send); |
|
|
|
cdata->sender_sends = NULL; |
|
|
|
mutex_unlock(&cdata->sender_lock); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sent = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Service delayed sends only if we have timed out on the
|
|
|
|
|
|
|
|
* conditional with no new sends appearing or have just |
|
|
|
|
|
|
|
* serviced another message successfully. */ |
|
|
|
|
|
|
|
if (!sender_send) { |
|
|
|
|
|
|
|
/* Find a delayed client that needs servicing and set
|
|
|
|
|
|
|
|
* ret accordingly. We do not need to use FOREACH_SAFE |
|
|
|
|
|
|
|
* as we break out of the loop as soon as we manipuate |
|
|
|
|
|
|
|
* the list. */ |
|
|
|
|
|
|
|
DL_FOREACH(cdata->delayed_sends, delayed) { |
|
|
|
|
|
|
|
if ((ret = wait_write_select(delayed->client->fd, 0))) { |
|
|
|
|
|
|
|
sender_send = cdata->delayed_sends; |
|
|
|
|
|
|
|
DL_DELETE(cdata->delayed_sends, sender_send); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/* None found ? */ |
|
|
|
|
|
|
|
if (!sender_send) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client = sender_send->client; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* If this socket is not ready to receive data from us, put the
|
|
|
|
|
|
|
|
* send back on the tail of the list and decrease the timeout |
|
|
|
|
|
|
|
* to poll to either look for a client that is ready or poll |
|
|
|
|
|
|
|
* select on this one */ |
|
|
|
|
|
|
|
ck_rlock(&cdata->lock); |
|
|
|
|
|
|
|
fd = client->fd; |
|
|
|
|
|
|
|
if (!ret) |
|
|
|
|
|
|
|
ret = wait_write_select(fd, 0); |
|
|
|
|
|
|
|
ck_runlock(&cdata->lock); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ret < 1) { |
|
|
|
|
|
|
|
if (ret < 0) { |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d interrupted", client->id, fd); |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
|
|
|
|
goto contfree; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LOGDEBUG("Client %"PRId64" not ready for writes", client->id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Append it to the tail of the delayed sends list.
|
|
|
|
|
|
|
|
* This is the only function that alters it so no |
|
|
|
|
|
|
|
* locking is required. Keep the client ref. */ |
|
|
|
|
|
|
|
DL_APPEND(cdata->delayed_sends, sender_send); |
|
|
|
|
|
|
|
cdata->sends_delayed++; |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
while (sender_send->len) { |
|
|
|
|
|
|
|
ret = send(fd, sender_send->buf + ofs, sender_send->len , 0); |
|
|
|
|
|
|
|
if (unlikely(ret < 0)) { |
|
|
|
|
|
|
|
LOGINFO("Client id %"PRId64" fd %d disconnected", client->id, fd); |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ofs += ret; |
|
|
|
|
|
|
|
sender_send->len -= ret; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
contfree: |
|
|
|
mutex_unlock(&cdata->sender_lock); |
|
|
|
sent = true; |
|
|
|
|
|
|
|
free(sender_send->buf); |
|
|
|
|
|
|
|
free(sender_send); |
|
|
|
|
|
|
|
dec_instance_ref(cdata, client); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* We shouldn't get here unless there's an error */ |
|
|
|
|
|
|
|
childsighandler(15); |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Send a client by id a heap allocated buffer, allowing this function to
|
|
|
|
/* Send a client by id a heap allocated buffer, allowing this function to
|
|
|
|
* free the ram. */ |
|
|
|
* free the ram. */ |
|
|
|
static void send_client(cdata_t *cdata, int64_t id, char *buf) |
|
|
|
static void send_client(cdata_t *cdata, const int64_t id, char *buf) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
sender_send_t *sender_send; |
|
|
|
sender_send_t *sender_send; |
|
|
|
client_instance_t *client; |
|
|
|
client_instance_t *client; |
|
|
|
int fd = -1, len; |
|
|
|
int len; |
|
|
|
|
|
|
|
|
|
|
|
if (unlikely(!buf)) { |
|
|
|
if (unlikely(!buf)) { |
|
|
|
LOGWARNING("Connector send_client sent a null buffer"); |
|
|
|
LOGWARNING("Connector send_client sent a null buffer"); |
|
|
@ -623,28 +653,36 @@ static void send_client(cdata_t *cdata, int64_t id, char *buf) |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ck_wlock(&cdata->lock); |
|
|
|
/* Grab a reference to this client until the sender_send has
|
|
|
|
HASH_FIND_I64(cdata->clients, &id, client); |
|
|
|
* completed processing. Is this a passthrough subclient ? */ |
|
|
|
if (likely(client)) { |
|
|
|
if (id > 0xffffffffll) { |
|
|
|
fd = client->fd; |
|
|
|
int64_t client_id, pass_id; |
|
|
|
/* Grab a reference to this client until the sender_send has
|
|
|
|
|
|
|
|
* completed processing. */ |
|
|
|
|
|
|
|
__inc_instance_ref(client); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ck_wunlock(&cdata->lock); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (unlikely(fd == -1)) { |
|
|
|
|
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (client) { |
|
|
|
client_id = id & 0xffffffffll; |
|
|
|
/* This shouldn't happen */ |
|
|
|
pass_id = id >> 32; |
|
|
|
LOGWARNING("Client id %"PRId64" disconnected but fd already invalidated!", id); |
|
|
|
/* Make sure the passthrough exists for passthrough subclients */ |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
client = ref_client_by_id(cdata, pass_id); |
|
|
|
} else { |
|
|
|
if (unlikely(!client)) { |
|
|
|
|
|
|
|
LOGINFO("Connector failed to find passthrough id %"PRId64" of client id %"PRId64" to send to", |
|
|
|
|
|
|
|
pass_id, client_id); |
|
|
|
|
|
|
|
/* Now see if the subclient exists */ |
|
|
|
|
|
|
|
client = ref_client_by_id(cdata, client_id); |
|
|
|
|
|
|
|
if (client) { |
|
|
|
|
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
|
|
|
|
dec_instance_ref(cdata, client); |
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
stratifier_drop_id(ckp, id); |
|
|
|
|
|
|
|
free(buf); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
client = ref_client_by_id(cdata, id); |
|
|
|
|
|
|
|
if (unlikely(!client)) { |
|
|
|
LOGINFO("Connector failed to find client id %"PRId64" to send to", id); |
|
|
|
LOGINFO("Connector failed to find client id %"PRId64" to send to", id); |
|
|
|
|
|
|
|
stratifier_drop_id(ckp, id); |
|
|
|
|
|
|
|
free(buf); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
free(buf); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sender_send = ckzalloc(sizeof(sender_send_t)); |
|
|
|
sender_send = ckzalloc(sizeof(sender_send_t)); |
|
|
@ -671,7 +709,7 @@ static void passthrough_client(cdata_t *cdata, client_instance_t *client) |
|
|
|
|
|
|
|
|
|
|
|
static void process_client_msg(cdata_t *cdata, const char *buf) |
|
|
|
static void process_client_msg(cdata_t *cdata, const char *buf) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int64_t client_id64, client_id; |
|
|
|
int64_t client_id; |
|
|
|
json_t *json_msg; |
|
|
|
json_t *json_msg; |
|
|
|
char *msg; |
|
|
|
char *msg; |
|
|
|
|
|
|
|
|
|
|
@ -682,22 +720,18 @@ static void process_client_msg(cdata_t *cdata, const char *buf) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Extract the client id from the json message and remove its entry */ |
|
|
|
/* Extract the client id from the json message and remove its entry */ |
|
|
|
client_id64 = json_integer_value(json_object_get(json_msg, "client_id")); |
|
|
|
client_id = json_integer_value(json_object_get(json_msg, "client_id")); |
|
|
|
json_object_del(json_msg, "client_id"); |
|
|
|
json_object_del(json_msg, "client_id"); |
|
|
|
if (client_id64 > 0xffffffffll) { |
|
|
|
/* Put client_id back in for a passthrough subclient, passing its
|
|
|
|
int64_t passthrough_id; |
|
|
|
* upstream client_id instead of the passthrough's. */ |
|
|
|
|
|
|
|
if (client_id > 0xffffffffll) |
|
|
|
passthrough_id = client_id64 & 0xffffffffll; |
|
|
|
json_object_set_new_nocheck(json_msg, "client_id", json_integer(client_id & 0xffffffffll)); |
|
|
|
client_id = client_id64 >> 32; |
|
|
|
|
|
|
|
json_object_set_new_nocheck(json_msg, "client_id", json_integer(passthrough_id)); |
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
client_id = client_id64; |
|
|
|
|
|
|
|
msg = json_dumps(json_msg, JSON_EOL); |
|
|
|
msg = json_dumps(json_msg, JSON_EOL); |
|
|
|
send_client(cdata, client_id, msg); |
|
|
|
send_client(cdata, client_id, msg); |
|
|
|
json_decref(json_msg); |
|
|
|
json_decref(json_msg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static char *connector_stats(cdata_t *cdata) |
|
|
|
static char *connector_stats(cdata_t *cdata, const int runtime) |
|
|
|
{ |
|
|
|
{ |
|
|
|
json_t *val = json_object(), *subval; |
|
|
|
json_t *val = json_object(), *subval; |
|
|
|
client_instance_t *client; |
|
|
|
client_instance_t *client; |
|
|
@ -706,6 +740,10 @@ static char *connector_stats(cdata_t *cdata) |
|
|
|
int64_t memsize; |
|
|
|
int64_t memsize; |
|
|
|
char *buf; |
|
|
|
char *buf; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* If called in passthrough mode we log stats instead of the stratifier */ |
|
|
|
|
|
|
|
if (runtime) |
|
|
|
|
|
|
|
json_set_int(val, "runtime", runtime); |
|
|
|
|
|
|
|
|
|
|
|
ck_rlock(&cdata->lock); |
|
|
|
ck_rlock(&cdata->lock); |
|
|
|
objects = HASH_COUNT(cdata->clients); |
|
|
|
objects = HASH_COUNT(cdata->clients); |
|
|
|
memsize = SAFE_HASH_OVERHEAD(cdata->clients) + sizeof(client_instance_t) * objects; |
|
|
|
memsize = SAFE_HASH_OVERHEAD(cdata->clients) + sizeof(client_instance_t) * objects; |
|
|
@ -728,68 +766,57 @@ static char *connector_stats(cdata_t *cdata) |
|
|
|
memsize = 0; |
|
|
|
memsize = 0; |
|
|
|
|
|
|
|
|
|
|
|
mutex_lock(&cdata->sender_lock); |
|
|
|
mutex_lock(&cdata->sender_lock); |
|
|
|
generated = cdata->sends_generated; |
|
|
|
|
|
|
|
DL_FOREACH(cdata->sender_sends, send) { |
|
|
|
DL_FOREACH(cdata->sender_sends, send) { |
|
|
|
objects++; |
|
|
|
objects++; |
|
|
|
memsize += sizeof(sender_send_t) + send->len + 1; |
|
|
|
memsize += sizeof(sender_send_t) + send->len + 1; |
|
|
|
} |
|
|
|
} |
|
|
|
mutex_unlock(&cdata->sender_lock); |
|
|
|
JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", cdata->sends_generated); |
|
|
|
|
|
|
|
|
|
|
|
JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); |
|
|
|
|
|
|
|
json_set_object(val, "sends", subval); |
|
|
|
json_set_object(val, "sends", subval); |
|
|
|
|
|
|
|
|
|
|
|
objects = 0; |
|
|
|
JSON_CPACK(subval, "{si,si,si}", "count", cdata->sends_queued, "memory", cdata->sends_size, "generated", cdata->sends_delayed); |
|
|
|
memsize = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mutex_lock(&cdata->sender_lock); |
|
|
|
|
|
|
|
generated = cdata->sends_delayed; |
|
|
|
|
|
|
|
DL_FOREACH(cdata->delayed_sends, send) { |
|
|
|
|
|
|
|
objects++; |
|
|
|
|
|
|
|
memsize += sizeof(sender_send_t) + send->len + 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mutex_unlock(&cdata->sender_lock); |
|
|
|
mutex_unlock(&cdata->sender_lock); |
|
|
|
|
|
|
|
|
|
|
|
JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); |
|
|
|
|
|
|
|
json_set_object(val, "delays", subval); |
|
|
|
json_set_object(val, "delays", subval); |
|
|
|
|
|
|
|
|
|
|
|
buf = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); |
|
|
|
buf = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); |
|
|
|
json_decref(val); |
|
|
|
json_decref(val); |
|
|
|
LOGNOTICE("Connector stats: %s", buf); |
|
|
|
if (runtime) |
|
|
|
|
|
|
|
LOGNOTICE("Passthrough:%s", buf); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LOGNOTICE("Connector stats: %s", buf); |
|
|
|
return buf; |
|
|
|
return buf; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int connector_loop(proc_instance_t *pi, cdata_t *cdata) |
|
|
|
static int connector_loop(proc_instance_t *pi, cdata_t *cdata) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int64_t client_id64, client_id; |
|
|
|
|
|
|
|
unix_msg_t *umsg = NULL; |
|
|
|
unix_msg_t *umsg = NULL; |
|
|
|
ckpool_t *ckp = pi->ckp; |
|
|
|
ckpool_t *ckp = pi->ckp; |
|
|
|
uint8_t test_cycle = 0; |
|
|
|
time_t last_stats; |
|
|
|
char *buf; |
|
|
|
int64_t client_id; |
|
|
|
int ret = 0; |
|
|
|
int ret = 0; |
|
|
|
|
|
|
|
char *buf; |
|
|
|
|
|
|
|
|
|
|
|
LOGWARNING("%s connector ready", ckp->name); |
|
|
|
LOGWARNING("%s connector ready", ckp->name); |
|
|
|
|
|
|
|
last_stats = cdata->start_time; |
|
|
|
|
|
|
|
|
|
|
|
retry: |
|
|
|
retry: |
|
|
|
|
|
|
|
if (ckp->passthrough) { |
|
|
|
|
|
|
|
time_t diff = time(NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (diff - last_stats >= 60) { |
|
|
|
|
|
|
|
last_stats = diff; |
|
|
|
|
|
|
|
diff -= cdata->start_time; |
|
|
|
|
|
|
|
buf = connector_stats(cdata, diff); |
|
|
|
|
|
|
|
dealloc(buf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (umsg) { |
|
|
|
if (umsg) { |
|
|
|
Close(umsg->sockd); |
|
|
|
Close(umsg->sockd); |
|
|
|
free(umsg->buf); |
|
|
|
free(umsg->buf); |
|
|
|
dealloc(umsg); |
|
|
|
dealloc(umsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!++test_cycle) { |
|
|
|
|
|
|
|
/* Test for pthread join every 256 messages */ |
|
|
|
|
|
|
|
if (unlikely(!pthread_tryjoin_np(cdata->pth_sender, NULL))) { |
|
|
|
|
|
|
|
LOGEMERG("Connector sender thread shutdown, exiting"); |
|
|
|
|
|
|
|
ret = 1; |
|
|
|
|
|
|
|
goto out; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (unlikely(!pthread_tryjoin_np(cdata->pth_receiver, NULL))) { |
|
|
|
|
|
|
|
LOGEMERG("Connector receiver thread shutdown, exiting"); |
|
|
|
|
|
|
|
ret = 1; |
|
|
|
|
|
|
|
goto out; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
do { |
|
|
|
umsg = get_unix_msg(pi); |
|
|
|
umsg = get_unix_msg(pi); |
|
|
|
} while (!umsg); |
|
|
|
} while (!umsg); |
|
|
@ -803,12 +830,14 @@ retry: |
|
|
|
} else if (cmdmatch(buf, "dropclient")) { |
|
|
|
} else if (cmdmatch(buf, "dropclient")) { |
|
|
|
client_instance_t *client; |
|
|
|
client_instance_t *client; |
|
|
|
|
|
|
|
|
|
|
|
ret = sscanf(buf, "dropclient=%"PRId64, &client_id64); |
|
|
|
ret = sscanf(buf, "dropclient=%"PRId64, &client_id); |
|
|
|
if (ret < 0) { |
|
|
|
if (ret < 0) { |
|
|
|
LOGDEBUG("Connector failed to parse dropclient command: %s", buf); |
|
|
|
LOGDEBUG("Connector failed to parse dropclient command: %s", buf); |
|
|
|
goto retry; |
|
|
|
goto retry; |
|
|
|
} |
|
|
|
} |
|
|
|
client_id = client_id64 & 0xffffffffll; |
|
|
|
/* A passthrough client, we can't drop this yet */ |
|
|
|
|
|
|
|
if (client_id > 0xffffffffll) |
|
|
|
|
|
|
|
goto retry; |
|
|
|
client = ref_client_by_id(cdata, client_id); |
|
|
|
client = ref_client_by_id(cdata, client_id); |
|
|
|
if (unlikely(!client)) { |
|
|
|
if (unlikely(!client)) { |
|
|
|
LOGINFO("Connector failed to find client id %"PRId64" to drop", client_id); |
|
|
|
LOGINFO("Connector failed to find client id %"PRId64" to drop", client_id); |
|
|
@ -831,7 +860,7 @@ retry: |
|
|
|
char *msg; |
|
|
|
char *msg; |
|
|
|
|
|
|
|
|
|
|
|
LOGDEBUG("Connector received stats request"); |
|
|
|
LOGDEBUG("Connector received stats request"); |
|
|
|
msg = connector_stats(cdata); |
|
|
|
msg = connector_stats(cdata, 0); |
|
|
|
send_unix_msg(umsg->sockd, msg); |
|
|
|
send_unix_msg(umsg->sockd, msg); |
|
|
|
} else if (cmdmatch(buf, "loglevel")) { |
|
|
|
} else if (cmdmatch(buf, "loglevel")) { |
|
|
|
sscanf(buf, "loglevel=%d", &ckp->loglevel); |
|
|
|
sscanf(buf, "loglevel=%d", &ckp->loglevel); |
|
|
@ -910,7 +939,9 @@ int connector(proc_instance_t *pi) |
|
|
|
Close(sockd); |
|
|
|
Close(sockd); |
|
|
|
goto out; |
|
|
|
goto out; |
|
|
|
} |
|
|
|
} |
|
|
|
if (listen(sockd, SOMAXCONN) < 0) { |
|
|
|
/* Set listen backlog to larger than SOMAXCONN in case the
|
|
|
|
|
|
|
|
* system configuration supports it */ |
|
|
|
|
|
|
|
if (listen(sockd, 8192) < 0) { |
|
|
|
LOGERR("Connector failed to listen on socket"); |
|
|
|
LOGERR("Connector failed to listen on socket"); |
|
|
|
Close(sockd); |
|
|
|
Close(sockd); |
|
|
|
goto out; |
|
|
|
goto out; |
|
|
@ -952,7 +983,7 @@ int connector(proc_instance_t *pi) |
|
|
|
ret = 1; |
|
|
|
ret = 1; |
|
|
|
goto out; |
|
|
|
goto out; |
|
|
|
} |
|
|
|
} |
|
|
|
if (listen(sockd, SOMAXCONN) < 0) { |
|
|
|
if (listen(sockd, 8192) < 0) { |
|
|
|
LOGERR("Connector failed to listen on socket"); |
|
|
|
LOGERR("Connector failed to listen on socket"); |
|
|
|
Close(sockd); |
|
|
|
Close(sockd); |
|
|
|
goto out; |
|
|
|
goto out; |
|
|
@ -974,6 +1005,7 @@ int connector(proc_instance_t *pi) |
|
|
|
cond_init(&cdata->sender_cond); |
|
|
|
cond_init(&cdata->sender_cond); |
|
|
|
create_pthread(&cdata->pth_sender, sender, cdata); |
|
|
|
create_pthread(&cdata->pth_sender, sender, cdata); |
|
|
|
create_pthread(&cdata->pth_receiver, receiver, cdata); |
|
|
|
create_pthread(&cdata->pth_receiver, receiver, cdata); |
|
|
|
|
|
|
|
cdata->start_time = time(NULL); |
|
|
|
|
|
|
|
|
|
|
|
create_unix_receiver(pi); |
|
|
|
create_unix_receiver(pi); |
|
|
|
|
|
|
|
|
|
|
|