Use monotonic time
This commit is contained in:
parent
34baf6e841
commit
bdec94020e
|
@ -55,8 +55,8 @@ AC_FUNC_FORK
|
||||||
AC_FUNC_MALLOC
|
AC_FUNC_MALLOC
|
||||||
AC_FUNC_MKTIME
|
AC_FUNC_MKTIME
|
||||||
AC_FUNC_REALLOC
|
AC_FUNC_REALLOC
|
||||||
AC_CHECK_FUNCS(ftruncate gethostname gettimeofday localtime_r memmove memset)
|
AC_CHECK_FUNCS(clock_gettime ftruncate gethostname gettimeofday localtime_r memmove)
|
||||||
AC_CHECK_FUNCS(mkdir select socket strcasecmp strchr strcspn strdup strerror)
|
AC_CHECK_FUNCS(memset mkdir select socket strcasecmp strchr strcspn strdup strerror)
|
||||||
AC_CHECK_FUNCS(strrchr strstr)
|
AC_CHECK_FUNCS(strrchr strstr)
|
||||||
|
|
||||||
# Deal with parameters
|
# Deal with parameters
|
||||||
|
|
|
@ -742,7 +742,7 @@ static int check_event_write(fd_set *fds, connection_t *cn, int *nc)
|
||||||
static int cn_want_write(connection_t *cn)
|
static int cn_want_write(connection_t *cn)
|
||||||
{
|
{
|
||||||
if (cn->anti_flood) {
|
if (cn->anti_flood) {
|
||||||
struct timeval tv;
|
struct timespec tv;
|
||||||
unsigned long now;
|
unsigned long now;
|
||||||
|
|
||||||
/* fill the bucket */
|
/* fill the bucket */
|
||||||
|
@ -750,8 +750,8 @@ static int cn_want_write(connection_t *cn)
|
||||||
/* now is the number of milliseconds since the Epoch,
|
/* now is the number of milliseconds since the Epoch,
|
||||||
* cn->lasttoken is the number of milliseconds when we
|
* cn->lasttoken is the number of milliseconds when we
|
||||||
* last added a token to the bucket */
|
* last added a token to the bucket */
|
||||||
if (!gettimeofday(&tv, NULL)) {
|
if (!clock_gettime(CLOCK_MONOTONIC, &tv)) {
|
||||||
now = tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
now = tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
|
||||||
/* round now to TOKEN_INTERVAL multiple */
|
/* round now to TOKEN_INTERVAL multiple */
|
||||||
now -= now % TOKEN_INTERVAL;
|
now -= now % TOKEN_INTERVAL;
|
||||||
if (now < cn->lasttoken) {
|
if (now < cn->lasttoken) {
|
||||||
|
@ -768,7 +768,7 @@ static int cn_want_write(connection_t *cn)
|
||||||
cn->lasttoken = now;
|
cn->lasttoken = now;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
/* if gettimeofday() fails, juste ignore
|
/* if clock_gettime() fails, juste ignore
|
||||||
* antiflood */
|
* antiflood */
|
||||||
cn->token = 1;
|
cn->token = 1;
|
||||||
|
|
||||||
|
@ -782,14 +782,15 @@ static int cn_want_write(connection_t *cn)
|
||||||
return !list_is_empty(cn->outgoing);
|
return !list_is_empty(cn->outgoing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
list_t *wait_event(list_t *cn_list, int *msec, int *nc)
|
list_t *wait_event(list_t *cn_list, int *msec, int *nc)
|
||||||
{
|
{
|
||||||
fd_set fds_read, fds_write, fds_except;
|
fd_set fds_read, fds_write, fds_except;
|
||||||
int maxfd = -1, err;
|
int maxfd = -1, err, errtime;
|
||||||
list_t *cn_newdata;
|
list_t *cn_newdata;
|
||||||
list_iterator_t it;
|
list_iterator_t it;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
struct timeval btv, etv;
|
struct timespec btv, etv;
|
||||||
*nc = 0;
|
*nc = 0;
|
||||||
|
|
||||||
cn_newdata = list_new(list_ptr_cmp);
|
cn_newdata = list_new(list_ptr_cmp);
|
||||||
|
@ -853,31 +854,45 @@ list_t *wait_event(list_t *cn_list, int *msec, int *nc)
|
||||||
|
|
||||||
tv.tv_sec = *msec / 1000;
|
tv.tv_sec = *msec / 1000;
|
||||||
tv.tv_usec = (*msec % 1000) * 1000;
|
tv.tv_usec = (*msec % 1000) * 1000;
|
||||||
gettimeofday(&btv, NULL);
|
|
||||||
mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, tv.tv_sec,
|
mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, tv.tv_sec,
|
||||||
tv.tv_usec);
|
tv.tv_usec);
|
||||||
err = select(maxfd + 1, &fds_read, &fds_write, &fds_except, &tv);
|
|
||||||
gettimeofday(&etv, NULL);
|
|
||||||
|
|
||||||
if (etv.tv_sec < btv.tv_sec)
|
errtime = clock_gettime(CLOCK_MONOTONIC, &btv);
|
||||||
mylog(LOG_ERROR, "Time rewinded ! not touching interval");
|
if (errtime != 0) {
|
||||||
else
|
fatal("clock_gettime: %s", strerror(errno));
|
||||||
*msec -= (etv.tv_sec - btv.tv_sec) * 1000
|
}
|
||||||
+ (etv.tv_usec - btv.tv_usec) / 1000;
|
|
||||||
/* in case we go forward in time */
|
err = select(maxfd + 1, &fds_read, &fds_write, &fds_except, &tv);
|
||||||
if (*msec < 0)
|
|
||||||
*msec = 0;
|
if (err == 0) {
|
||||||
mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, tv.tv_sec,
|
|
||||||
tv.tv_usec);
|
|
||||||
if (err < 0) {
|
|
||||||
if (errno == EINTR)
|
|
||||||
return cn_newdata;
|
|
||||||
fatal("select(): %s", strerror(errno));
|
|
||||||
} else if (err == 0) {
|
|
||||||
/* select timed-out */
|
/* select timed-out */
|
||||||
mylog(LOG_DEBUGTOOMUCH, "Select timed-out. irc.o timer !");
|
mylog(LOG_DEBUGTOOMUCH, "Select timed-out. irc.o timer !");
|
||||||
*msec = 0;
|
*msec = 0;
|
||||||
return cn_newdata;
|
return cn_newdata;
|
||||||
|
} else {
|
||||||
|
mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec,
|
||||||
|
tv.tv_sec, tv.tv_usec);
|
||||||
|
}
|
||||||
|
|
||||||
|
errtime = clock_gettime(CLOCK_MONOTONIC, &etv);
|
||||||
|
if (errtime != 0) {
|
||||||
|
fatal("clock_gettime: %s", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (etv.tv_sec < btv.tv_sec)
|
||||||
|
mylog(LOG_ERROR, "Time rewinded ! not touching interval");
|
||||||
|
else {
|
||||||
|
*msec -= (etv.tv_sec - btv.tv_sec) * 1000
|
||||||
|
+ (etv.tv_nsec - btv.tv_nsec) / 1000000;
|
||||||
|
/* in case we go forward in time */
|
||||||
|
if (*msec < 0)
|
||||||
|
*msec = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err < 0) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
return cn_newdata;
|
||||||
|
fatal("select(): %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (list_it_init(cn_list, &it); list_it_item(&it); list_it_next(&it)) {
|
for (list_it_init(cn_list, &it); list_it_item(&it); list_it_next(&it)) {
|
||||||
|
|
Loading…
Reference in New Issue