Skip to content

Commit

Permalink
Always use hi-res timers internally.
Browse files Browse the repository at this point in the history
This helps us to avoid off-by-one and roundoff errors that crop up with
one-second-granularity timers. Also makes it easier in the future to
allow clients to specify hi-res time intervals.

Closes beanstalkdgh-5.
  • Loading branch information
kr committed Oct 1, 2009
1 parent e306226 commit 6e9339f
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 84 deletions.
6 changes: 3 additions & 3 deletions binlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ uint64_t last_fsync = 0;

char *binlog_dir = NULL;
static int binlog_index = 0;
static int binlog_version = 4;
static int binlog_version = 5;
static int lock_fd;

static binlog oldest_binlog = 0,
Expand Down Expand Up @@ -208,7 +208,7 @@ binlog_read_log_file(binlog b, job binlog_jobs)
j = make_job_with_id(js.pri, js.delay, js.ttr, js.body_size,
t, js.id);
j->next = j->prev = j;
j->creation = js.creation;
j->created_at = js.created_at;
job_insert(binlog_jobs, j);
}
if (js.body_size) {
Expand All @@ -234,7 +234,7 @@ binlog_read_log_file(binlog b, job binlog_jobs)
}
if (j) {
j->state = js.state;
j->deadline = js.deadline;
j->deadline_at = js.deadline_at;
j->pri = js.pri;
j->delay = js.delay;
j->ttr = js.ttr;
Expand Down
18 changes: 9 additions & 9 deletions conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <limits.h>

Expand All @@ -27,7 +26,7 @@
#include "util.h"
#include "prot.h"

#define SAFETY_MARGIN 1 /* seconds */
#define SAFETY_MARGIN (1 * SECOND)

/* Doubly-linked list of free connections. */
static struct conn pool = { &pool, &pool, 0 };
Expand Down Expand Up @@ -151,19 +150,20 @@ conn_set_evq(conn c, const int events, evh handler)
{
int r, margin = 0, should_timeout = 0;
struct timeval tv = {INT_MAX, 0};
usec t = UINT64_MAX;

event_set(&c->evq, c->fd, events, handler, c);

if (conn_waiting(c)) margin = 1;
if (conn_waiting(c)) margin = SAFETY_MARGIN;
if (has_reserved_job(c)) {
time_t t = soonest_job(c)->deadline - time(NULL) - margin;
tv.tv_sec = t > 0 ? t : 0;
t = soonest_job(c)->deadline_at - now_usec() - margin;
should_timeout = 1;
}
if (c->pending_timeout >= 0) {
tv.tv_sec = min(tv.tv_sec, c->pending_timeout);
t = min(t, c->pending_timeout * SECOND);
should_timeout = 1;
}
if (should_timeout) timeval_from_usec(&tv, t);

r = event_add(&c->evq, should_timeout ? &tv : NULL);
if (r == -1) return twarn("event_add() err %d", errno), -1;
Expand Down Expand Up @@ -226,7 +226,7 @@ soonest_job(conn c)

if (soonest == NULL) {
for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
if (j->deadline_at <= (soonest ? : j)->deadline_at) soonest = j;
}
}
c->soonest_job = soonest;
Expand All @@ -244,10 +244,10 @@ has_reserved_this_job(conn c, job j)
int
conn_has_close_deadline(conn c)
{
time_t t = time(NULL);
usec t = now_usec();
job j = soonest_job(c);

return j && t >= j->deadline - SAFETY_MARGIN;
return j && t >= j->deadline_at - SAFETY_MARGIN;
}

int
Expand Down
1 change: 0 additions & 1 deletion conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define conn_h

#include <sys/types.h>
#include <sys/time.h>
#include <event.h>
#include "ms.h"
#include "tube.h"
Expand Down
8 changes: 4 additions & 4 deletions job.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ allocate_job(int body_size)

j->id = 0;
j->state = JOB_STATE_INVALID;
j->creation = time(NULL);
j->created_at = now_usec();
j->reserve_ct = j->timeout_ct = j->release_ct = j->bury_ct = j->kick_ct = 0;
j->body_size = body_size;
j->next = j->prev = j; /* not in a linked list */
Expand All @@ -124,7 +124,7 @@ allocate_job(int body_size)
}

job
make_job_with_id(unsigned int pri, unsigned int delay, unsigned int ttr,
make_job_with_id(unsigned int pri, usec delay, usec ttr,
int body_size, tube tube, uint64_t id)
{
job j;
Expand Down Expand Up @@ -188,8 +188,8 @@ job_pri_cmp(job a, job b)
int
job_delay_cmp(job a, job b)
{
if (a->deadline > b->deadline) return 1;
if (a->deadline < b->deadline) return -1;
if (a->deadline_at > b->deadline_at) return 1;
if (a->deadline_at < b->deadline_at) return -1;
if (a->id > b->id) return 1;
if (a->id < b->id) return -1;
return 0;
Expand Down
12 changes: 6 additions & 6 deletions job.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# include <stdint.h>
#endif /* else we get int types from config.h */

#include <time.h>
#include "util.h"

typedef struct job *job;
typedef int(*job_cmp_fn)(job, job);
Expand All @@ -46,11 +46,11 @@ struct job {
/* persistent fields; these get written to the binlog */
uint64_t id;
uint32_t pri;
uint32_t delay;
uint32_t ttr;
usec delay;
usec ttr;
int32_t body_size;
time_t creation;
time_t deadline;
usec created_at;
usec deadline_at;
uint32_t reserve_ct;
uint32_t timeout_ct;
uint32_t release_ct;
Expand All @@ -75,7 +75,7 @@ struct job {
#define make_job(pri,delay,ttr,body_size,tube) make_job_with_id(pri,delay,ttr,body_size,tube,0)

job allocate_job(int body_size);
job make_job_with_id(unsigned int pri, unsigned int delay, unsigned int ttr,
job make_job_with_id(unsigned int pri, usec delay, usec ttr,
int body_size, tube tube, uint64_t id);
void job_free(job j);

Expand Down
13 changes: 8 additions & 5 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
static int listen_socket = -1;
static struct event listen_evq;
static evh accept_handler;
static time_t main_deadline = 0;
static usec main_deadline = 0;
static int brakes_are_on = 1, after_startup = 0;

int
Expand Down Expand Up @@ -99,12 +99,15 @@ unbrake(evh h)
}

void
set_main_timeout(time_t deadline)
set_main_timeout(usec deadline_at)
{
int r;
struct timeval tv = {deadline - time(NULL), 0};
struct timeval tv;
usec now = now_usec();

main_deadline = deadline;
r = event_add(&listen_evq, deadline ? &tv : NULL);
timeval_from_usec(&tv, deadline_at - now);

main_deadline = deadline_at;
r = event_add(&listen_evq, deadline_at ? &tv : NULL);
if (r == -1) twarn("event_add()");
}
4 changes: 2 additions & 2 deletions net.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef net_h
#define net_h

#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
Expand All @@ -26,11 +25,12 @@

#include "event.h"
#include "conn.h"
#include "util.h"

int make_server_socket(struct in_addr host_addr, int port);

void brake();
void unbrake(evh h);
void set_main_timeout(time_t deadline);
void set_main_timeout(usec deadline_at);

#endif /*net_h*/
Loading

0 comments on commit 6e9339f

Please sign in to comment.