@dxos/network-manager 0.3.9-main.3ced312 → 0.3.9-main.3fcc0fa

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.
@@ -2349,9 +2349,12 @@ import distance from "xor-distance";
2349
2349
  import { invariant as invariant8 } from "@dxos/invariant";
2350
2350
  import { log as log8 } from "@dxos/log";
2351
2351
  var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2352
+ var MIN_UPDATE_INTERVAL = 1e3 * 10;
2353
+ var MAX_CHANGES_PER_UPDATE = 1;
2352
2354
  var MMSTTopology = class {
2353
2355
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2354
2356
  this._sampleCollected = false;
2357
+ this._lastAction = /* @__PURE__ */ new Date(0);
2355
2358
  this._originateConnections = originateConnections;
2356
2359
  this._maxPeers = maxPeers;
2357
2360
  this._sampleSize = sampleSize;
@@ -2359,7 +2362,7 @@ var MMSTTopology = class {
2359
2362
  init(controller) {
2360
2363
  invariant8(!this._controller, "Already initialized", {
2361
2364
  F: __dxlog_file9,
2362
- L: 46,
2365
+ L: 51,
2363
2366
  S: this,
2364
2367
  A: [
2365
2368
  "!this._controller",
@@ -2371,7 +2374,7 @@ var MMSTTopology = class {
2371
2374
  update() {
2372
2375
  invariant8(this._controller, "Not initialized", {
2373
2376
  F: __dxlog_file9,
2374
- L: 51,
2377
+ L: 56,
2375
2378
  S: this,
2376
2379
  A: [
2377
2380
  "this._controller",
@@ -2382,7 +2385,7 @@ var MMSTTopology = class {
2382
2385
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
2383
2386
  log8("Running the algorithm.", void 0, {
2384
2387
  F: __dxlog_file9,
2385
- L: 55,
2388
+ L: 60,
2386
2389
  S: this,
2387
2390
  C: (f, a) => f(...a)
2388
2391
  });
@@ -2393,7 +2396,7 @@ var MMSTTopology = class {
2393
2396
  async onOffer(peer) {
2394
2397
  invariant8(this._controller, "Not initialized", {
2395
2398
  F: __dxlog_file9,
2396
- L: 62,
2399
+ L: 67,
2397
2400
  S: this,
2398
2401
  A: [
2399
2402
  "this._controller",
@@ -2404,7 +2407,7 @@ var MMSTTopology = class {
2404
2407
  const accept = connected.length < this._maxPeers;
2405
2408
  log8(`Offer ${peer} accept=${accept}`, void 0, {
2406
2409
  F: __dxlog_file9,
2407
- L: 65,
2410
+ L: 70,
2408
2411
  S: this,
2409
2412
  C: (f, a) => f(...a)
2410
2413
  });
@@ -2415,7 +2418,7 @@ var MMSTTopology = class {
2415
2418
  _runAlgorithm() {
2416
2419
  invariant8(this._controller, "Not initialized", {
2417
2420
  F: __dxlog_file9,
2418
- L: 74,
2421
+ L: 79,
2419
2422
  S: this,
2420
2423
  A: [
2421
2424
  "this._controller",
@@ -2424,27 +2427,84 @@ var MMSTTopology = class {
2424
2427
  });
2425
2428
  const { connected, candidates, ownPeerId } = this._controller.getState();
2426
2429
  if (connected.length > this._maxPeers) {
2430
+ log8(`disconnect ${connected.length - this._maxPeers} peers.`, void 0, {
2431
+ F: __dxlog_file9,
2432
+ L: 85,
2433
+ S: this,
2434
+ C: (f, a) => f(...a)
2435
+ });
2427
2436
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
2428
- for (const peer of sorted) {
2429
- log8(`Disconnect ${peer}.`, void 0, {
2437
+ invariant8(sorted.length === 0, void 0, {
2438
+ F: __dxlog_file9,
2439
+ L: 89,
2440
+ S: this,
2441
+ A: [
2442
+ "sorted.length === 0",
2443
+ ""
2444
+ ]
2445
+ });
2446
+ if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2447
+ log8(`want to disconnect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2448
+ F: __dxlog_file9,
2449
+ L: 92,
2450
+ S: this,
2451
+ C: (f, a) => f(...a)
2452
+ });
2453
+ }
2454
+ if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2455
+ for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2456
+ log8(`Disconnect ${peer}.`, void 0, {
2457
+ F: __dxlog_file9,
2458
+ L: 97,
2459
+ S: this,
2460
+ C: (f, a) => f(...a)
2461
+ });
2462
+ this._controller.disconnect(peer);
2463
+ }
2464
+ this._lastAction = /* @__PURE__ */ new Date();
2465
+ } else {
2466
+ log8("rate limited discconnect", void 0, {
2430
2467
  F: __dxlog_file9,
2431
- L: 83,
2468
+ L: 102,
2432
2469
  S: this,
2433
2470
  C: (f, a) => f(...a)
2434
2471
  });
2435
- this._controller.disconnect(peer);
2436
2472
  }
2437
2473
  } else if (connected.length < this._originateConnections) {
2474
+ log8(`connect ${this._originateConnections - connected.length} peers.`, void 0, {
2475
+ F: __dxlog_file9,
2476
+ L: 106,
2477
+ S: this,
2478
+ C: (f, a) => f(...a)
2479
+ });
2438
2480
  const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
2439
2481
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
2440
- for (const peer of sorted) {
2441
- log8(`Connect ${peer}.`, void 0, {
2482
+ if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2483
+ log8(`want to connect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2442
2484
  F: __dxlog_file9,
2443
- L: 91,
2485
+ L: 111,
2486
+ S: this,
2487
+ C: (f, a) => f(...a)
2488
+ });
2489
+ }
2490
+ if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2491
+ for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2492
+ log8(`Connect ${peer}.`, void 0, {
2493
+ F: __dxlog_file9,
2494
+ L: 115,
2495
+ S: this,
2496
+ C: (f, a) => f(...a)
2497
+ });
2498
+ this._controller.connect(peer);
2499
+ }
2500
+ this._lastAction = /* @__PURE__ */ new Date();
2501
+ } else {
2502
+ log8("rate limited connect", void 0, {
2503
+ F: __dxlog_file9,
2504
+ L: 120,
2444
2505
  S: this,
2445
2506
  C: (f, a) => f(...a)
2446
2507
  });
2447
- this._controller.connect(peer);
2448
2508
  }
2449
2509
  }
2450
2510
  }
@@ -2978,8 +3038,8 @@ var SimplePeerTransport = class {
2978
3038
  if (!rc) {
2979
3039
  return "unavailable";
2980
3040
  }
2981
- if (rc.relay) {
2982
- return `${rc.ip}:${rc.port}/${rc.protocol} relay for XXX ${rc.candidateType}`;
3041
+ if (rc.candidateType === "relay") {
3042
+ return `${rc.ip}:${rc.port}/${rc.protocol} relay for ${rc.relatedAddress}:${rc.relatedPort}`;
2983
3043
  }
2984
3044
  return `${rc.ip}:${rc.port}/${rc.protocol} ${rc.candidateType}`;
2985
3045
  }
@@ -2993,14 +3053,13 @@ var SimplePeerTransport = class {
2993
3053
  if (this._closed) {
2994
3054
  return;
2995
3055
  }
2996
- this._closed = true;
2997
3056
  this._disconnectStreams();
2998
3057
  this._peer.destroy();
2999
3058
  this._closed = true;
3000
3059
  this.closed.emit();
3001
3060
  log11("closed", void 0, {
3002
3061
  F: __dxlog_file12,
3003
- L: 201,
3062
+ L: 200,
3004
3063
  S: this,
3005
3064
  C: (f, a) => f(...a)
3006
3065
  });
@@ -3877,4 +3936,4 @@ export {
3877
3936
  TcpTransport,
3878
3937
  createTeleportProtocolFactory
3879
3938
  };
3880
- //# sourceMappingURL=chunk-HJ6JLRZM.mjs.map
3939
+ //# sourceMappingURL=chunk-KGZ5C6JO.mjs.map