Skip to content

Commit

Permalink
Reserve timeout, part 1.
Browse files Browse the repository at this point in the history
If we are already within the safety margin and the client issues a new
reserve command, just respond with the timeout message.
  • Loading branch information
Keith Rarick committed Mar 8, 2008
1 parent ad38067 commit f96ee87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
11 changes: 9 additions & 2 deletions doc/protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id> <bytes>\r\n
<data>\r\n
Expand Down
3 changes: 3 additions & 0 deletions prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f96ee87

Please sign in to comment.