@volter/teams 0.2.34 → 0.2.36

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/client.mjs CHANGED
@@ -62,10 +62,19 @@ import { lineSplitter } from './lines.mjs';
62
62
  */
63
63
  export function harnessTransport(channel) {
64
64
  let stream = null;
65
+ // Each start and close moves the generation on: a stream whose open was overtaken by a close is closed as it arrives,
66
+ // so a start the client gave up on (its start timeout) leaves no harness stream open on the machine.
67
+ let generation = 0;
65
68
  return {
66
69
  get running() { return stream !== null && !stream.closed; },
67
70
  async start(handlers) {
68
- stream = await channel.open('harness');
71
+ const mine = ++generation;
72
+ const opened = await channel.open('harness');
73
+ if (mine !== generation) {
74
+ opened.close();
75
+ throw new Error('harness stream was closed while it opened');
76
+ }
77
+ stream = opened;
69
78
  const lines = lineSplitter();
70
79
  stream.on('data', (chunk) => {
71
80
  for (const line of lines(chunk)) if (line) handlers.onLine(line);
@@ -77,6 +86,7 @@ export function harnessTransport(channel) {
77
86
  stream.write(line.endsWith('\n') ? line : line + '\n');
78
87
  },
79
88
  async close() {
89
+ generation += 1;
80
90
  stream?.close();
81
91
  stream = null;
82
92
  },
package/http-client.mjs CHANGED
@@ -242,7 +242,10 @@ async function sseError(response){let value=null;try{value=await response.json()
242
242
  // 5 s; a refusal that names none waits 30 s. Null for an error that is not a refusal for load.
243
243
  export function retryAfterMs(error,response=null){
244
244
  if(typeof error?.retryAfterMs==='number')return error.retryAfterMs;
245
- if(error?.code!=='rate_limited'&&response?.status!==429&&error?.status!==429)return null;
245
+ // a rate limit, or a 503 that says when to come back (Retry-After: the Teams Worker's own, 15 s when its object is
246
+ // overloaded): a refusal for load, waited out, never re-dialled into on a fixed backoff by every machine at once
247
+ const unavailable=(response?.status===503||error?.status===503)&&response?.headers?.get?.('retry-after');
248
+ if(error?.code!=='rate_limited'&&response?.status!==429&&error?.status!==429&&!unavailable)return null;
246
249
  const named=error?.details?.retry_after_seconds??error?.retry_after_seconds??response?.headers?.get?.('retry-after'),seconds=named==null||named===''?NaN:Number(named);
247
250
  return Math.max(5000,Number.isFinite(seconds)?seconds*1000:30000);
248
251
  }
package/machine-core.mjs CHANGED
@@ -201,6 +201,16 @@ export class MachineCore extends EventEmitter {
201
201
  this.emit('link', { event: 'up', server });
202
202
  } catch (error) { socket.terminate(); dropped(error); }
203
203
  });
204
+ // A refused upgrade (a 503 from the Worker, a 429) says when to come back: its Retry-After is waited, at least,
205
+ // before the next dial, so every machine does not re-dial an overloaded server at 1/2/4/8 s (#1246 review).
206
+ socket.once('unexpected-response', (request, response) => {
207
+ const seconds = Number(response.headers?.['retry-after']);
208
+ if ([429, 503].includes(response.statusCode) && Number.isFinite(seconds) && seconds > 0) this.linkRetryAfter = Math.min(seconds * 1000, LINK_BACKOFF_MAX_MS * 4);
209
+ response.resume();
210
+ request.destroy();
211
+ dropped(new Error(`the server refused the link: HTTP ${response.statusCode}`));
212
+ });
213
+ socket.on('error', () => {}); // after a refused upgrade, its aborted request's error too
204
214
  socket.once('error', dropped);
205
215
  socket.once('close', (_code, reason) => {
206
216
  // The server keeps one link per machine and closes the older one as `replaced`: another
@@ -216,7 +226,8 @@ export class MachineCore extends EventEmitter {
216
226
 
217
227
  #retryLink() {
218
228
  if (this.stopping || !this.link || this.linkTimer || this.linkReplaced) return;
219
- const delay = this.linkBackoff;
229
+ const delay = Math.max(this.linkBackoff, this.linkRetryAfter ?? 0);
230
+ this.linkRetryAfter = null;
220
231
  this.linkBackoff = Math.min(this.linkBackoff * 2, LINK_BACKOFF_MAX_MS);
221
232
  this.linkTimer = setTimeout(() => { this.linkTimer = null; this.#connectLink(); }, delay);
222
233
  this.linkTimer.unref?.();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.2.34",
3
+ "version": "0.2.36",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",