kobako 0.31.0 → 0.32.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.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/.githooks/linux-tests.sh +29 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/CHANGELOG.md +125 -0
  5. data/Cargo.lock +457 -494
  6. data/README.md +2 -3
  7. data/ROADMAP.md +0 -1
  8. data/crates/kobako-runtime/CHANGELOG.md +90 -0
  9. data/crates/kobako-runtime/Cargo.toml +14 -15
  10. data/crates/kobako-runtime/README.md +1 -1
  11. data/crates/kobako-runtime/src/cred.rs +32 -20
  12. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  13. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
  14. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  15. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
  16. data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
  17. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
  18. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
  19. data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
  20. data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
  21. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  22. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
  23. data/crates/kobako-runtime/src/filesystem.rs +2 -0
  24. data/crates/kobako-runtime/src/fork.rs +239 -16
  25. data/crates/kobako-runtime/src/limits.rs +17 -41
  26. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  27. data/crates/kobako-runtime/src/locking.rs +508 -113
  28. data/crates/kobako-runtime/src/memcg.rs +83 -160
  29. data/crates/kobako-runtime/src/mount.rs +0 -30
  30. data/crates/kobako-runtime/src/net/connections.rs +53 -13
  31. data/crates/kobako-runtime/src/net/egress.rs +44 -24
  32. data/crates/kobako-runtime/src/net/http.rs +888 -456
  33. data/crates/kobako-runtime/src/net/lookup.rs +150 -50
  34. data/crates/kobako-runtime/src/net/namespace.rs +97 -95
  35. data/crates/kobako-runtime/src/net/reactor.rs +298 -203
  36. data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
  37. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
  38. data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
  39. data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
  40. data/crates/kobako-runtime/src/net/resolv.rs +39 -15
  41. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  42. data/crates/kobako-runtime/src/net/socket.rs +51 -52
  43. data/crates/kobako-runtime/src/net/task.rs +223 -0
  44. data/crates/kobako-runtime/src/net/tcp.rs +410 -158
  45. data/crates/kobako-runtime/src/net/tls.rs +65 -3
  46. data/crates/kobako-runtime/src/net/transport.rs +72 -47
  47. data/crates/kobako-runtime/src/net/udp.rs +231 -129
  48. data/crates/kobako-runtime/src/net.rs +3 -0
  49. data/crates/kobako-runtime/src/sched.rs +244 -58
  50. data/crates/kobako-runtime/src/security.rs +27 -16
  51. data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
  52. data/crates/kobako-runtime/src/vfs/file.rs +52 -53
  53. data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
  54. data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
  55. data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
  56. data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
  57. data/crates/kobako-runtime/src/vfs.rs +1 -0
  58. data/crates/kobako-runtime/src/wait.rs +75 -50
  59. data/crates/kobako-transport/CHANGELOG.md +7 -0
  60. data/crates/kobako-transport/Cargo.toml +1 -1
  61. data/crates/kobako-transport/README.md +1 -1
  62. data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
  63. data/crates/kobako-wasmtime/Cargo.toml +27 -6
  64. data/crates/kobako-wasmtime/README.md +3 -3
  65. data/crates/kobako-wasmtime/src/abi.rs +2 -1
  66. data/crates/kobako-wasmtime/src/ambient.rs +11 -2
  67. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  68. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  69. data/crates/kobako-wasmtime/src/cache.rs +88 -103
  70. data/crates/kobako-wasmtime/src/driver.rs +40 -27
  71. data/crates/kobako-wasmtime/src/invocation.rs +21 -7
  72. data/crates/kobako-wasmtime/src/lib.rs +2 -0
  73. data/crates/kobako-wasmtime/src/limiter.rs +78 -25
  74. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
  75. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
  76. data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
  77. data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
  78. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
  79. data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
  80. data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
  81. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  82. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  83. data/crates/kobako-wasmtime/src/preview1.rs +85 -9
  84. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
  85. data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
  86. data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
  87. data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
  88. data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
  89. data/crates/kobako-wasmtime/src/trap.rs +68 -23
  90. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  91. data/crates/kobako-wasmtime/src/wasi.rs +91 -10
  92. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  93. data/data/kobako.wasm +0 -0
  94. data/ext/kobako/Cargo.toml +6 -2
  95. data/ext/kobako/src/escape.rs +332 -26
  96. data/ext/kobako/src/filesystem/grants.rs +5 -7
  97. data/ext/kobako/src/filesystem/ruby.rs +10 -3
  98. data/ext/kobako/src/filesystem.rs +47 -8
  99. data/ext/kobako/src/fork.rs +72 -0
  100. data/ext/kobako/src/gvl.rs +499 -76
  101. data/ext/kobako/src/handoff.rs +490 -0
  102. data/ext/kobako/src/lib.rs +10 -0
  103. data/ext/kobako/src/network.rs +5 -3
  104. data/ext/kobako/src/pinned_value.rs +28 -15
  105. data/ext/kobako/src/runtime/bridge.rs +32 -25
  106. data/ext/kobako/src/runtime.rs +74 -43
  107. data/lib/kobako/catalog/handles.rb +9 -5
  108. data/lib/kobako/codec/decoder.rb +1 -1
  109. data/lib/kobako/codec/state.rb +2 -18
  110. data/lib/kobako/codec/value_budget.rb +4 -4
  111. data/lib/kobako/codec.rb +5 -6
  112. data/lib/kobako/errors.rb +4 -7
  113. data/lib/kobako/filesystem.rb +1 -1
  114. data/lib/kobako/fork.rb +42 -0
  115. data/lib/kobako/handle.rb +1 -2
  116. data/lib/kobako/sandbox.rb +17 -36
  117. data/lib/kobako/sandbox_options.rb +7 -9
  118. data/lib/kobako/usage.rb +4 -2
  119. data/lib/kobako/version.rb +1 -1
  120. data/lib/kobako.rb +2 -2
  121. data/sig/kobako/catalog/handles.rbs +2 -0
  122. data/sig/kobako/codec/state.rbs +0 -4
  123. data/sig/kobako/codec/value_budget.rbs +1 -1
  124. data/sig/kobako/codec.rbs +2 -2
  125. data/sig/kobako/errors.rbs +0 -3
  126. data/sig/kobako/fork.rbs +17 -0
  127. data/sig/kobako/runtime.rbs +2 -2
  128. data/sig/kobako/sandbox.rbs +3 -12
  129. data/sig/kobako/sandbox_options.rbs +0 -2
  130. metadata +15 -4
  131. data/lib/kobako/pool.rb +0 -162
  132. data/sig/core/thread.rbs +0 -6
  133. data/sig/kobako/pool.rbs +0 -46
@@ -17,21 +17,23 @@ module Kobako
17
17
  # Integer bytes kept from each output channel; 1 MiB.
18
18
  # [+open_file_limit+]
19
19
  # Integer descriptors one invocation may hold open, the
20
- # standard ones and each outermost mount's included; 1024.
20
+ # standard ones and the root's included; 16.
21
21
  # [+open_connection_limit+]
22
22
  # Integer connections one invocation holds open at once, each
23
23
  # socket once it dials, HTTP request, and name lookup in
24
24
  # flight one; 6.
25
25
  # [+connection_limit+]
26
26
  # Integer connections one invocation makes in all, each
27
- # socket's dial and HTTP request one; 50.
27
+ # lookup, dial, and HTTP request one, failed ones too,
28
+ # as a Worker counts its subrequests; 50.
28
29
  # [+profile+] The isolation the runtime must provide at least:
29
30
  # +:hermetic+, which denies the guest the clock and
30
31
  # entropy, or the weaker +:permissive+, which leaves them
31
32
  # live; +:hermetic+.
32
- # [+gvl+] How an invocation holds Ruby's GVL: +:hold+ through
33
- # the invocation, or +:release+ while the guest runs, so
34
- # Sandboxes on different Threads run in parallel; +:hold+.
33
+ # [+gvl+] How an invocation holds Ruby's GVL: +:hold+ while the
34
+ # guest runs, handing it over each quantum, or +:release+,
35
+ # giving it up while the guest runs, so Sandboxes on
36
+ # different Threads run in parallel; +:hold+.
35
37
  # [+network+] What the guest's network dials and looks names up
36
38
  # through, beneath every rule its entries make: +nil+
37
39
  # for the host's own, or a Kobako::Network::Mock a test
@@ -52,9 +54,5 @@ module Kobako
52
54
  open_file_limit:, open_connection_limit:, connection_limit: })
53
55
  super(**caps.merge(profile:, gvl:, network:))
54
56
  end
55
-
56
- def value_budget # :nodoc:
57
- Kobako::Runtime.value_budget_of(memory_limit)
58
- end
59
57
  end
60
58
  end
data/lib/kobako/usage.rb CHANGED
@@ -8,8 +8,10 @@ module Kobako
8
8
  # +timeout+ deadline governs; Service callbacks count, reading the
9
9
  # result and the captures afterwards does not.
10
10
  # * +memory_peak+ — Integer bytes of memory the invocation grew,
11
- # against the same baseline as +memory_limit+; it never exceeds the
12
- # cap, even when the cap was hit.
11
+ # against the same baseline as +memory_limit+. It can pass the cap by
12
+ # what a network queue had to take to move on or to keep a write's
13
+ # promised room, at most one read and one write per open connection,
14
+ # as Linux's +memory.peak+ records a forced charge.
13
15
  #
14
16
  # Filled on every outcome, traps included, so a Host App that rescues a
15
17
  # trap reads from the error's +execution+ how much of the budget the
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kobako
4
- VERSION = "0.31.0"
4
+ VERSION = "0.32.0"
5
5
  end
data/lib/kobako.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # Inside +lib/+, the code sits in tiers: a tier may use the tiers below it
4
4
  # and never one above.
5
5
  #
6
- # Orchestration Sandbox, Pool, Runtime (+ ext), Context
6
+ # Orchestration Sandbox, Runtime (+ ext), Context
7
7
  # |
8
8
  # Catalog setup-time registries + the per-invocation Handle table
9
9
  # |
@@ -54,5 +54,5 @@ require_relative "kobako/extension"
54
54
  require_relative "kobako/transport"
55
55
  require_relative "kobako/catalog"
56
56
  require_relative "kobako/runtime"
57
+ require_relative "kobako/fork"
57
58
  require_relative "kobako/sandbox"
58
- require_relative "kobako/pool"
@@ -1,6 +1,8 @@
1
1
  module Kobako
2
2
  module Catalog
3
3
  class Handles
4
+ MAX_HANDLES: Integer
5
+
4
6
  def initialize: (?next_id: Integer) -> void
5
7
 
6
8
  def alloc: (untyped object) -> Kobako::Handle
@@ -6,13 +6,9 @@ module Kobako
6
6
  def self.current: () -> State
7
7
 
8
8
  @carried_handle: bool
9
- @value_budget: Integer?
10
-
11
- attr_reader value_budget: Integer?
12
9
 
13
10
  def track_handles: [T] { () -> T } -> [T, bool]
14
11
  def record_handle: () -> void
15
- def within_value_budget: [T] (Integer? budget) { () -> T } -> T
16
12
 
17
13
  private
18
14
 
@@ -15,7 +15,7 @@ module Kobako
15
15
 
16
16
  NAN_ENTRY: Integer
17
17
 
18
- def self.assert_within: (String bytes, Integer? budget) -> void
18
+ def self.assert_within: (String bytes, Integer budget) -> void
19
19
 
20
20
  class Count
21
21
  class OpenContainer < Struct[untyped]
data/sig/kobako/codec.rbs CHANGED
@@ -2,6 +2,8 @@ module Kobako
2
2
  module Codec
3
3
  MAX_NESTING_DEPTH: Integer
4
4
 
5
+ MAX_PAYLOAD_VALUES: Integer
6
+
5
7
  # A value in the closed wire type set: the scalar leaves, a Capability Handle, and Array / Hash
6
8
  # containers of the same. The by-value payloads that cross the wire —
7
9
  # dispatch arguments and success values — inhabit this type; a Fault is
@@ -13,8 +15,6 @@ module Kobako
13
15
 
14
16
  def self.track_handles: [T] { () -> T } -> [T, bool]
15
17
 
16
- def self.within_value_budget: [T] (Integer? budget) { () -> T } -> T
17
-
18
18
  # The Handle allocator/resolver a wire walk works against: alloc
19
19
  # registers a non-wire-representable object and returns its Handle,
20
20
  # fetch resolves a wire Handle id back to the live object. The
@@ -85,7 +85,4 @@ module Kobako
85
85
  ?backtrace_lines: Array[String]?
86
86
  ) -> void
87
87
  end
88
-
89
- class PoolTimeoutError < Error
90
- end
91
88
  end
@@ -0,0 +1,17 @@
1
+ module Kobako
2
+ module Fork
3
+ module Hook
4
+ def _fork: () -> Integer
5
+
6
+ def daemon: (?boolish nochdir, ?boolish noclose) -> 0
7
+ end
8
+
9
+ def self.around: [T] () { () -> T } -> T
10
+
11
+ def self.install: () -> void
12
+
13
+ def self.prepare: () -> void
14
+
15
+ def self.release: () -> void
16
+ end
17
+ end
@@ -27,8 +27,6 @@ module Kobako
27
27
  connection_limit: Integer?
28
28
  }
29
29
 
30
- def self.value_budget_of: (Integer? memory_limit) -> Integer?
31
-
32
30
  def self.from_path: (
33
31
  String path,
34
32
  timeout: (Float | Integer)?,
@@ -64,5 +62,7 @@ module Kobako
64
62
  def profile: () -> Symbol
65
63
 
66
64
  def compile: (Kobako::Context::preamble preamble) -> Array[String]?
65
+
66
+ def stat: () -> Hash[Symbol, Integer]
67
67
  end
68
68
  end
@@ -42,6 +42,9 @@ module Kobako
42
42
 
43
43
  def eval: (String code) ?{ (Kobako::Context) -> void } -> Kobako::Execution
44
44
 
45
+ def stat: () -> Hash[Symbol, Integer]
46
+ | (Symbol key) -> Integer
47
+
45
48
  private
46
49
 
47
50
  def build_runtime: () -> Kobako::Runtime
@@ -50,18 +53,6 @@ module Kobako
50
53
 
51
54
  def refuse_once_invoked: (String verb) -> void
52
55
 
53
- def seal: (String reason) -> String
54
-
55
56
  def begin_invocation: () -> Kobako::Context
56
-
57
- def warn_release_on_main_thread: () -> void
58
-
59
- module MainThreadRelease
60
- MESSAGE: String
61
-
62
- self.@warned: bool
63
-
64
- def self.warn_once: () -> void
65
- end
66
57
  end
67
58
  end
@@ -53,7 +53,5 @@ module Kobako
53
53
  gvl: Symbol,
54
54
  network: Network::Mock?
55
55
  }
56
-
57
- def value_budget: () -> Integer?
58
56
  end
59
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kobako
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.31.0
4
+ version: 0.32.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aotokitsuruya
@@ -53,6 +53,7 @@ extensions:
53
53
  - ext/kobako/extconf.rb
54
54
  extra_rdoc_files: []
55
55
  files:
56
+ - ".githooks/linux-tests.sh"
56
57
  - ".release-please-manifest.json"
57
58
  - CHANGELOG.md
58
59
  - Cargo.lock
@@ -77,6 +78,7 @@ files:
77
78
  - crates/kobako-runtime/src/filesystem/bind.rs
78
79
  - crates/kobako-runtime/src/filesystem/bind/fid.rs
79
80
  - crates/kobako-runtime/src/filesystem/bind/lookup.rs
81
+ - crates/kobako-runtime/src/filesystem/bind/unicode.rs
80
82
  - crates/kobako-runtime/src/filesystem/bind/walk.rs
81
83
  - crates/kobako-runtime/src/filesystem/overlay.rs
82
84
  - crates/kobako-runtime/src/filesystem/overlay/file.rs
@@ -89,6 +91,7 @@ files:
89
91
  - crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs
90
92
  - crates/kobako-runtime/src/filesystem/pipe.rs
91
93
  - crates/kobako-runtime/src/filesystem/rdir.rs
94
+ - crates/kobako-runtime/src/filesystem/testing.rs
92
95
  - crates/kobako-runtime/src/filesystem/tmpfs.rs
93
96
  - crates/kobako-runtime/src/filesystem/tmpfs/seed.rs
94
97
  - crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs
@@ -120,7 +123,9 @@ files:
120
123
  - crates/kobako-runtime/src/net/resolv/stub.rs
121
124
  - crates/kobako-runtime/src/net/resolv/stub/tests.rs
122
125
  - crates/kobako-runtime/src/net/resolv/windows.rs
126
+ - crates/kobako-runtime/src/net/skbuff.rs
123
127
  - crates/kobako-runtime/src/net/socket.rs
128
+ - crates/kobako-runtime/src/net/task.rs
124
129
  - crates/kobako-runtime/src/net/tcp.rs
125
130
  - crates/kobako-runtime/src/net/tls.rs
126
131
  - crates/kobako-runtime/src/net/transport.rs
@@ -182,6 +187,8 @@ files:
182
187
  - crates/kobako-wasmtime/src/abi.rs
183
188
  - crates/kobako-wasmtime/src/ambient.rs
184
189
  - crates/kobako-wasmtime/src/cache.rs
190
+ - crates/kobako-wasmtime/src/cache/unix.rs
191
+ - crates/kobako-wasmtime/src/cache/windows.rs
185
192
  - crates/kobako-wasmtime/src/config.rs
186
193
  - crates/kobako-wasmtime/src/driver.rs
187
194
  - crates/kobako-wasmtime/src/exports.rs
@@ -203,7 +210,9 @@ files:
203
210
  - crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs
204
211
  - crates/kobako-wasmtime/src/preview1/path.rs
205
212
  - crates/kobako-wasmtime/src/preview1/poll.rs
213
+ - crates/kobako-wasmtime/src/preview1/random.rs
206
214
  - crates/kobako-wasmtime/src/preview1/sock.rs
215
+ - crates/kobako-wasmtime/src/preview1/testing.rs
207
216
  - crates/kobako-wasmtime/src/preview1/testsuite-expected.txt
208
217
  - crates/kobako-wasmtime/src/preview1/testsuite.rs
209
218
  - crates/kobako-wasmtime/src/preview2.rs
@@ -216,6 +225,7 @@ files:
216
225
  - crates/kobako-wasmtime/src/preview2/sockets.rs
217
226
  - crates/kobako-wasmtime/src/preview2/table.rs
218
227
  - crates/kobako-wasmtime/src/trap.rs
228
+ - crates/kobako-wasmtime/src/waits.rs
219
229
  - crates/kobako-wasmtime/src/wasi.rs
220
230
  - crates/kobako-wasmtime/wit/deps/cli.wit
221
231
  - crates/kobako-wasmtime/wit/deps/clocks.wit
@@ -234,7 +244,9 @@ files:
234
244
  - ext/kobako/src/filesystem.rs
235
245
  - ext/kobako/src/filesystem/grants.rs
236
246
  - ext/kobako/src/filesystem/ruby.rs
247
+ - ext/kobako/src/fork.rs
237
248
  - ext/kobako/src/gvl.rs
249
+ - ext/kobako/src/handoff.rs
238
250
  - ext/kobako/src/lib.rs
239
251
  - ext/kobako/src/network.rs
240
252
  - ext/kobako/src/network/mock.rs
@@ -270,13 +282,13 @@ files:
270
282
  - lib/kobako/execution.rb
271
283
  - lib/kobako/extension.rb
272
284
  - lib/kobako/filesystem.rb
285
+ - lib/kobako/fork.rb
273
286
  - lib/kobako/handle.rb
274
287
  - lib/kobako/names.rb
275
288
  - lib/kobako/network.rb
276
289
  - lib/kobako/outcome.rb
277
290
  - lib/kobako/payload.rb
278
291
  - lib/kobako/payload/arguments.rb
279
- - lib/kobako/pool.rb
280
292
  - lib/kobako/runtime.rb
281
293
  - lib/kobako/sandbox.rb
282
294
  - lib/kobako/sandbox_options.rb
@@ -297,7 +309,6 @@ files:
297
309
  - lib/kobako/usage.rb
298
310
  - lib/kobako/version.rb
299
311
  - release-please-config.json
300
- - sig/core/thread.rbs
301
312
  - sig/kobako.rbs
302
313
  - sig/kobako/capture.rbs
303
314
  - sig/kobako/catalog.rbs
@@ -325,13 +336,13 @@ files:
325
336
  - sig/kobako/execution.rbs
326
337
  - sig/kobako/extension.rbs
327
338
  - sig/kobako/filesystem.rbs
339
+ - sig/kobako/fork.rbs
328
340
  - sig/kobako/guest_yielder.rbs
329
341
  - sig/kobako/handle.rbs
330
342
  - sig/kobako/network.rbs
331
343
  - sig/kobako/outcome.rbs
332
344
  - sig/kobako/payload.rbs
333
345
  - sig/kobako/payload/arguments.rbs
334
- - sig/kobako/pool.rbs
335
346
  - sig/kobako/runtime.rbs
336
347
  - sig/kobako/sandbox.rbs
337
348
  - sig/kobako/sandbox_options.rbs
data/lib/kobako/pool.rb DELETED
@@ -1,162 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "errors"
4
- require_relative "sandbox"
5
-
6
- module Kobako
7
- # A bounded set of warm, identically set-up Sandboxes, handed out to one
8
- # holder at a time. Every Sandbox it builds is kept: an invocation leaves
9
- # nothing behind, whatever ended it.
10
- #
11
- # Deprecated: the next release removes Pool. A Sandbox keeps nothing
12
- # between runs, so Threads share one Sandbox instead.
13
- class Pool
14
- # The default +checkout_timeout+: 5 seconds.
15
- DEFAULT_CHECKOUT_TIMEOUT_SECONDS = 5.0
16
- # What building a Pool warns while deprecation warnings are shown.
17
- DEPRECATION = "Kobako::Pool is deprecated, removed next release; share a Kobako::Sandbox across Threads"
18
- private_constant :DEPRECATION
19
-
20
- # Build a Pool of up to +size+ Sandboxes, built on first demand with the
21
- # other keywords as Sandbox.new takes them. +checkout_timeout+ bounds how
22
- # many seconds #with waits, and +nil+ waits indefinitely. The block, if
23
- # given, sets up each Sandbox once, as with Sandbox#bind and
24
- # Sandbox#preload.
25
- #
26
- # Raises TypeError when +size+ is not an Integer or +checkout_timeout+
27
- # is not Numeric, and ArgumentError when either is not positive or
28
- # +checkout_timeout+ is not finite.
29
- def initialize(size:, checkout_timeout: DEFAULT_CHECKOUT_TIMEOUT_SECONDS, **sandbox_options, &setup)
30
- warn(DEPRECATION, category: :deprecated, uplevel: 1)
31
- validate_size(size)
32
- @size = size
33
- @checkout_timeout = normalize_checkout_timeout(checkout_timeout)
34
- @sandbox_options = sandbox_options
35
- @setup = setup
36
- @idle = [] # : Array[Kobako::Sandbox]
37
- @constructed = 0
38
- @mutex = Mutex.new
39
- @slot_freed = ConditionVariable.new
40
- end
41
-
42
- # Yield one exclusively-held Sandbox to the block and return the
43
- # block's value; the Sandbox returns to the pool however the block
44
- # exits. Raises PoolTimeoutError once every slot has stayed held past
45
- # +checkout_timeout+.
46
- #
47
- # An interrupt reaches the checkout only while it waits or builds, and
48
- # the block as it runs, as connection_pool's +with+ does, so none lands
49
- # between taking a Sandbox and handing it back.
50
- def with
51
- Thread.handle_interrupt(Exception => :never) do
52
- sandbox = acquire
53
- begin
54
- Thread.handle_interrupt(Exception => :immediate) { yield sandbox }
55
- ensure
56
- checkin(sandbox)
57
- end
58
- end
59
- end
60
-
61
- private
62
-
63
- def acquire
64
- timeout = @checkout_timeout
65
- deadline = timeout && (monotonic_now + timeout)
66
- loop do
67
- action, sandbox = claim_or_wait(deadline)
68
- return sandbox if action == :idle && sandbox
69
- return construct_slot if action == :build
70
- end
71
- end
72
-
73
- # Waiting happens inside the lock (so a checkin can wake it);
74
- # construction happens outside (so a slow setup block never holds the
75
- # lock) — capacity is reserved here and released by +construct_slot+
76
- # on failure.
77
- def claim_or_wait(deadline)
78
- @mutex.synchronize do
79
- return [:idle, @idle.pop] unless @idle.empty?
80
-
81
- if @constructed < @size
82
- @constructed += 1
83
- return [:build, nil]
84
- end
85
-
86
- await_slot(deadline)
87
- [:retry, nil]
88
- end
89
- end
90
-
91
- # Must run while holding +@mutex+.
92
- def await_slot(deadline)
93
- remaining = deadline && (deadline - monotonic_now)
94
- if remaining && remaining <= 0
95
- raise PoolTimeoutError,
96
- "no Sandbox returned within #{@checkout_timeout}s: all #{@size} Sandboxes are held"
97
- end
98
-
99
- Thread.handle_interrupt(Exception => :immediate) { @slot_freed.wait(@mutex, remaining) }
100
- end
101
-
102
- # A failed construction releases its reserved capacity so a later
103
- # checkout can retry. Setup closes registration, so each holder gets
104
- # the Sandbox as the setup block left it.
105
- def construct_slot
106
- done = false
107
- sandbox = Thread.handle_interrupt(Exception => :immediate) { build_slot }
108
- done = true
109
- sandbox
110
- ensure
111
- release_capacity unless done
112
- end
113
-
114
- def build_slot
115
- sandbox = Sandbox.new(**@sandbox_options)
116
- @setup&.call(sandbox)
117
- sandbox.send(:seal, "on a pooled Sandbox once its setup ran")
118
- sandbox
119
- end
120
-
121
- def checkin(sandbox)
122
- @mutex.synchronize do
123
- @idle.push(sandbox)
124
- @slot_freed.broadcast
125
- end
126
- end
127
-
128
- def release_capacity
129
- @mutex.synchronize do
130
- @constructed -= 1
131
- @slot_freed.broadcast
132
- end
133
- end
134
-
135
- # The wait deadline runs on the monotonic clock so a wall-clock jump
136
- # cannot stretch or cut the checkout wait.
137
- def monotonic_now
138
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
139
- end
140
-
141
- def validate_size(size)
142
- raise TypeError, "size must be an Integer, got #{size.class}" unless size.is_a?(Integer)
143
- return if size.positive?
144
-
145
- raise ArgumentError, "size must be a positive Integer, got #{size.inspect}"
146
- end
147
-
148
- def normalize_checkout_timeout(checkout_timeout)
149
- return nil if checkout_timeout.nil?
150
- unless checkout_timeout.is_a?(Numeric)
151
- raise TypeError, "checkout_timeout must be Numeric or nil, got #{checkout_timeout.inspect}"
152
- end
153
-
154
- seconds = checkout_timeout.to_f
155
- unless seconds.positive? && seconds.finite?
156
- raise ArgumentError, "checkout_timeout must be > 0 and finite (got #{checkout_timeout})"
157
- end
158
-
159
- seconds
160
- end
161
- end
162
- end
data/sig/core/thread.rbs DELETED
@@ -1,6 +0,0 @@
1
- # The block form rbs 4.2.0 leaves out. ruby/rbs types it after that release
2
- # (7c396890); this goes once a release carrying it is the floor.
3
- class Thread
4
- def self.handle_interrupt: [T] (Hash[Class, :immediate | :on_blocking | :never] hash) { (nil) -> T } -> T
5
- | ...
6
- end
data/sig/kobako/pool.rbs DELETED
@@ -1,46 +0,0 @@
1
- module Kobako
2
- class Pool
3
- DEFAULT_CHECKOUT_TIMEOUT_SECONDS: Float
4
-
5
- DEPRECATION: String
6
-
7
- @size: Integer
8
- @checkout_timeout: Float?
9
- @sandbox_options: Hash[Symbol, untyped]
10
- @setup: ^(Kobako::Sandbox) -> void | nil
11
- @idle: Array[Kobako::Sandbox]
12
- @constructed: Integer
13
- @mutex: Thread::Mutex
14
- @slot_freed: Thread::ConditionVariable
15
-
16
- def initialize: (
17
- size: Integer,
18
- ?checkout_timeout: (Float | Integer)?,
19
- **untyped sandbox_options
20
- ) ?{ (Kobako::Sandbox) -> void } -> void
21
-
22
- def with: [T] () { (Kobako::Sandbox) -> T } -> T
23
-
24
- private
25
-
26
- def acquire: () -> Kobako::Sandbox
27
-
28
- def claim_or_wait: (Float? deadline) -> [Symbol, Kobako::Sandbox?]
29
-
30
- def await_slot: (Float? deadline) -> void
31
-
32
- def construct_slot: () -> Kobako::Sandbox
33
-
34
- def build_slot: () -> Kobako::Sandbox
35
-
36
- def checkin: (Kobako::Sandbox sandbox) -> void
37
-
38
- def release_capacity: () -> void
39
-
40
- def monotonic_now: () -> Float
41
-
42
- def validate_size: (untyped size) -> void
43
-
44
- def normalize_checkout_timeout: ((Float | Integer)? checkout_timeout) -> Float?
45
- end
46
- end