GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Beanstalkd is a fast, distributed, in-memory workqueue service. Its interface is generic, but was designed for use in reducing the latency of page views in high-volume web applications by running most time-consuming tasks asynchronously.
Homepage: http://xph.us/software/beanstalkd/
Clone URL: git://github.com/kr/beanstalkd.git
Click here to lend your support to: beanstalkd and make a donation at www.pledgie.com !
Memoize the soonest job.
dustin (author)
Fri Jun 06 14:39:29 -0700 2008
commit  9856ada75c976411ca50dff8bb465233fdf439e8
tree    da71e1f4d1b04b253aaec3598e57a31dd68dda37
parent  8b0e9bdfd91509fa01b21e72e1a2007052028b0c
0
...
86
87
88
 
89
90
91
...
220
221
222
223
 
 
224
225
226
 
 
 
 
227
 
228
229
230
...
86
87
88
89
90
91
92
...
221
222
223
 
224
225
226
 
 
227
228
229
230
231
232
233
234
235
0
@@ -86,6 +86,7 @@ make_conn(int fd, char start_state, tube use, tube watch)
0
     c->type = 0;
0
     c->cmd_read = 0;
0
     c->pending_timeout = -1;
0
+ c->soonest_job = NULL;
0
     c->in_job = c->out_job = NULL;
0
     c->in_job_read = c->out_job_sent = 0;
0
     c->prev = c->next = c; /* must be out of a linked list right now */
0
@@ -220,11 +221,15 @@ conn_insert(conn head, conn c)
0
 job
0
 soonest_job(conn c)
0
 {
0
- job j, soonest = NULL;
0
+ job j = NULL;
0
+ job soonest = c->soonest_job;
0
 
0
- for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
0
- if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
0
+ if (soonest == NULL) {
0
+ for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
0
+ if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
0
+ }
0
     }
0
+ c->soonest_job = soonest;
0
     return soonest;
0
 }
0
 
0
...
60
61
62
 
 
 
63
64
65
...
60
61
62
63
64
65
66
67
68
0
@@ -60,6 +60,9 @@ struct conn {
0
     /* A job to be read from the client. */
0
     job in_job;
0
 
0
+ /* Memoization of the soonest job */
0
+ job soonest_job;
0
+
0
     /* How many bytes of in_job->body have been read so far. If in_job is NULL
0
      * while in_job_read is nonzero, we are in bit bucket mode and
0
      * in_job_read's meaning is inverted -- then it counts the bytes that
0
...
340
341
342
 
 
 
 
 
 
 
343
344
345
...
455
456
457
 
458
459
460
...
1019
1020
1021
 
1022
1023
1024
...
340
341
342
343
344
345
346
347
348
349
350
351
352
...
462
463
464
465
466
467
468
...
1027
1028
1029
1030
1031
1032
1033
0
@@ -340,6 +340,13 @@ reserve_job(conn c, job j)
0
     conn_insert(&running, c);
0
     j->state = JOB_STATE_RESERVED;
0
     job_insert(&c->reserved_jobs, j);
0
+ if (c->soonest_job == NULL) {
0
+ c->soonest_job = j;
0
+ } else {
0
+ if (j->deadline < c->soonest_job->deadline) {
0
+ c->soonest_job = j;
0
+ }
0
+ }
0
     return reply_job(c, j, MSG_RESERVED);
0
 }
0
 
0
@@ -455,6 +462,7 @@ enqueue_reserved_jobs(conn c)
0
         if (!r) bury_job(j);
0
         global_stat.reserved_ct--;
0
         j->tube->stat.reserved_ct--;
0
+ c->soonest_job = NULL;
0
         if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
0
     }
0
 }
0
@@ -1019,6 +1027,7 @@ remove_this_reserved_job(conn c, job j)
0
         global_stat.reserved_ct--;
0
         j->tube->stat.reserved_ct--;
0
     }
0
+ c->soonest_job = NULL;
0
     if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
0
     return j;
0
 }

Comments

    No one has commented yet.