-
Notifications
You must be signed in to change notification settings - Fork 5
/
haricot.lua
448 lines (388 loc) · 11.1 KB
/
haricot.lua
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
-- NOTES:
-- `job` format: {id=...,data=...}
--- low level
local default_cfg = function()
return { max_job_size = 2^16 }
end
local is_posint = function(x)
return ( type(x) == "number" and math.floor(x) == x and x >= 0 )
end
local hyphen = string.byte("-")
local valid_name = function(x)
local n = #x
return (
type(x) == "string" and
n > 0 and n <= 200 and
x:byte() ~= hyphen and
x:match("^[%w-_+/;.$()]+$")
)
end
local luasocket_send = function(s, buf)
return s:send(buf)
end
local luasocket_recv = function(s, bytes)
return s:receive(bytes)
end
local luasocket_getline = function(s)
return s:receive("*l")
end
local luasocket_connect = function(server, port)
local s = (require "socket").tcp()
local ok, err = s:connect(server, port)
if ok then return s else return nil, err end
end
local luasocket_close = function(s)
s:close()
end
local luasocket_t = {
send = luasocket_send,
recv = luasocket_recv,
getline = luasocket_getline,
connect = luasocket_connect,
close = luasocket_close,
}
local lsocket_send = function(s, buf)
local c = #buf
while c > 0 do
local _, wsock = s.lsocket.select(nil, {s.s})
assert(wsock[1] == s.s)
local sent, err = s.s:send(buf)
if not sent then return nil, err end
c = c - sent
end
end
local lsocket_recv = function(s, bytes)
local c, r = bytes, {}
while c > 0 do
local rsock = s.lsocket.select({s.s})
assert(rsock[1] == s.s)
local t = s.s:recv(c)
if not t then return nil end
r[#r+1] = t
c = c - #t
end
return table.concat(r)
end
local lsocket_getline = function(s)
local r = {}
while true do
local c = lsocket_recv(s, 1)
if not c then return nil end
if c == '\n' then return table.concat(r) end
if c ~= '\r' then r[#r+1] = c end
end
end
local lsocket_connect = function(server, port)
local r = {lsocket = (require "lsocket")}
local s, err = r.lsocket.connect("tcp", server, port)
if not s then return nil, err end
local _, wsock = r.lsocket.select(nil, {s})
assert(wsock[1] == s)
r.s = s
s, err = r.s:status()
if not s then return nil, err end
return r
end
local lsocket_close = function(s)
s.s:close()
end
local lsocket_t = {
send = lsocket_send,
recv = lsocket_recv,
getline = lsocket_getline,
connect = lsocket_connect,
close = lsocket_close,
}
local ll_recv = function(self, bytes)
assert(is_posint(bytes))
return self.mod.recv(self.cnx, bytes)
end
local ll_send = function(self, buf)
return self.mod.send(self.cnx, buf)
end
local getline = function(self)
if not self.cnx then return "NOT_CONNECTED" end
return self.mod.getline(self.cnx) or "NOT_CONNECTED"
end
local mkcmd = function(cmd, ...)
return table.concat({cmd, ...}, " ") .. "\r\n"
end
local call = function(self, cmd, ...)
if not self.cnx then return "NOT_CONNECTED" end
ll_send(self, mkcmd(cmd, ...))
return getline(self)
end
local recv = function(self, bytes)
if not self.cnx then return nil end
local r = ll_recv(self, bytes + 2)
if r then
return r:sub(1, bytes)
else return nil end
end
local expect_simple = function(res, s)
if res:match(string.format("^%s$", s)) then
return true
else
return false, res
end
end
local expect_int = function(res, s)
local id = tonumber(res:match(string.format("^%s (%%d+)$", s)))
if id then
return true, id
else
return false, res
end
end
local expect_data = function(self, res)
local bytes = tonumber(res:match("^OK (%d+)$"))
if bytes then
local data = recv(self, bytes)
if data then
assert(#data == bytes)
return true, data
else
return false, "NOT_CONNECTED"
end
else
return false, res
end
end
local expect_job_body = function(self, bytes, id)
local data = recv(self, bytes)
if data then
assert(#data == bytes)
return true, {id = id, data = data}
else
return false, "NOT_CONNECTED"
end
end
--- methods
-- connection
local connect = function(self, server, port)
if self.cnx ~= nil then self:disconnect() end
local err
self.cnx, err = self.mod.connect(server, port)
if not self.cnx then return false, err end
return true
end
local disconnect = function(self)
if self.cnx ~= nil then
self:quit()
self.mod.close(self.cnx)
self.cnx = nil
return true
end
return false, "NOT_CONNECTED"
end
-- producer
local put = function(self, pri, delay, ttr, data)
if not self.cnx then return false, "NOT_CONNECTED" end
assert(
is_posint(pri) and pri < 2^32 and
is_posint(delay) and
is_posint(ttr) and ttr > 0
)
local bytes = #data
assert(bytes < self.cfg.max_job_size)
local cmd = mkcmd("put", pri, delay, ttr, bytes) .. data .. "\r\n"
ll_send(self, cmd)
local res = getline(self)
return expect_int(res, "INSERTED")
end
local use = function(self, tube)
assert(valid_name(tube))
local res = call(self, "use", tube)
local ok = res:match("^USING ([%w-_+/;.$()]+)$")
ok = (ok == tube)
if ok then
return true
else
return false, res
end
end
-- consumer
local reserve = function(self)
local res = call(self, "reserve")
local id, bytes = res:match("^RESERVED (%d+) (%d+)$")
if id --[[and bytes]] then
id, bytes = tonumber(id), tonumber(bytes)
return expect_job_body(self, bytes, id)
else
return false, res
end
end
local reserve_with_timeout = function(self, timeout)
assert(is_posint(timeout))
local res = call(self, "reserve-with-timeout", timeout)
local id, bytes = res:match("^RESERVED (%d+) (%d+)$")
if id --[[and bytes]] then
id, bytes = tonumber(id), tonumber(bytes)
return expect_job_body(self, bytes, id)
else
return expect_simple(res, "TIMED_OUT")
end
end
local delete = function(self, id)
assert(is_posint(id))
local res = call(self, "delete", id)
return expect_simple(res, "DELETED")
end
local release = function(self, id, pri, delay)
assert(
is_posint(id) and
is_posint(pri) and pri < 2^32 and
is_posint(delay)
)
local res = call(self, "release", id, pri, delay)
return expect_simple(res, "RELEASED")
end
local bury = function(self, id, pri)
assert(
is_posint(id) and
is_posint(pri) and pri < 2^32
)
local res = call(self, "bury", id, pri)
return expect_simple(res, "BURIED")
end
local touch = function(self, id)
assert(is_posint(id))
local res = call(self, "touch", id)
return expect_simple(res, "TOUCHED")
end
local watch = function(self, tube)
assert(valid_name(tube))
local res = call(self, "watch", tube)
return expect_int(res, "WATCHING")
end
local ignore = function(self, tube)
assert(valid_name(tube))
local res = call(self, "ignore", tube)
return expect_int(res, "WATCHING")
end
-- other
local _peek_result = function(self, res) -- private
local id, bytes = res:match("^FOUND (%d+) (%d+)$")
if id --[[and bytes]] then
id, bytes = tonumber(id), tonumber(bytes)
return expect_job_body(self, bytes, id)
else
return expect_simple(res, "NOT_FOUND")
end
end
local peek = function(self, id)
assert(is_posint(id))
local res = call(self, "peek", id)
return _peek_result(self, res)
end
local make_peek = function(state)
return function(self)
local res = call(self, string.format("peek-%s", state))
return _peek_result(self, res)
end
end
local kick = function(self, bound)
assert(is_posint(bound))
local res = call(self, "kick", bound)
return expect_int(res, "KICKED")
end
local kick_job = function(self, id)
assert(is_posint(id))
local res = call(self, "kick-job", id)
return expect_simple(res, "KICKED")
end
local stats_job = function(self, id)
assert(is_posint(id))
local res = call(self, "stats-job", id)
return expect_data(self, res)
end
local stats_tube = function(self, tube)
assert(valid_name(tube))
local res = call(self, "stats-tube", tube)
return expect_data(self, res)
end
local stats = function(self)
local res = call(self, "stats")
return expect_data(self, res)
end
local list_tubes = function(self)
local res = call(self, "list-tubes")
return expect_data(self, res)
end
local list_tube_used = function(self)
local res = call(self, "list-tube-used")
local tube = res:match("^USING ([%w-_+/;.$()]+)$")
if tube then
return true, tube
else
return false, res
end
end
local list_tubes_watched = function(self)
local res = call(self, "list-tubes-watched")
return expect_data(self, res)
end
local quit = function(self)
if not self.cnx then return false, "NOT_CONNECTED" end
ll_send(self, mkcmd("quit"))
return true
end
local pause_tube = function(self, tube, delay)
assert(valid_name(tube) and is_posint(delay))
local res = call(self, "pause-tube", tube, delay)
return expect_simple(res, "PAUSED")
end
--- class
local methods = {
-- connection
connect = connect, -- (server,port) -> ok,[err]
disconnect = disconnect, -- () -> ok,[err]
-- producer
put = put, -- (pri,delay,ttr,data) -> ok,[id|err]
use = use, -- (tube) -> ok,[err]
-- consumer
reserve = reserve, -- () -> ok,[job|err]
reserve_with_timeout = reserve_with_timeout, -- () -> ok,[job|nil|err]
delete = delete, -- (id) -> ok,[err]
release = release, -- (id,pri,delay) -> ok,[err]
bury = bury, -- (id,pri) -> ok,[err]
touch = touch, -- (id) -> ok,[err]
watch = watch, -- (tube) -> ok,[count|err]
ignore = ignore, -- (tube) -> ok,[count|err]
-- other
peek = peek, -- (id) -> ok,[job|nil|err]
peek_ready = make_peek("ready"), -- () -> ok,[job|nil|err]
peek_delayed = make_peek("delayed"), -- () -> ok,[job|nil|err]
peek_buried = make_peek("buried"), -- () -> ok,[job|nil|err]
kick = kick, -- (bound) -> ok,[count|err]
kick_job = kick_job, -- (id) -> ok,[err]
stats_job = stats_job, -- (id) -> ok,[yaml|err]
stats_tube = stats_tube, -- (tube) -> ok,[yaml|err]
stats = stats, -- () -> ok,[yaml|err]
list_tubes = list_tubes, -- () -> ok,[yaml|err]
list_tube_used = list_tube_used, -- () -> ok,[tube|err]
list_tubes_watched = list_tubes_watched, -- () -> ok,[tube|err]
quit = quit, -- () -> ok
pause_tube = pause_tube, -- (tube,delay) -> ok,[err]
}
local new = function(server, port, mod)
if not mod then
if pcall(require, "socket") then
mod = luasocket_t
elseif pcall(require, "lsocket") then
mod = lsocket_t
else
error("could not find luasocket or lsocket")
end
end
local r = {mod = mod, cfg = default_cfg()}
local ok, err = connect(r, server, port)
return setmetatable(r, {__index = methods}), ok, err
end
return {
new = new, -- instance,conn_ok,[err]
mod = {
luasocket = luasocket_t,
lsocket = lsocket_t,
},
}