Skip to content

Commit 9a51457

Browse files
committed
monitoring (2)
1 parent c75e0cb commit 9a51457

4 files changed

Lines changed: 35 additions & 26 deletions

File tree

src/core/connection.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ int connection_open_listener(struct scache_bind ibind) {
242242
return -1;
243243
}
244244

245-
static scache_connection* connection_add(int fd, scache_connection_node* ctable) {
245+
static scache_connection* connection_add(int fd) {
246246
scache_connection_node* node = &ctable[CONNECTION_HASH_KEY(fd)];
247247
if (node->connection.client_sock != -1) {
248248
while (node->next != NULL) {
@@ -263,7 +263,7 @@ static scache_connection* connection_add(int fd, scache_connection_node* ctable)
263263
return &(node->connection);
264264
}
265265

266-
static scache_connection* connection_get(int fd, scache_connection_node* ctable) {
266+
static scache_connection* connection_get(int fd) {
267267
scache_connection_node* node = &ctable[CONNECTION_HASH_KEY(fd)];
268268
if (node->connection.client_sock == -1) {
269269
return NULL;
@@ -281,7 +281,7 @@ static scache_connection* connection_get(int fd, scache_connection_node* ctable)
281281
return &(node->connection);
282282
}
283283

284-
static bool connection_remove(int fd, scache_connection_node* ctable) {
284+
bool connection_remove(int fd) {
285285
scache_connection_node* temp = NULL;
286286
scache_connection_node* node = &ctable[CONNECTION_HASH_KEY(fd)];
287287
if (node->connection.client_sock == -1) {
@@ -321,7 +321,7 @@ static bool connection_remove(int fd, scache_connection_node* ctable) {
321321
return true;
322322
}
323323

324-
static unsigned int connection_count(scache_connection_node* ctable) {
324+
static unsigned int connection_count() {
325325
unsigned count = 0;
326326
for (unsigned int i = 0; i < CONNECTION_HASH_ENTRIES; i++) {
327327
scache_connection_node* target = &ctable[i];
@@ -337,7 +337,7 @@ static unsigned int connection_count(scache_connection_node* ctable) {
337337
return count;
338338
}
339339

340-
static unsigned int connection_any(scache_connection_node* ctable) {\
340+
static unsigned int connection_any() {\
341341
for (unsigned int i = 0; i < CONNECTION_HASH_ENTRIES; i++) {
342342
scache_connection_node* target = &ctable[i];
343343
if (target->connection.client_sock != -1) return true;
@@ -547,7 +547,7 @@ void connection_event_loop(void (*connection_handler)(scache_connection* connect
547547

548548
//Handle connection
549549
DEBUG("[#%d] A new %s socket was accepted %d\n", fd, listener_type_string(client_type), client_sock);
550-
scache_connection* connection = connection_add(client_sock, ctable);
550+
scache_connection* connection = connection_add(client_sock);
551551
assert(connection->client_sock == client_sock);
552552
connection->ltype = client_type;
553553
connection_handler(connection);
@@ -569,7 +569,7 @@ void connection_event_loop(void (*connection_handler)(scache_connection* connect
569569
if (fd != efd)
570570
{
571571
DEBUG("[#%d] Got socket event %d (in=%d, out=%d, hup=%d)\n", fd, events[n].events, events[n].events & EPOLLIN ? 1 : 0, events[n].events & EPOLLOUT ? 1 : 0, events[n].events & EPOLLHUP ? 1 : 0);
572-
scache_connection* connection = connection_get(fd, ctable);
572+
scache_connection* connection = connection_get(fd);
573573
if (connection != NULL) {
574574
assert(connection->client_sock == fd);
575575
bool do_close = events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP);
@@ -589,10 +589,10 @@ void connection_event_loop(void (*connection_handler)(scache_connection* connect
589589
DEBUG("[#%d] Closing connection due to err:%d hup:%d rdhup:%d\n", fd, !! (events[n].events&EPOLLERR), !! (events[n].events&EPOLLHUP), !! (events[n].events&EPOLLRDHUP));
590590
http_cleanup(connection);
591591
assert(fd != 0 || (settings.daemon_mode && fd >= 0));
592-
if(connection_remove(fd, ctable)){
593-
assert(connection_get(fd, ctable) == NULL);
592+
if(connection_remove(fd)){
593+
assert(connection_get(fd) == NULL);
594594
#ifdef DEBUG_BUILD
595-
if (!connection_any(ctable)) {
595+
if (!connection_any()) {
596596
db_check_table_refs();
597597
}
598598
#endif

src/core/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void connection_close_listeners();
1111
void connection_event_loop(void(*connection_handler)(scache_connection* connection));
1212
void connection_setup(struct scache_binds cache_binds, struct scache_binds cache_monitor);
1313
void connection_cleanup();
14+
bool connection_remove(int fd);
1415

1516
#define _DEBUG_CONNECTION_HANDLER
1617
#ifdef DEBUG_CONNECTION_HANDLER

src/core/http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static const char http_templates[][128] = {
8383
"HTTP/1.1 400 Bad Request\r\nConnection: Close\r\nContent-Length: 19\r\n\r\nRequest too large\r\n",
8484
"HTTP/1.1 400 Bad Request\r\nConnection: Close\r\nContent-Length: 17\r\n\r\nUnknown Request\r\n",
8585
"HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n",
86-
"HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/x-streaming\r\nTransfer-Encoding: chunked\r\n\r\n",
86+
"HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/x-streaming\r\n\r\n",
8787
"HTTP/1.1 404 Not Found\r\nConnection: Keep-Alive\r\nContent-Length: 16\r\n\r\nPath Not Found\r\n",
8888
};
8989

src/core/http_parse_mon.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#include "http_parse.h"
2828
#include "http_respond.h"
2929

30-
static char monitoring_strings[0xffff][7] = {};
31-
static uint8_t monitoring_lens[0xffff] = {};
30+
#define MONITORING_DEFAULT_INTERVAL 5
31+
static char monitoring_strings[0x10000][7] = {};
32+
static uint8_t monitoring_lens[0x10000] = {};
3233

3334
static state_action http_write_response_after_eol(scache_connection* connection, int http_template) {
3435
CONNECTION_HANDLER(connection, http_handle_eolwritetoend);
@@ -76,8 +77,8 @@ state_action http_respond_writecount_starting(scache_connection* connection) {
7677

7778
static state_action http_respond_start_to_count(scache_connection* connection) {
7879
DEBUG("[#%d] Ready to start to count \n", connection->client_sock);
79-
http_respond_cleanupafterwrite(connection);
8080
monitoring_add(connection);
81+
return http_respond_cleanupafterwrite(connection);
8182
}
8283

8384
state_action http_mon_read_eol_inital(scache_connection* connection, char* buffer, int n, uint32_t& temporary) {
@@ -342,18 +343,6 @@ void monitoring_check(){
342343

343344
// Not yet time
344345
if(timercmp(&conn->monitoring.scheduled, &current_time, >)) break;
345-
346-
// Add to output buffer
347-
// this will override anything already there
348-
// if we can't write in the time allocated just discard. It's not worth buffering.
349-
conn->output_buffer = monitoring_strings[conn->monitoring.current];
350-
conn->output_length = monitoring_lens[conn->monitoring.current];
351-
conn->monitoring.current ++;
352-
memcpy(&conn->monitoring.scheduled, (void*)&current_time, sizeof(current_time));
353-
conn->monitoring.scheduled.tv_sec += 1;
354-
355-
// signal for write registration
356-
connection_register_write(conn->client_sock);
357346

358347
// move on
359348
mon_head = conn->monitoring.next;
@@ -367,6 +356,25 @@ void monitoring_check(){
367356
mon_tail = mon_head = conn;
368357
conn->monitoring.next = conn->monitoring.prev = NULL;
369358
}
359+
360+
// If buffer wasnt cleared already, then we will need to disconnect
361+
if(conn->output_buffer != NULL){
362+
http_cleanup(conn);
363+
connection_remove(conn->client_sock);
364+
return;
365+
}
366+
367+
// Add to output buffer
368+
// this will override anything already there
369+
// if we can't write in the time allocated just discard. It's not worth buffering.
370+
conn->output_buffer = monitoring_strings[conn->monitoring.current];
371+
conn->output_length = monitoring_lens[conn->monitoring.current];
372+
conn->monitoring.current ++;
373+
memcpy(&conn->monitoring.scheduled, (void*)&current_time, sizeof(current_time));
374+
conn->monitoring.scheduled.tv_sec += MONITORING_DEFAULT_INTERVAL;
375+
376+
// signal for write registration
377+
connection_register_write(conn->client_sock);
370378
}
371379
}
372380

0 commit comments

Comments
 (0)