Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent system time changes from affecting timeouts, other misc changes. #51

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PROFILEFLAGS=-pg -profile
# -fdump-rtl-expand
endif
CFLAGS=-Wall -O2 -minline-all-stringops -I ./deps/udns-0.0.9/
LFLAGS=-rdynamic -ldl -lm -lpthread
LFLAGS=-rdynamic -ldl -lm -lpthread -lrt
CC=gcc -D_GNU_SOURCE
RM=rm -f

Expand Down
6 changes: 5 additions & 1 deletion modules/libape-spidermonkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,7 @@ static void sm_sock_ondisconnect(ape_socket *client, acetables *g_ape)

free(cb->private);
free(cb);
client->attach = NULL;

//JS_EndRequest(cb->asc->cx);
//JS_ClearContextThread(cb->asc->cx);
Expand Down Expand Up @@ -4743,7 +4744,8 @@ static void mysac_setdb_success(struct _ape_mysql_data *myhandle, int code)

/* TODO : Supress queue */

free(myhandle->db);
free(myhandle->db);
myhandle->db = NULL;
}
}

Expand Down Expand Up @@ -4772,6 +4774,7 @@ static void mysac_connect_success(struct _ape_mysql_data *myhandle, int code)
myhandle->on_success = NULL;

free(myhandle->db);
myhandle->db = NULL;
}
}

Expand Down Expand Up @@ -4832,6 +4835,7 @@ static void mysac_query_success(struct _ape_mysql_data *myhandle, int code)
JS_free(myhandle->cx, queue->query);
free(queue->res);
free(queue);
myhandle->data = NULL;

apemysql_shift_queue(myhandle);
}
Expand Down
2 changes: 1 addition & 1 deletion src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void process_websocket(ape_socket *co, acetables *g_ape)
}

if (buffer->length > 502400) {
shutdown(co->fd, 2);
safe_shutdown(co->fd, g_ape);
return;
}

Expand Down
7 changes: 5 additions & 2 deletions src/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ SOFTWARE.
#include <locale.h>

#include "json_parser.h"
#include "utils.h"

#ifdef _MSC_VER
# if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
Expand Down Expand Up @@ -624,8 +625,9 @@ static int parse_parse_buffer(JSON_parser jc)
break;
case JSON_T_STRING:
arg = &value;
urldecode(jc->parse_buffer);
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
value.vu.str.length = strlen(jc->parse_buffer);
break;
}

Expand Down Expand Up @@ -1037,8 +1039,9 @@ JSON_parser_char(JSON_parser jc, int next_char)

if (jc->callback) {
JSON_value value;
urldecode(jc->parse_buffer);
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
value.vu.str.length = strlen(jc->parse_buffer);
if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
return false;
}
Expand Down
34 changes: 26 additions & 8 deletions src/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ unsigned int sockroutine(acetables *g_ape)

int new_fd, nfds, sin_size = sizeof(struct sockaddr_in), i, tfd = 0;

struct timeval t_start, t_end;
struct timespec t_start, t_end;
long int ticks = 0, uticks = 0, lticks = 0;
struct sockaddr_in their_addr;

Expand All @@ -291,7 +291,13 @@ unsigned int sockroutine(acetables *g_ape)
#if 0
add_periodical(5, 0, check_idle, &sl, g_ape);
#endif
gettimeofday(&t_start, NULL);
if (clock_gettime(CLOCK_MONOTONIC, &t_start) < 0)
{
ape_log(APE_ERR, __FILE__, __LINE__, g_ape,
"clock_gettime(CLOCK_MONOTONIC) : %s", strerror(errno));
return 1;
}

while (server_is_running) {
/* Linux 2.6.25 provides a fd-driven timer system. It could be usefull to implement */
int timeout_to_hang = get_first_timer_ms(g_ape);
Expand Down Expand Up @@ -502,13 +508,25 @@ unsigned int sockroutine(acetables *g_ape)
}
}
}

gettimeofday(&t_end, NULL);


if (clock_gettime(CLOCK_MONOTONIC, &t_end) < 0)
{
ape_log(APE_ERR, __FILE__, __LINE__, g_ape,
"clock_gettime(CLOCK_MONOTONIC) : %s", strerror(errno));
return 1;
}

ticks = 0;

uticks = 1000000L * (t_end.tv_sec - t_start.tv_sec);
uticks += (t_end.tv_usec - t_start.tv_usec);

if (t_end.tv_sec >= t_start.tv_sec)
{
uticks = 1000000L * (t_end.tv_sec - t_start.tv_sec);
uticks += ((t_end.tv_nsec - t_start.tv_nsec) / 1000);
} else {
ape_log(APE_ERR, __FILE__, __LINE__, g_ape,
"end time less than start time (start:%ld, end:%ld)", t_start.tv_sec, t_end.tv_sec);
uticks = 1000; // a total hack, but probably better than 0
}
t_start = t_end;
lticks += uticks;
/* Tic tac, tic tac */
Expand Down