From f96ee87c8fa2d13e6fdc7fea73d0969f52b1deb9 Mon Sep 17 00:00:00 2001 From: Keith Rarick Date: Fri, 7 Mar 2008 15:40:14 -0800 Subject: [PATCH] Reserve timeout, part 1. If we are already within the safety margin and the client issues a new reserve command, just respond with the timeout message. --- conn.c | 13 +++++++++++++ conn.h | 1 + doc/protocol.txt | 11 +++++++++-- prot.c | 3 +++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/conn.c b/conn.c index 2dda059b..4d0f1bef 100644 --- a/conn.c +++ b/conn.c @@ -26,6 +26,8 @@ #include "util.h" #include "prot.h" +#define SAFETY_MARGIN 1 /* seconds */ + /* Doubly-linked list of free connections. */ static struct conn pool = { &pool, &pool, 0 }; @@ -226,6 +228,17 @@ has_reserved_this_job(conn c, job needle) return 0; } +/* return true if c has a reserved job with less than one second until its + * deadline */ +int +conn_has_close_deadline(conn c) +{ + time_t t = time(NULL); + job j = soonest_job(c); + + return j && t >= j->deadline - SAFETY_MARGIN; +} + void conn_close(conn c) { diff --git a/conn.h b/conn.h index eb5d8e9d..ee2d43ad 100644 --- a/conn.h +++ b/conn.h @@ -107,5 +107,6 @@ void conn_set_worker(conn c); job soonest_job(conn c); int has_reserved_this_job(conn c, job j); +int conn_has_close_deadline(conn c); #endif /*conn_h*/ diff --git a/doc/protocol.txt b/doc/protocol.txt index bbc32fee..08c4ebea 100644 --- a/doc/protocol.txt +++ b/doc/protocol.txt @@ -200,8 +200,15 @@ is reserved for the client, the client has limited time to run the job before the job times out, when the server will put the job back into the ready queue. The time available can be found by asking the server for the job's stats. -There is only one possible response in the form of a text line followed by the -job body: +If the client has previously reserved job J and issues a reserve command when +J has less than one second left in its ttr interval, the server will respond +with: + +TIMEOUT\r\n + +This gives the client a chance to delete or release job J before the server +automatically releases it. Otherwise, the only response is in the form of a +text line followed by the job body: RESERVED \r\n \r\n diff --git a/prot.c b/prot.c index eee9fdef..9d6a6e1a 100644 --- a/prot.c +++ b/prot.c @@ -84,6 +84,7 @@ #define MSG_FOUND "FOUND" #define MSG_NOTFOUND "NOT_FOUND\r\n" #define MSG_RESERVED "RESERVED" +#define MSG_TIMEOUT "TIMEOUT\r\n" #define MSG_DELETED "DELETED\r\n" #define MSG_RELEASED "RELEASED\r\n" #define MSG_BURIED "BURIED\r\n" @@ -1073,6 +1074,8 @@ dispatch_cmd(conn c) reserve_ct++; /* stats */ conn_set_worker(c); + if (conn_has_close_deadline(c)) return reply_msg(c, MSG_TIMEOUT); + /* try to get a new job for this guy */ wait_for_job(c); process_queue();