forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prot.c
224 lines (189 loc) · 4.03 KB
/
prot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* prot.c - protocol implementation */
#include <stdio.h>
#include <string.h>
#include "prot.h"
#include "pq.h"
#include "job.h"
#include "conn.h"
#include "util.h"
#include "reserve.h"
static pq ready_q;
static pq delay_q;
/* Doubly-linked list of waiting connections. */
static struct conn wait_queue = { &wait_queue, &wait_queue, 0 };
static struct job graveyard = { &graveyard, &graveyard, 0 };
static unsigned int buried_ct = 0, urgent_ct = 0, waiting_ct = 0;
static int
waiting_conn_p()
{
return conn_list_any_p(&wait_queue);
}
static int
buried_job_p()
{
return job_list_any_p(&graveyard);
}
void
reply(conn c, char *line, int len, int state)
{
int r;
r = conn_update_evq(c, EV_WRITE | EV_PERSIST);
if (r == -1) return twarnx("conn_update_evq() failed"), conn_close(c);
c->reply = line;
c->reply_len = len;
c->reply_sent = 0;
c->state = state;
}
void
reply_job(conn c, job j, const char *word)
{
int r;
/* tell this connection which job to send */
c->out_job = j;
c->out_job_sent = 0;
r = snprintf(c->reply_buf, LINE_BUF_SIZE, "%s %llu %u %u\r\n",
word, j->id, j->pri, j->body_size - 2);
/* can't happen */
if (r >= LINE_BUF_SIZE) return twarnx("truncated reply"), conn_close(c);
return reply(c, c->reply_buf, strlen(c->reply_buf), STATE_SENDJOB);
}
conn
remove_waiting_conn(conn c)
{
if (!(c->type & CONN_TYPE_WAITING)) return NULL;
c->type &= ~CONN_TYPE_WAITING;
waiting_ct--;
return conn_remove(c);
}
void
process_queue()
{
job j;
while (waiting_conn_p()) {
j = pq_take(ready_q);
if (!j) return;
if (j->pri < URGENT_THRESHOLD) urgent_ct--;
reserve_job(remove_waiting_conn(wait_queue.next), j);
}
}
int
enqueue_job(job j, unsigned int delay)
{
int r;
if (delay) {
r = pq_give(delay_q, j);
if (!r) return 0;
j->state = JOB_STATE_DELAY;
} else {
r = pq_give(ready_q, j);
if (!r) return 0;
j->state = JOB_STATE_READY;
if (j->pri < URGENT_THRESHOLD) urgent_ct++;
}
process_queue();
return 1;
}
void
bury_job(job j)
{
job_insert(&graveyard, j);
buried_ct++;
j->state = JOB_STATE_BURIED;
j->bury_ct++;
}
/* return the number of jobs successfully kicked */
int
kick_job()
{
int r;
job j;
if (!buried_job_p()) return 0;
j = job_remove(graveyard.next);
buried_ct--;
j->kick_ct++;
r = enqueue_job(j, 0);
if (!r) return bury_job(j), 0;
return 1;
}
job
peek_buried_job()
{
return buried_job_p() ? graveyard.next : NULL;
}
static job
find_buried_job(unsigned long long int id)
{
job j;
for (j = graveyard.next; j != &graveyard; j = j->next) {
if (j->id == id) return j;
}
return NULL;
}
void
enqueue_waiting_conn(conn c)
{
waiting_ct++;
c->type |= CONN_TYPE_WAITING;
conn_insert(&wait_queue, conn_remove(c) ? : c);
}
job
peek_job(unsigned long long int id)
{
return pq_find(ready_q, id) ? :
find_reserved_job(id) ? :
find_reserved_job_in_list(&wait_queue, id) ? :
find_buried_job(id);
}
unsigned int
get_ready_job_ct()
{
return pq_used(ready_q);
}
unsigned int
get_delayed_job_ct()
{
return pq_used(delay_q);
}
unsigned int
get_buried_job_ct()
{
return buried_ct;
}
unsigned int
get_urgent_job_ct()
{
return urgent_ct;
}
int
count_cur_waiting()
{
return waiting_ct;
}
static int
job_pri_cmp(job a, job b)
{
if (a->pri == b->pri) {
/* we can't just subtract because id has too many bits */
if (a->id > b->id) return 1;
if (a->id < b->id) return -1;
return 0;
}
return a->pri - b->pri;
}
static int
job_delay_cmp(job a, job b)
{
if (a->delay == b->delay) {
/* we can't just subtract because id has too many bits */
if (a->id > b->id) return 1;
if (a->id < b->id) return -1;
return 0;
}
return a->delay - b->delay;
}
void
prot_init()
{
ready_q = make_pq(HEAP_SIZE, job_pri_cmp);
delay_q = make_pq(HEAP_SIZE, job_delay_cmp);
}