beam.com 0.1.0-rc.2 → 0.1.0-rc.3

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.
package/runtime/deno.js CHANGED
@@ -315,6 +315,8 @@ const handler = {
315
315
  if (!vm) {
316
316
  const v = vm = new Vm(env, { plain: false, sql, vars, files, secrets, host: new URL(request.url).hostname });
317
317
  v.ready.catch(() => { if (vm === v) vm = undefined; });
318
+ // A VM that stopped: the next request starts a new one.
319
+ v.onDead = () => { if (vm === v) vm = undefined; };
318
320
  }
319
321
  // The scheme of the client (Deno gives it in the URL; Plug.SSL and
320
322
  // force_ssl read x-forwarded-proto), and the address of the client
@@ -44,6 +44,7 @@ export class Beam extends DurableObject {
44
44
  super(ctx, env);
45
45
  this.vm = null;
46
46
  this.expires = undefined;
47
+ this.resetting = false;
47
48
  }
48
49
 
49
50
  // The VM starts at the first request: a path tenant gives its name there.
@@ -55,6 +56,10 @@ export class Beam extends DurableObject {
55
56
  }
56
57
 
57
58
  async fetch(request) {
59
+ if (this.resetting) {
60
+ return new Response('The app stopped. Try again.\n',
61
+ { status: 503, headers: { 'content-type': 'text/plain', 'retry-after': '1' } });
62
+ }
58
63
  if (this.env.BEAM_INSTANCES && this.env.BEAM_TENANTS === 'path') {
59
64
  this.expires ??= (await this.ctx.storage.get(EXPIRES)) ?? null;
60
65
  if (!this.expires || this.expires <= Date.now()) {
@@ -71,11 +76,36 @@ export class Beam extends DurableObject {
71
76
  vars.BEAM_TENANT_PATH = `/t/${tenant}`;
72
77
  }
73
78
  if (this.expires) vars.BEAM_INSTANCE_EXPIRES = String(Math.floor(this.expires / 1000));
74
- this.vm = this.makeVm(vars, new URL(request.url).hostname);
79
+ const vm = this.vm = this.makeVm(vars, new URL(request.url).hostname);
80
+ vm.onDead = () => this.vmStopped(vm);
81
+ vm.ready.catch(() => this.vmStopped(vm));
75
82
  }
76
83
  return this.vm.fetch(request);
77
84
  }
78
85
 
86
+ // The VM stopped (erlang:halt, or a trap such as an allocation that
87
+ // failed), or its boot failed. Its open requests got 503. The memory of
88
+ // WebAssembly does not shrink, so the object resets after these answers:
89
+ // a new instance of the object, with a new VM, takes the next request.
90
+ vmStopped(vm) {
91
+ if (this.vm !== vm) return;
92
+ this.vm = null;
93
+ this.resetting = true;
94
+ setTimeout(() => this.reset('the VM stopped'), 100);
95
+ }
96
+
97
+ // A new instance of the object. When the host cannot abort the object,
98
+ // the next request starts a new VM in this instance: no request stays
99
+ // at 503.
100
+ reset(reason) {
101
+ try {
102
+ this.ctx.abort(reason);
103
+ } catch (e) {
104
+ console.log(`beam: the object did not reset (${e.message}): the next request starts a new VM`);
105
+ this.resetting = false;
106
+ }
107
+ }
108
+
79
109
  // An instance: its time limit (from the registry).
80
110
  async start(expires) {
81
111
  await this.ctx.storage.put(EXPIRES, expires);
@@ -92,7 +122,7 @@ export class Beam extends DurableObject {
92
122
  async expire() {
93
123
  this.expires = null;
94
124
  await this.ctx.storage.deleteAll();
95
- if (this.vm) setTimeout(() => this.ctx.abort('the instance expired'), 0);
125
+ if (this.vm) setTimeout(() => this.reset('the instance expired'), 0);
96
126
  }
97
127
 
98
128
  // The registry (the object ".registry") admits a visitor: a ticket and a
package/runtime/global.js CHANGED
@@ -11,16 +11,19 @@
11
11
  // node wasm/snapshot/snapshot.mjs DIR --warm 4000:/
12
12
  // (cd DIR && wrangler deploy -c wrangler.global.jsonc)
13
13
  import { env } from 'cloudflare:workers';
14
- import { Vm } from './worker.js';
14
+ import plain, { Vm } from './worker.js';
15
15
  import release from './release/release.bin';
16
16
  import snapshot from './release/snapshot.bin';
17
17
 
18
- const vm = new Vm(env, { release, snapshot });
18
+ let vm = new Vm(env, { release, snapshot });
19
19
  await vm.ready;
20
20
  if (env.BEAM_WARM) await vm.warm(env.BEAM_WARM);
21
+ // A VM that stopped: the requests of this isolate then boot a VM of their
22
+ // own (the Worker of worker.js).
23
+ vm.onDead = () => { vm = null; };
21
24
 
22
25
  export default {
23
26
  fetch(request, env, ctx) {
24
- return vm.fetch(request, ctx);
27
+ return vm ? vm.fetch(request, ctx) : plain.fetch(request, env, ctx);
25
28
  },
26
29
  };