volucer-worker 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90e630f897db56c1270cb42f99d874e07343f625481310306926cb19068b4c91
4
- data.tar.gz: 8a822412a40e30e2892ce2a44ed202ef5a5549d9f81b0cb8e44c16c895af577c
3
+ metadata.gz: 8e5589aa71f49ac079278d680ab85596d41ed251328f32bd83643788adf77c6d
4
+ data.tar.gz: 7387af05f1545aa47a0d2493c1718f5150a17dd2b9781d740fdeeb76c850f740
5
5
  SHA512:
6
- metadata.gz: 2093ebaa9b65e4106db42e27605481d8ff46043fe4ebb94233376dde6a03eefac2cc2d86dfa75f4263953e60ece2add4e52ed470cd1266618f0207655da17c5f
7
- data.tar.gz: '030794b6447b7e80e50fe1384599ccc540441972d752160dfc1db9e1cd9050e343a1a1cad8c379430ac1a1660300f9e37bea5d1e932c4b5525f4271d4e3fdfa7'
6
+ metadata.gz: 1bbf75f9b10f97e744c2ad40ceca6bd670d605b73ec3b9f5c150831fbca3ffb7e37ae62a745cc0e8cba7eacb204efe3784c754f9837d9e0e66e3662bdbc41cb7
7
+ data.tar.gz: 0f8317a7c1bd19585faaaf39c527f91f6c8b6ad44b76db771eed9c3590cdc4c63d82adee8bbaf1f17525bfc1f266ca8621bf31fa41cd55a3a5cf3a55a6ada8a4
data/README.md CHANGED
@@ -259,6 +259,23 @@ No wrapper is needed here, unlike Python: a `TCPSocket` already responds to `rea
259
259
  which is what the worker calls on its streams. `exit!` rather than `exit` skips the master's
260
260
  `at_exit` handlers and buffer flushing, which every child would otherwise run again.
261
261
 
262
+ ### Consuming jobs
263
+
264
+ The attach supervisor above serves HTTP with `Worker#run`; it does not consume jobs. A job
265
+ consumer is a separate process started by Volucer through `[jobs.pool]`. Its entry point uses
266
+ `Volucer::Worker.connect.consume { |job, worker| ... }`, not `Volucer::Supervisor.serve`:
267
+
268
+ ```toml
269
+ [jobs]
270
+ consume = ["emails"]
271
+
272
+ [jobs.pool]
273
+ command = "ruby consumer.rb"
274
+ workers = 4
275
+ ```
276
+
277
+ See the Volucer [job-pool configuration](https://github.com/sailantis/volucer/blob/main/docs/08-SERVICES.md#configuring-the-server-side).
278
+
262
279
  ## Rails
263
280
 
264
281
  Rails' `Application` object is already a Rack app, so there is nothing to adapt:
@@ -322,6 +339,18 @@ Two configuration keys are not optional for a non-PHP worker:
322
339
 
323
340
  Everything else in this gem is a transliteration of the specification. These two are not.
324
341
 
342
+ ### The request body may arrive in pieces, and that is a feature
343
+
344
+ This gem advertises `stream_request_body` in its `HELLO_ACK`, so a server that supports it forwards
345
+ the body frame by frame as the client produces it instead of buffering the whole thing first. The
346
+ handler still sees `request.body` as one `String`, so nothing changes on your side — what changes is
347
+ that a 256 MiB upload costs the *server* a few hundred kilobytes rather than 256 MiB.
348
+
349
+ The cost is on this side, and it is unavoidable: Rack's `rack.input` is a file-like object, and this
350
+ SDK hands it `StringIO.new(request.body)`, so the application's own copy is still the full body. The
351
+ saving is the server's, which is the process that was previously holding every concurrent upload at
352
+ once.
353
+
325
354
  ### stdout is the channel, so the SDK takes it
326
355
 
327
356
  The protocol uses the child's stdout as its channel. PHP solves this with an output buffer
@@ -366,7 +395,7 @@ method on a class you control — if you want to feed it `jemalloc`'s numbers or
366
395
  | Frame | Handler surface |
367
396
  | --- | --- |
368
397
  | `REQUEST` / `RESPONSE` / `BODY` / `END` | `Worker#run { }`; the block returns a `Response`, a `String`, a Rack `[status, headers, body]` array, or `nil` |
369
- | `JOB` / `ACK` | `Worker#consume { }`; `nil` acknowledges, `false` requeues, a `String` rejects with a reason, and a raise requeues |
398
+ | `JOB` / `ACK` | `Worker#consume { }`; anything except `false` or a `String` acknowledges, `false` requeues, a `String` rejects with a reason, and a raise requeues |
370
399
  | `CALL` / `RETURN` | `Worker#call(name, params)` from inside a block; `Worker#on_call { }` answers the server's calls. Outside a worker, `Volucer::Rpc` does the same over `[rpc]` |
371
400
  | `LOG` / `METRIC` | `Worker#emit_log`, `Worker#emit_metric` |
372
401
  | `CONTROL` | Answered by the SDK; `drain` and `shutdown` stop the loop |
@@ -11,5 +11,5 @@ module Volucer
11
11
  # `undefined method 'set_encoding' for an instance of Volucer::GuardedStdout`.
12
12
  #
13
13
  # Reading one constant is all a gemspec should ever need from its own library.
14
- VERSION = '0.1.0'
14
+ VERSION = '0.2.0'
15
15
  end
@@ -273,7 +273,13 @@ module Volucer
273
273
  'sapi' => '',
274
274
  'pid' => Process.pid,
275
275
  'runtime' => Volucer.runtime,
276
- 'no_checksum' => offer
276
+ 'no_checksum' => offer,
277
+ # **This SDK can read a body whose length it was not told.** The server only sends
278
+ # a REQUEST without `body_len` to a worker that says so here, because the
279
+ # alternative is worse than a missing feature: an SDK that predates the field reads
280
+ # an absent length as 0, concludes there is nothing to read, and then takes the
281
+ # first BODY frame for its own response.
282
+ 'stream_request_body' => true
277
283
  })
278
284
 
279
285
  @checksum = false if offer
@@ -303,7 +309,7 @@ module Volucer
303
309
  #
304
310
  # What the block returns decides the job's fate:
305
311
  #
306
- # * `nil` — **acknowledge.** Done.
312
+ # * Anything except `false` or a String — **acknowledge.** Done.
307
313
  # * `false` — **requeue.** Not done, and it should be handed out again.
308
314
  # * a String — **reject**, with that string as the reason.
309
315
  #
@@ -485,7 +491,9 @@ module Volucer
485
491
  @keep_alive = true
486
492
 
487
493
  metadata = Volucer.decode_json(payload, 'a REQUEST header')
488
- body = read_body(header.request_id, metadata.fetch('body_len', 0).to_i)
494
+ # `nil` when the server declared no length: the body is streamed and `LAST` ends it. Keeping
495
+ # that distinct from an explicit `0` — a correctly empty body — is what makes it readable.
496
+ body = read_body(header.request_id, metadata['body_len']&.to_i)
489
497
 
490
498
  request = Request.new(
491
499
  method: metadata.fetch('method', 'GET').to_s,
@@ -526,13 +534,21 @@ module Volucer
526
534
  # Read the request body from the BODY frames that follow a REQUEST.
527
535
  #
528
536
  # Stops at `body_len` bytes, because the declared length is authoritative.
537
+ #
538
+ # `body_len` is `nil` when the server did not declare one, which is how it says "this body is
539
+ # streamed and its length is unknown". Then `LAST` is the *only* end signal, so the loop reads
540
+ # until it sees one rather than counting — and an explicit `0` stays what it always was, a
541
+ # correctly empty body.
529
542
  def read_body(request_id, body_len)
530
- return ''.b if body_len.zero?
543
+ return ''.b if body_len == 0
531
544
 
532
545
  body = +''
533
- while body.bytesize < body_len
546
+ while body_len.nil? || body.bytesize < body_len
534
547
  header = read_header
535
548
  if header.nil?
549
+ raise ProtocolError,
550
+ 'the server closed the pipe in the middle of a streamed request body' if body_len.nil?
551
+
536
552
  raise ProtocolError,
537
553
  "the server closed the pipe after #{body.bytesize} of #{body_len} body bytes"
538
554
  end
@@ -552,6 +568,14 @@ module Volucer
552
568
 
553
569
  body << payload
554
570
 
571
+ if body_len.nil?
572
+ # Checked after the bytes are kept, so a final frame that also carried the last bytes of
573
+ # the body is not dropped.
574
+ break if header.last?
575
+
576
+ next
577
+ end
578
+
555
579
  if header.last? && body.bytesize != body_len
556
580
  raise ProtocolError,
557
581
  "the last BODY frame arrived after #{body.bytesize} of #{body_len} declared bytes"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volucer-worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sailantis