@crawlee/core 4.0.0-beta.12 → 4.0.0-beta.121

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 (279) hide show
  1. package/README.md +17 -13
  2. package/autoscaling/autoscaled_pool.d.ts +67 -172
  3. package/autoscaling/autoscaled_pool.js +165 -320
  4. package/autoscaling/client_load_signal.d.ts +55 -0
  5. package/autoscaling/client_load_signal.js +73 -0
  6. package/autoscaling/concurrency_system.d.ts +268 -0
  7. package/autoscaling/concurrency_system.js +351 -0
  8. package/autoscaling/cpu_load_signal.d.ts +43 -0
  9. package/autoscaling/cpu_load_signal.js +47 -0
  10. package/autoscaling/event_loop_load_signal.d.ts +51 -0
  11. package/autoscaling/event_loop_load_signal.js +60 -0
  12. package/autoscaling/index.d.ts +6 -1
  13. package/autoscaling/index.js +6 -1
  14. package/autoscaling/load_signal.d.ts +99 -0
  15. package/autoscaling/load_signal.js +104 -0
  16. package/autoscaling/memory_load_signal.d.ts +47 -0
  17. package/autoscaling/memory_load_signal.js +106 -0
  18. package/autoscaling/snapshotter.d.ts +58 -163
  19. package/autoscaling/snapshotter.js +45 -263
  20. package/autoscaling/system_status.d.ts +62 -84
  21. package/autoscaling/system_status.js +92 -122
  22. package/autoscaling/weighted_avg.d.ts +5 -0
  23. package/autoscaling/weighted_avg.js +14 -0
  24. package/byte_utils.d.ts +17 -0
  25. package/byte_utils.js +42 -0
  26. package/configuration.d.ts +96 -223
  27. package/configuration.js +170 -222
  28. package/cookie_utils.d.ts +4 -3
  29. package/cookie_utils.js +22 -13
  30. package/crawlers/context_pipeline.d.ts +10 -1
  31. package/crawlers/context_pipeline.js +31 -8
  32. package/crawlers/crawler_commons.d.ts +90 -83
  33. package/crawlers/crawler_commons.js +1 -116
  34. package/crawlers/error_snapshotter.d.ts +2 -5
  35. package/crawlers/error_snapshotter.js +7 -8
  36. package/crawlers/error_tracker.d.ts +0 -1
  37. package/crawlers/error_tracker.js +0 -1
  38. package/crawlers/index.d.ts +1 -3
  39. package/crawlers/index.js +0 -3
  40. package/crawlers/internals/types.d.ts +0 -1
  41. package/crawlers/internals/types.js +0 -1
  42. package/crawlers/statistics.d.ts +143 -59
  43. package/crawlers/statistics.js +243 -153
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +59 -68
  47. package/enqueue_links/enqueue_links.js +57 -62
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +40 -27
  51. package/enqueue_links/shared.js +90 -68
  52. package/errors.d.ts +72 -4
  53. package/errors.js +89 -5
  54. package/events/event_manager.d.ts +35 -9
  55. package/events/event_manager.js +10 -12
  56. package/events/index.d.ts +0 -1
  57. package/events/index.js +0 -1
  58. package/events/local_event_manager.d.ts +15 -3
  59. package/events/local_event_manager.js +39 -13
  60. package/http.d.ts +9 -0
  61. package/http.js +28 -0
  62. package/index.d.ts +7 -4
  63. package/index.js +6 -3
  64. package/iterables.d.ts +79 -0
  65. package/iterables.js +134 -0
  66. package/log.d.ts +82 -3
  67. package/log.js +106 -1
  68. package/memory-storage/consts.d.ts +4 -0
  69. package/memory-storage/consts.js +4 -0
  70. package/memory-storage/index.d.ts +1 -0
  71. package/memory-storage/index.js +1 -0
  72. package/memory-storage/memory-storage.d.ts +38 -0
  73. package/memory-storage/memory-storage.js +130 -0
  74. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  75. package/memory-storage/resource-clients/common/base-client.js +6 -0
  76. package/memory-storage/resource-clients/dataset.d.ts +40 -0
  77. package/memory-storage/resource-clients/dataset.js +114 -0
  78. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  79. package/memory-storage/resource-clients/key-value-store.js +204 -0
  80. package/memory-storage/resource-clients/request-queue.d.ts +77 -0
  81. package/memory-storage/resource-clients/request-queue.js +422 -0
  82. package/memory-storage/utils.d.ts +16 -0
  83. package/memory-storage/utils.js +41 -0
  84. package/owned_or_injected.d.ts +58 -0
  85. package/owned_or_injected.js +98 -0
  86. package/package.json +13 -12
  87. package/proxy_configuration.d.ts +24 -132
  88. package/proxy_configuration.js +24 -143
  89. package/recoverable_state.d.ts +140 -0
  90. package/recoverable_state.js +212 -0
  91. package/request.d.ts +86 -17
  92. package/request.js +120 -41
  93. package/router.d.ts +193 -21
  94. package/router.js +188 -43
  95. package/serialization.d.ts +0 -1
  96. package/serialization.js +9 -11
  97. package/service_locator.d.ts +165 -0
  98. package/service_locator.js +253 -0
  99. package/session_pool/consts.d.ts +1 -2
  100. package/session_pool/consts.js +1 -2
  101. package/session_pool/errors.d.ts +0 -1
  102. package/session_pool/errors.js +0 -1
  103. package/session_pool/fingerprint.d.ts +9 -0
  104. package/session_pool/fingerprint.js +30 -0
  105. package/session_pool/index.d.ts +0 -2
  106. package/session_pool/index.js +0 -2
  107. package/session_pool/session.d.ts +35 -89
  108. package/session_pool/session.js +82 -142
  109. package/session_pool/session_pool.d.ts +69 -90
  110. package/session_pool/session_pool.js +151 -150
  111. package/storages/batched_adds.d.ts +37 -0
  112. package/storages/batched_adds.js +73 -0
  113. package/storages/dataset.d.ts +114 -54
  114. package/storages/dataset.js +285 -144
  115. package/storages/index.d.ts +10 -8
  116. package/storages/index.js +8 -8
  117. package/storages/key_value_store.d.ts +185 -42
  118. package/storages/key_value_store.js +424 -151
  119. package/storages/key_value_store_codec.d.ts +32 -0
  120. package/storages/key_value_store_codec.js +113 -0
  121. package/storages/request_dedup_cache.d.ts +22 -0
  122. package/storages/request_dedup_cache.js +48 -0
  123. package/storages/request_list.d.ts +52 -116
  124. package/storages/request_list.js +159 -133
  125. package/storages/request_loader.d.ts +101 -0
  126. package/storages/request_loader.js +1 -0
  127. package/storages/request_manager.d.ts +33 -0
  128. package/storages/request_manager.js +1 -0
  129. package/storages/request_manager_tandem.d.ts +97 -0
  130. package/storages/request_manager_tandem.js +197 -0
  131. package/storages/request_queue.d.ts +290 -47
  132. package/storages/request_queue.js +757 -216
  133. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
  134. package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +137 -143
  135. package/storages/storage_instance_manager.d.ts +87 -0
  136. package/storages/storage_instance_manager.js +256 -0
  137. package/storages/storage_stats.d.ts +48 -0
  138. package/storages/storage_stats.js +29 -0
  139. package/storages/throttling_request_manager.d.ts +216 -0
  140. package/storages/throttling_request_manager.js +453 -0
  141. package/storages/transaction.d.ts +252 -0
  142. package/storages/transaction.js +251 -0
  143. package/storages/utils.d.ts +58 -11
  144. package/storages/utils.js +64 -13
  145. package/system-info/cpu-info.d.ts +67 -0
  146. package/system-info/cpu-info.js +216 -0
  147. package/system-info/memory-info.d.ts +31 -0
  148. package/system-info/memory-info.js +115 -0
  149. package/system-info/ps-tree.d.ts +17 -0
  150. package/system-info/ps-tree.js +144 -0
  151. package/system-info/runtime.d.ts +14 -0
  152. package/system-info/runtime.js +80 -0
  153. package/typedefs.d.ts +0 -6
  154. package/typedefs.js +0 -1
  155. package/url.d.ts +9 -0
  156. package/url.js +11 -0
  157. package/validators.d.ts +8 -1
  158. package/validators.js +10 -3
  159. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  160. package/autoscaling/autoscaled_pool.js.map +0 -1
  161. package/autoscaling/index.d.ts.map +0 -1
  162. package/autoscaling/index.js.map +0 -1
  163. package/autoscaling/snapshotter.d.ts.map +0 -1
  164. package/autoscaling/snapshotter.js.map +0 -1
  165. package/autoscaling/system_status.d.ts.map +0 -1
  166. package/autoscaling/system_status.js.map +0 -1
  167. package/configuration.d.ts.map +0 -1
  168. package/configuration.js.map +0 -1
  169. package/cookie_utils.d.ts.map +0 -1
  170. package/cookie_utils.js.map +0 -1
  171. package/crawlers/context_pipeline.d.ts.map +0 -1
  172. package/crawlers/context_pipeline.js.map +0 -1
  173. package/crawlers/crawler_commons.d.ts.map +0 -1
  174. package/crawlers/crawler_commons.js.map +0 -1
  175. package/crawlers/crawler_utils.d.ts +0 -10
  176. package/crawlers/crawler_utils.d.ts.map +0 -1
  177. package/crawlers/crawler_utils.js +0 -12
  178. package/crawlers/crawler_utils.js.map +0 -1
  179. package/crawlers/error_snapshotter.d.ts.map +0 -1
  180. package/crawlers/error_snapshotter.js.map +0 -1
  181. package/crawlers/error_tracker.d.ts.map +0 -1
  182. package/crawlers/error_tracker.js.map +0 -1
  183. package/crawlers/index.d.ts.map +0 -1
  184. package/crawlers/index.js.map +0 -1
  185. package/crawlers/internals/types.d.ts.map +0 -1
  186. package/crawlers/internals/types.js.map +0 -1
  187. package/crawlers/statistics.d.ts.map +0 -1
  188. package/crawlers/statistics.js.map +0 -1
  189. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  190. package/enqueue_links/enqueue_links.js.map +0 -1
  191. package/enqueue_links/index.d.ts.map +0 -1
  192. package/enqueue_links/index.js.map +0 -1
  193. package/enqueue_links/shared.d.ts.map +0 -1
  194. package/enqueue_links/shared.js.map +0 -1
  195. package/errors.d.ts.map +0 -1
  196. package/errors.js.map +0 -1
  197. package/events/event_manager.d.ts.map +0 -1
  198. package/events/event_manager.js.map +0 -1
  199. package/events/index.d.ts.map +0 -1
  200. package/events/index.js.map +0 -1
  201. package/events/local_event_manager.d.ts.map +0 -1
  202. package/events/local_event_manager.js.map +0 -1
  203. package/http_clients/base-http-client.d.ts +0 -134
  204. package/http_clients/base-http-client.d.ts.map +0 -1
  205. package/http_clients/base-http-client.js +0 -33
  206. package/http_clients/base-http-client.js.map +0 -1
  207. package/http_clients/form-data-like.d.ts +0 -67
  208. package/http_clients/form-data-like.d.ts.map +0 -1
  209. package/http_clients/form-data-like.js +0 -5
  210. package/http_clients/form-data-like.js.map +0 -1
  211. package/http_clients/got-scraping-http-client.d.ts +0 -15
  212. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  213. package/http_clients/got-scraping-http-client.js +0 -69
  214. package/http_clients/got-scraping-http-client.js.map +0 -1
  215. package/http_clients/index.d.ts +0 -3
  216. package/http_clients/index.d.ts.map +0 -1
  217. package/http_clients/index.js +0 -3
  218. package/http_clients/index.js.map +0 -1
  219. package/index.d.ts.map +0 -1
  220. package/index.js.map +0 -1
  221. package/log.d.ts.map +0 -1
  222. package/log.js.map +0 -1
  223. package/proxy_configuration.d.ts.map +0 -1
  224. package/proxy_configuration.js.map +0 -1
  225. package/request.d.ts.map +0 -1
  226. package/request.js.map +0 -1
  227. package/router.d.ts.map +0 -1
  228. package/router.js.map +0 -1
  229. package/serialization.d.ts.map +0 -1
  230. package/serialization.js.map +0 -1
  231. package/session_pool/consts.d.ts.map +0 -1
  232. package/session_pool/consts.js.map +0 -1
  233. package/session_pool/errors.d.ts.map +0 -1
  234. package/session_pool/errors.js.map +0 -1
  235. package/session_pool/events.d.ts +0 -3
  236. package/session_pool/events.d.ts.map +0 -1
  237. package/session_pool/events.js +0 -3
  238. package/session_pool/events.js.map +0 -1
  239. package/session_pool/index.d.ts.map +0 -1
  240. package/session_pool/index.js.map +0 -1
  241. package/session_pool/session.d.ts.map +0 -1
  242. package/session_pool/session.js.map +0 -1
  243. package/session_pool/session_pool.d.ts.map +0 -1
  244. package/session_pool/session_pool.js.map +0 -1
  245. package/storages/access_checking.d.ts +0 -13
  246. package/storages/access_checking.d.ts.map +0 -1
  247. package/storages/access_checking.js +0 -14
  248. package/storages/access_checking.js.map +0 -1
  249. package/storages/dataset.d.ts.map +0 -1
  250. package/storages/dataset.js.map +0 -1
  251. package/storages/index.d.ts.map +0 -1
  252. package/storages/index.js.map +0 -1
  253. package/storages/key_value_store.d.ts.map +0 -1
  254. package/storages/key_value_store.js.map +0 -1
  255. package/storages/request_list.d.ts.map +0 -1
  256. package/storages/request_list.js.map +0 -1
  257. package/storages/request_provider.d.ts +0 -308
  258. package/storages/request_provider.d.ts.map +0 -1
  259. package/storages/request_provider.js +0 -555
  260. package/storages/request_provider.js.map +0 -1
  261. package/storages/request_queue.d.ts.map +0 -1
  262. package/storages/request_queue.js.map +0 -1
  263. package/storages/request_queue_v2.d.ts +0 -87
  264. package/storages/request_queue_v2.d.ts.map +0 -1
  265. package/storages/request_queue_v2.js +0 -438
  266. package/storages/request_queue_v2.js.map +0 -1
  267. package/storages/sitemap_request_list.d.ts.map +0 -1
  268. package/storages/sitemap_request_list.js.map +0 -1
  269. package/storages/storage_manager.d.ts +0 -58
  270. package/storages/storage_manager.d.ts.map +0 -1
  271. package/storages/storage_manager.js +0 -105
  272. package/storages/storage_manager.js.map +0 -1
  273. package/storages/utils.d.ts.map +0 -1
  274. package/storages/utils.js.map +0 -1
  275. package/tsconfig.build.tsbuildinfo +0 -1
  276. package/typedefs.d.ts.map +0 -1
  277. package/typedefs.js.map +0 -1
  278. package/validators.d.ts.map +0 -1
  279. package/validators.js.map +0 -1
@@ -0,0 +1,351 @@
1
+ import ow from 'ow';
2
+ import { betterClearInterval, betterSetInterval } from '@apify/utilities';
3
+ import { serviceLocator } from '../service_locator.js';
4
+ import { Snapshotter } from './snapshotter.js';
5
+ import { SystemStatus } from './system_status.js';
6
+ /**
7
+ * The shareable "governor" behind an {@link AutoscaledPool}: it decides whether there is free compute for one more
8
+ * task by combining live system load (via an internal {@link Snapshotter}) with a concurrency budget it autoscales
9
+ * over time.
10
+ *
11
+ * Sharing one instance between several pools (and therefore several crawlers) caps their *combined* compute, instead
12
+ * of letting each scale independently and oversubscribe the machine.
13
+ *
14
+ * Whoever builds the instance owns its lifecycle: call {@link ConcurrencySystem.start|`start()`} before any
15
+ * borrowing pool runs and {@link ConcurrencySystem.stop|`stop()`} once they are all done (crawlers do this for the
16
+ * default system they build, never for an injected one). Both calls are idempotent, and the first `stop()` tears the
17
+ * system down for every borrower.
18
+ * @category Scaling
19
+ */
20
+ export class ConcurrencySystem {
21
+ log;
22
+ desiredConcurrencyRatio;
23
+ scaleUpStepRatio;
24
+ scaleDownStepRatio;
25
+ #loggingIntervalMillis;
26
+ #autoscaleIntervalMillis;
27
+ maxTasksPerMinute;
28
+ #minConcurrency;
29
+ #maxConcurrency;
30
+ #desiredConcurrency;
31
+ // kept as TS-private _-prefixed: autoscaled_pool tests write this backing field directly
32
+ _currentConcurrency = 0;
33
+ #lastLoggingTime;
34
+ #tasksPerMinute = Array.from({ length: 60 }, () => 0);
35
+ snapshotter;
36
+ #loadSignals;
37
+ systemStatus;
38
+ #autoscaleInterval;
39
+ #tasksDonePerSecondInterval;
40
+ /** Whether the snapshotter and autoscaling intervals are currently running. */
41
+ #running = false;
42
+ /** The in-flight (or completed) startup, memoized so concurrent `start()` calls await one boot. */
43
+ #startPromise;
44
+ /** Set once per session, so a pool outliving `stop()` is reported once rather than every half second. */
45
+ #warnedAboutQueryWhileStopped = false;
46
+ constructor(options = {}) {
47
+ ow(options, ow.object.exactShape({
48
+ maxConcurrency: ow.optional.number.integer.greaterThanOrEqual(1),
49
+ minConcurrency: ow.optional.number.integer.greaterThanOrEqual(1),
50
+ desiredConcurrency: ow.optional.number.integer.greaterThanOrEqual(1),
51
+ desiredConcurrencyRatio: ow.optional.number.greaterThan(0).lessThan(1),
52
+ scaleUpStepRatio: ow.optional.number.greaterThan(0).lessThan(1),
53
+ scaleDownStepRatio: ow.optional.number.greaterThan(0).lessThan(1),
54
+ loggingIntervalSecs: ow.any(ow.number.greaterThan(0), ow.nullOrUndefined),
55
+ autoscaleIntervalSecs: ow.optional.number.greaterThan(0),
56
+ loadSignals: ow.optional.object,
57
+ snapshotHistorySecs: ow.optional.number.greaterThan(0),
58
+ currentHistorySecs: ow.optional.number.greaterThan(0),
59
+ log: ow.optional.object,
60
+ maxTasksPerMinute: ow.optional.number.integerOrInfinite.greaterThanOrEqual(1),
61
+ }));
62
+ const { maxConcurrency = 200, minConcurrency = 1, desiredConcurrency, desiredConcurrencyRatio = 0.9, scaleUpStepRatio = 0.05, scaleDownStepRatio = 0.05, loggingIntervalSecs = 60, autoscaleIntervalSecs = 10, loadSignals = {}, snapshotHistorySecs, currentHistorySecs, log = serviceLocator.getLogger(), maxTasksPerMinute = Infinity, } = options;
63
+ this.log = log.child({ prefix: 'ConcurrencySystem' });
64
+ this.desiredConcurrencyRatio = desiredConcurrencyRatio;
65
+ this.scaleUpStepRatio = scaleUpStepRatio;
66
+ this.scaleDownStepRatio = scaleDownStepRatio;
67
+ this.#loggingIntervalMillis = (loggingIntervalSecs ?? 0) * 1000;
68
+ this.#autoscaleIntervalMillis = autoscaleIntervalSecs * 1000;
69
+ this.maxTasksPerMinute = maxTasksPerMinute;
70
+ this.#minConcurrency = minConcurrency;
71
+ this.#maxConcurrency = maxConcurrency;
72
+ this.#desiredConcurrency = desiredConcurrency ?? minConcurrency;
73
+ this.clampDesiredConcurrency();
74
+ this.autoscale = this.autoscale.bind(this);
75
+ this.incrementTasksDonePerSecond = this.incrementTasksDonePerSecond.bind(this);
76
+ // The built-in signals are collected by the snapshotter; custom ones are simply evaluated alongside them.
77
+ const { custom: customLoadSignals = [], ...builtinSignalOptions } = loadSignals;
78
+ this.snapshotter = new Snapshotter(builtinSignalOptions);
79
+ this.#loadSignals = customLoadSignals;
80
+ this.systemStatus = new SystemStatus({
81
+ snapshotter: this.snapshotter,
82
+ loadSignals: customLoadSignals,
83
+ currentHistorySecs,
84
+ // Both windows are requested from the signals explicitly, so a signal's own retention can neither widen
85
+ // nor (given the start context below) narrow what it contributes.
86
+ historySecs: snapshotHistorySecs,
87
+ });
88
+ }
89
+ /**
90
+ * Gets the minimum number of tasks running in parallel.
91
+ */
92
+ get minConcurrency() {
93
+ return this.#minConcurrency;
94
+ }
95
+ /**
96
+ * Sets the minimum number of tasks running in parallel.
97
+ *
98
+ * *WARNING:* If you set this value too high with respect to the available system memory and CPU, your code might run extremely slow or crash.
99
+ * If you're not sure, just keep the default value and the concurrency will scale up automatically.
100
+ */
101
+ set minConcurrency(value) {
102
+ ow(value, ow.optional.number.integer.greaterThanOrEqual(1));
103
+ this.#minConcurrency = value;
104
+ this.clampDesiredConcurrency();
105
+ }
106
+ /**
107
+ * Gets the maximum number of tasks running in parallel.
108
+ */
109
+ get maxConcurrency() {
110
+ return this.#maxConcurrency;
111
+ }
112
+ /**
113
+ * Sets the maximum number of tasks running in parallel. Lowering it below the current
114
+ * {@link ConcurrencySystem.desiredConcurrency|`desiredConcurrency`} pulls that down to the new ceiling too, so
115
+ * the change takes effect immediately (in-flight tasks are never cancelled — the budget simply drains to the new
116
+ * limit as they settle).
117
+ */
118
+ set maxConcurrency(value) {
119
+ ow(value, ow.optional.number.integer.greaterThanOrEqual(1));
120
+ this.#maxConcurrency = value;
121
+ this.clampDesiredConcurrency();
122
+ }
123
+ /**
124
+ * Gets the desired concurrency for the system,
125
+ * which is an estimated number of parallel tasks that the system can currently support.
126
+ */
127
+ get desiredConcurrency() {
128
+ return this.#desiredConcurrency;
129
+ }
130
+ /**
131
+ * Sets the desired concurrency for the system, i.e. the number of tasks that should be running
132
+ * in parallel if there's large enough supply of tasks.
133
+ */
134
+ set desiredConcurrency(value) {
135
+ ow(value, ow.optional.number.integer.greaterThanOrEqual(1));
136
+ this.#desiredConcurrency = value;
137
+ this.clampDesiredConcurrency();
138
+ }
139
+ /**
140
+ * Re-establishes `minConcurrency <= desiredConcurrency <= maxConcurrency` after any of the three is retuned.
141
+ * Dispatch gates on the desired value alone, so one stranded above `maxConcurrency` would make the ceiling
142
+ * meaningless. A contradictory pair (`minConcurrency > maxConcurrency`) resolves in favour of the maximum, since
143
+ * that is the limit callers set in order to protect something.
144
+ */
145
+ clampDesiredConcurrency() {
146
+ const atLeastMin = Math.max(this.#desiredConcurrency, this.#minConcurrency);
147
+ this.#desiredConcurrency = Math.min(atLeastMin, this.#maxConcurrency);
148
+ }
149
+ get currentConcurrency() {
150
+ return this._currentConcurrency;
151
+ }
152
+ /** Whether the system is currently monitoring load and autoscaling the budget. */
153
+ get isRunning() {
154
+ return this.#running;
155
+ }
156
+ /**
157
+ * Boots the underlying snapshotter and the autoscaling interval. Idempotent, so a shared system isn't restarted
158
+ * when handed to another consumer; concurrent callers await one startup. Rejects, leaving nothing running, if a
159
+ * signal fails to start.
160
+ */
161
+ async start() {
162
+ // Unwound and dropped again on failure, so a later `start()` retries instead of resolving instantly against
163
+ // a system that is down.
164
+ this.#startPromise ??= this.boot().catch(async (error) => {
165
+ this.#startPromise = undefined;
166
+ await this.shutDown();
167
+ throw error;
168
+ });
169
+ await this.#startPromise;
170
+ }
171
+ async boot() {
172
+ // Per-session measurement state, reset so a restarted system isn't judged on the previous session. The
173
+ // per-minute window matters most: its ageing interval is cleared while we are down, so starts from before an
174
+ // arbitrarily long stop would otherwise still count against "this minute" and trip the cap immediately.
175
+ this.#tasksPerMinute = Array.from({ length: 60 }, () => 0);
176
+ this.#lastLoggingTime = undefined;
177
+ this.#warnedAboutQueryWhileStopped = false;
178
+ // Signals are told how much history to keep when they start: exactly the longest window they will be sampled
179
+ // over, so nobody has to guess a retention value that matches this system's configuration.
180
+ const startContext = { maxSampleWindowMillis: this.systemStatus.maxSampleWindowMillis };
181
+ await this.snapshotter.start(startContext);
182
+ await Promise.all(this.#loadSignals.map(async (s) => s.start(startContext)));
183
+ this.#autoscaleInterval = betterSetInterval(this.autoscale, this.#autoscaleIntervalMillis);
184
+ if (this.maxTasksPerMinute !== Infinity) {
185
+ this.#tasksDonePerSecondInterval = betterSetInterval(this.incrementTasksDonePerSecond, 1000);
186
+ }
187
+ // Last, so `isRunning` never claims a system whose signals aren't collecting yet.
188
+ this.#running = true;
189
+ }
190
+ /**
191
+ * Stops the snapshotter and intervals. Idempotent and safe to call even if the system was never started.
192
+ */
193
+ async stop() {
194
+ if (this.#startPromise === undefined) {
195
+ return;
196
+ }
197
+ // Waited out rather than interrupted, or the intervals a starting signal is about to register outlive us.
198
+ await this.#startPromise.catch(() => { });
199
+ this.#startPromise = undefined;
200
+ this.#running = false;
201
+ await this.shutDown();
202
+ }
203
+ async shutDown() {
204
+ if (this.#autoscaleInterval)
205
+ betterClearInterval(this.#autoscaleInterval);
206
+ if (this.#tasksDonePerSecondInterval)
207
+ betterClearInterval(this.#tasksDonePerSecondInterval);
208
+ await this.snapshotter.stop();
209
+ await Promise.all(this.#loadSignals.map(async (s) => s.stop()));
210
+ }
211
+ /**
212
+ * Reports, once per session, that capacity is being queried on a system that isn't running — a mistake nothing
213
+ * else catches, since {@link AutoscaledPool.run|`pool.run()`} only checks
214
+ * {@link ConcurrencySystem.isRunning|`isRunning`} on the way in. Both the overload verdict and
215
+ * `desiredConcurrency` are frozen at that point, so the borrowing pool would otherwise just quietly mis-scale.
216
+ */
217
+ warnIfNotRunning() {
218
+ if (this.#running || this.#warnedAboutQueryWhileStopped) {
219
+ return;
220
+ }
221
+ this.#warnedAboutQueryWhileStopped = true;
222
+ this.log.warning('Capacity is being queried on a ConcurrencySystem that is not running, so system load is no longer being ' +
223
+ 'monitored and the concurrency will no longer be adjusted. Whoever creates a ConcurrencySystem owns ' +
224
+ 'its lifecycle: call `await concurrencySystem.stop()` only once every pool and crawler borrowing it ' +
225
+ 'has finished.');
226
+ }
227
+ /**
228
+ * May **one more** task start right now? Returns `false` when the shared budget is spent (desired concurrency
229
+ * reached) or when the machine is overloaded past `minConcurrency`.
230
+ *
231
+ * One budget for the whole machine, so the asking consumer is ignored — and therefore optional here, unlike in the
232
+ * interface, letting the answer be queried directly.
233
+ */
234
+ hasCapacityForTask(_consumer) {
235
+ this.warnIfNotRunning();
236
+ if (this._currentConcurrency >= this.#desiredConcurrency) {
237
+ this.log.perf('Task will not run. Desired concurrency achieved.');
238
+ return false;
239
+ }
240
+ const currentStatus = this.systemStatus.getCurrentStatus();
241
+ const { isSystemIdle } = currentStatus;
242
+ if (!isSystemIdle && this._currentConcurrency >= this.#minConcurrency) {
243
+ this.log.perf('Task will not be run. System is overloaded.', currentStatus);
244
+ return false;
245
+ }
246
+ return true;
247
+ }
248
+ /** Whether the per-minute task cap has been reached. */
249
+ get isOverMaxRequestLimit() {
250
+ if (this.maxTasksPerMinute === Infinity) {
251
+ return false;
252
+ }
253
+ return this.#tasksPerMinute.reduce((acc, curr) => acc + curr, 0) >= this.maxTasksPerMinute;
254
+ }
255
+ /**
256
+ * Atomically books a task against the shared budget: re-checks
257
+ * {@link ConcurrencySystem.hasCapacityForTask|`hasCapacityForTask()`} plus the per-minute task cap and
258
+ * increments the current concurrency in one synchronous step, returning `false` (without booking) when there is no
259
+ * room. Call right before the task actually runs.
260
+ *
261
+ * The cap is enforced here rather than in the pre-check so that an empty queue never blocks the pool for a whole
262
+ * extra minute.
263
+ */
264
+ tryRegisterTaskStart(consumer) {
265
+ if (!this.hasCapacityForTask(consumer)) {
266
+ return false;
267
+ }
268
+ if (this.isOverMaxRequestLimit) {
269
+ this.log.perf('Task will not run. Maximum tasks per minute reached.');
270
+ return false;
271
+ }
272
+ this._currentConcurrency++;
273
+ this.#tasksPerMinute[0]++;
274
+ return true;
275
+ }
276
+ /** Returns a slot to the shared budget, whoever booked it. */
277
+ registerTaskEnd(_consumer) {
278
+ this._currentConcurrency--;
279
+ }
280
+ /**
281
+ * What the system currently makes of the machine: the per-signal overload verdicts, evaluated over the
282
+ * task-gating window, exactly as {@link ConcurrencySystem.hasCapacityForTask|`hasCapacityForTask()`} sees them.
283
+ * The one public window into load monitoring — useful for answering *why* a crawl is not scaling up.
284
+ */
285
+ getCurrentStatus() {
286
+ return this.systemStatus.getCurrentStatus();
287
+ }
288
+ /**
289
+ * Evaluates the historical system status and scales the shared desired concurrency up or down accordingly. Driven
290
+ * by the autoscaling interval started in {@link ConcurrencySystem.start|`start()`}.
291
+ */
292
+ autoscale(intervalCallback) {
293
+ if (this.isOverMaxRequestLimit)
294
+ return intervalCallback();
295
+ const systemStatus = this.systemStatus.getHistoricalStatus();
296
+ const { isSystemIdle } = systemStatus;
297
+ const weAreNotAtMax = this.#desiredConcurrency < this.#maxConcurrency;
298
+ const minCurrentConcurrency = Math.floor(this.#desiredConcurrency * this.desiredConcurrencyRatio);
299
+ const weAreReachingDesiredConcurrency = this._currentConcurrency >= minCurrentConcurrency;
300
+ if (isSystemIdle && weAreNotAtMax && weAreReachingDesiredConcurrency)
301
+ this.scaleUp(systemStatus);
302
+ const isSystemOverloaded = !isSystemIdle;
303
+ const weAreNotAtMin = this.#desiredConcurrency > this.#minConcurrency;
304
+ if (isSystemOverloaded && weAreNotAtMin)
305
+ this.scaleDown(systemStatus);
306
+ if (this.#loggingIntervalMillis > 0) {
307
+ const now = Date.now();
308
+ if (this.#lastLoggingTime == null) {
309
+ this.#lastLoggingTime = now;
310
+ }
311
+ else if (now > this.#lastLoggingTime + this.#loggingIntervalMillis) {
312
+ this.#lastLoggingTime = now;
313
+ this.log.info('state', {
314
+ currentConcurrency: this._currentConcurrency,
315
+ desiredConcurrency: this.#desiredConcurrency,
316
+ systemStatus,
317
+ });
318
+ }
319
+ }
320
+ return intervalCallback();
321
+ }
322
+ /**
323
+ * Scales the system up by increasing the desired concurrency by the scaleUpStepRatio.
324
+ */
325
+ scaleUp(systemStatus) {
326
+ const step = Math.ceil(this.#desiredConcurrency * this.scaleUpStepRatio);
327
+ this.#desiredConcurrency = Math.min(this.#maxConcurrency, this.#desiredConcurrency + step);
328
+ this.log.debug('scaling up', {
329
+ oldConcurrency: this.#desiredConcurrency - step,
330
+ newConcurrency: this.#desiredConcurrency,
331
+ systemStatus,
332
+ });
333
+ }
334
+ /**
335
+ * Scales the system down by decreasing the desired concurrency by the scaleDownStepRatio.
336
+ */
337
+ scaleDown(systemStatus) {
338
+ const step = Math.ceil(this.#desiredConcurrency * this.scaleDownStepRatio);
339
+ this.#desiredConcurrency = Math.max(this.#minConcurrency, this.#desiredConcurrency - step);
340
+ this.log.debug('scaling down', {
341
+ oldConcurrency: this.#desiredConcurrency + step,
342
+ newConcurrency: this.#desiredConcurrency,
343
+ systemStatus,
344
+ });
345
+ }
346
+ incrementTasksDonePerSecond(intervalCallback) {
347
+ this.#tasksPerMinute.unshift(0);
348
+ this.#tasksPerMinute.pop();
349
+ return intervalCallback();
350
+ }
351
+ }
@@ -0,0 +1,43 @@
1
+ import type { LoadSignal, LoadSignalStartContext, LoadSnapshot } from './load_signal.js';
2
+ import type { SystemInfo } from './system_status.js';
3
+ /**
4
+ * A snapshot produced by the built-in CPU signal.
5
+ * @internal
6
+ */
7
+ export interface CpuSnapshot extends LoadSnapshot {
8
+ usedRatio: number;
9
+ ticks?: {
10
+ idle: number;
11
+ total: number;
12
+ };
13
+ }
14
+ /**
15
+ * Tuning for the built-in **CPU** load signal, as accepted both by {@link CpuLoadSignal} and by the
16
+ * {@link LoadSignalsOptions.cpu|`cpu`} shorthand on {@link LoadSignalsOptions}.
17
+ */
18
+ export interface CpuLoadSignalOptions {
19
+ /**
20
+ * Maximum ratio of overloaded snapshots in a sample before the CPU counts as overloaded.
21
+ * @default 0.4
22
+ */
23
+ overloadedRatio?: number;
24
+ }
25
+ /**
26
+ * Tracks CPU usage via `SYSTEM_INFO` events and reports overload when the platform or local OS metrics indicate the
27
+ * CPU is overloaded.
28
+ *
29
+ * Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
30
+ *
31
+ * @category Scaling
32
+ */
33
+ export declare class CpuLoadSignal implements LoadSignal {
34
+ #private;
35
+ readonly name = "cpuInfo";
36
+ readonly overloadedRatio: number;
37
+ constructor(options?: CpuLoadSignalOptions);
38
+ start(context: LoadSignalStartContext): Promise<void>;
39
+ stop(): Promise<void>;
40
+ getSample(sampleDurationMillis?: number): LoadSnapshot[];
41
+ /** @internal Records a snapshot from a `SYSTEM_INFO` payload. Exposed for tests. */
42
+ handle(systemInfo: SystemInfo): void;
43
+ }
@@ -0,0 +1,47 @@
1
+ import { EventType } from '../events/event_manager.js';
2
+ import { serviceLocator } from '../service_locator.js';
3
+ import { SnapshotStore } from './load_signal.js';
4
+ /**
5
+ * Tracks CPU usage via `SYSTEM_INFO` events and reports overload when the platform or local OS metrics indicate the
6
+ * CPU is overloaded.
7
+ *
8
+ * Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
9
+ *
10
+ * @category Scaling
11
+ */
12
+ export class CpuLoadSignal {
13
+ name = 'cpuInfo';
14
+ overloadedRatio;
15
+ #store = new SnapshotStore();
16
+ #events;
17
+ constructor(options = {}) {
18
+ this.overloadedRatio = options.overloadedRatio ?? 0.4;
19
+ this.handle = this.handle.bind(this);
20
+ }
21
+ async start(context) {
22
+ this.#store.useSampleWindow(context.maxSampleWindowMillis);
23
+ // A new session starts from a clean slate, so it is not judged on measurements from before the downtime.
24
+ this.#store.clear();
25
+ // Resolved here rather than in the constructor, so an instance built ahead of time (to be wrapped, or shared
26
+ // between systems) cannot capture whichever event manager happened to be registered at that moment.
27
+ this.#events = serviceLocator.getEventManager();
28
+ this.#events.on(EventType.SYSTEM_INFO, this.handle);
29
+ }
30
+ async stop() {
31
+ this.#events?.off(EventType.SYSTEM_INFO, this.handle);
32
+ this.#events = undefined;
33
+ }
34
+ getSample(sampleDurationMillis) {
35
+ return this.#store.getSample(sampleDurationMillis);
36
+ }
37
+ /** @internal Records a snapshot from a `SYSTEM_INFO` payload. Exposed for tests. */
38
+ handle(systemInfo) {
39
+ const { cpuCurrentUsage, isCpuOverloaded } = systemInfo;
40
+ const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
41
+ this.#store.push({
42
+ createdAt,
43
+ isOverloaded: isCpuOverloaded,
44
+ usedRatio: Math.ceil(cpuCurrentUsage / 100),
45
+ }, createdAt);
46
+ }
47
+ }
@@ -0,0 +1,51 @@
1
+ import type { LoadSignal, LoadSignalStartContext, LoadSnapshot } from './load_signal.js';
2
+ /**
3
+ * A snapshot produced by the built-in event loop signal.
4
+ * @internal
5
+ */
6
+ export interface EventLoopSnapshot extends LoadSnapshot {
7
+ exceededMillis: number;
8
+ }
9
+ /**
10
+ * Tuning for the built-in **event loop** load signal, as accepted both by {@link EventLoopLoadSignal} and by the
11
+ * {@link LoadSignalsOptions.eventLoop|`eventLoop`} shorthand on {@link LoadSignalsOptions}.
12
+ */
13
+ export interface EventLoopLoadSignalOptions {
14
+ /**
15
+ * Defines the interval of measuring the event loop response time, in seconds.
16
+ * @default 0.5
17
+ */
18
+ snapshotIntervalSecs?: number;
19
+ /**
20
+ * Maximum allowed delay of the event loop in milliseconds.
21
+ * Exceeding this limit overloads the event loop.
22
+ * @default 50
23
+ */
24
+ maxBlockedMillis?: number;
25
+ /**
26
+ * Maximum ratio of overloaded snapshots in a sample before the event loop counts as overloaded.
27
+ * @default 0.6
28
+ */
29
+ overloadedRatio?: number;
30
+ }
31
+ /**
32
+ * Periodically measures event loop delay and reports overload when the delay exceeds a configured threshold.
33
+ *
34
+ * Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
35
+ *
36
+ * @category Scaling
37
+ */
38
+ export declare class EventLoopLoadSignal implements LoadSignal {
39
+ #private;
40
+ readonly name = "eventLoopInfo";
41
+ readonly overloadedRatio: number;
42
+ constructor(options?: EventLoopLoadSignalOptions);
43
+ start(context: LoadSignalStartContext): Promise<void>;
44
+ stop(): Promise<void>;
45
+ getSample(sampleDurationMillis?: number): LoadSnapshot[];
46
+ /**
47
+ * Records one snapshot: how much later than scheduled this tick ran is how long the loop was blocked.
48
+ * @internal Also lets tests drive the measurement without waiting on a timer.
49
+ */
50
+ handle(intervalCallback: () => unknown): void;
51
+ }
@@ -0,0 +1,60 @@
1
+ import { betterClearInterval, betterSetInterval } from '@apify/utilities';
2
+ import { SnapshotStore } from './load_signal.js';
3
+ /**
4
+ * Periodically measures event loop delay and reports overload when the delay exceeds a configured threshold.
5
+ *
6
+ * Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
7
+ *
8
+ * @category Scaling
9
+ */
10
+ export class EventLoopLoadSignal {
11
+ name = 'eventLoopInfo';
12
+ overloadedRatio;
13
+ #store = new SnapshotStore();
14
+ #intervalMillis;
15
+ #maxBlockedMillis;
16
+ #interval;
17
+ constructor(options = {}) {
18
+ this.overloadedRatio = options.overloadedRatio ?? 0.6;
19
+ this.#intervalMillis = (options.snapshotIntervalSecs ?? 0.5) * 1000;
20
+ this.#maxBlockedMillis = options.maxBlockedMillis ?? 50;
21
+ this.handle = this.handle.bind(this);
22
+ }
23
+ async start(context) {
24
+ this.#store.useSampleWindow(context.maxSampleWindowMillis);
25
+ // A new session starts from a clean slate, or the downtime gets charged to the event loop: `handle()` measures
26
+ // the gap since the previous snapshot, which across a restart is however long the system was stopped.
27
+ this.#store.clear();
28
+ this.#interval = betterSetInterval(this.handle, this.#intervalMillis);
29
+ }
30
+ async stop() {
31
+ if (this.#interval)
32
+ betterClearInterval(this.#interval);
33
+ this.#interval = undefined;
34
+ }
35
+ getSample(sampleDurationMillis) {
36
+ return this.#store.getSample(sampleDurationMillis);
37
+ }
38
+ /**
39
+ * Records one snapshot: how much later than scheduled this tick ran is how long the loop was blocked.
40
+ * @internal Also lets tests drive the measurement without waiting on a timer.
41
+ */
42
+ handle(intervalCallback) {
43
+ const now = new Date();
44
+ const snapshot = {
45
+ createdAt: now,
46
+ isOverloaded: false,
47
+ exceededMillis: 0,
48
+ };
49
+ const all = this.#store.getAll();
50
+ const previousSnapshot = all[all.length - 1];
51
+ if (previousSnapshot) {
52
+ const delta = now.getTime() - +previousSnapshot.createdAt - this.#intervalMillis;
53
+ if (delta > this.#maxBlockedMillis)
54
+ snapshot.isOverloaded = true;
55
+ snapshot.exceededMillis = Math.max(delta - this.#maxBlockedMillis, 0);
56
+ }
57
+ this.#store.push(snapshot, now);
58
+ intervalCallback();
59
+ }
60
+ }
@@ -1,4 +1,9 @@
1
1
  export * from './autoscaled_pool.js';
2
+ export * from './concurrency_system.js';
3
+ export * from './client_load_signal.js';
4
+ export * from './cpu_load_signal.js';
5
+ export * from './event_loop_load_signal.js';
6
+ export * from './load_signal.js';
7
+ export * from './memory_load_signal.js';
2
8
  export * from './snapshotter.js';
3
9
  export * from './system_status.js';
4
- //# sourceMappingURL=index.d.ts.map
@@ -1,4 +1,9 @@
1
1
  export * from './autoscaled_pool.js';
2
+ export * from './concurrency_system.js';
3
+ export * from './client_load_signal.js';
4
+ export * from './cpu_load_signal.js';
5
+ export * from './event_loop_load_signal.js';
6
+ export * from './load_signal.js';
7
+ export * from './memory_load_signal.js';
2
8
  export * from './snapshotter.js';
3
9
  export * from './system_status.js';
4
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,99 @@
1
+ import type { ClientInfo } from './system_status.js';
2
+ /**
3
+ * A snapshot of a resource's overload state at a point in time.
4
+ */
5
+ export interface LoadSnapshot {
6
+ createdAt: Date;
7
+ isOverloaded: boolean;
8
+ }
9
+ /**
10
+ * Handed to a {@link LoadSignal} when it starts, so it can size its snapshot retention to what it will actually
11
+ * be asked for — without having to know how the {@link ConcurrencySystem} that drives it is configured.
12
+ */
13
+ export interface LoadSignalStartContext {
14
+ /**
15
+ * The longest sample window the signal will be queried with (the wider of the task-gating and autoscaling
16
+ * windows). Keeping less history than this contributes a narrower view of the resource than the other signals;
17
+ * keeping more is wasted memory, as the extra snapshots are never sampled.
18
+ */
19
+ maxSampleWindowMillis: number;
20
+ }
21
+ /**
22
+ * A signal that reports whether a particular resource is overloaded. The {@link ConcurrencySystem} aggregates
23
+ * several of them — if any one reports overload, the system is overloaded.
24
+ *
25
+ * The built-in signals cover memory, CPU, event loop and storage-client rate limits. Implement this interface to add
26
+ * your own (navigation timeouts, proxy health, …) and pass them via
27
+ * {@link LoadSignalsOptions.custom|`loadSignals.custom`}; {@link SnapshotStore} does the time-windowed
28
+ * bookkeeping if you want it. Each built-in is also a public class, so one can be *wrapped* rather than reimplemented
29
+ * — construct it yourself and switch the default off with {@link LoadSignalsOptions.cpu|`cpu: false`} or friends.
30
+ */
31
+ export interface LoadSignal {
32
+ /**
33
+ * This signal's key in the reported {@link SystemInfo}, also used in logging — so it must be unique among the
34
+ * signals of one {@link ConcurrencySystem}, which throws on a duplicate. The four built-in names (`memInfo`,
35
+ * `eventLoopInfo`, `cpuInfo`, `clientInfo`) land in the correspondingly named `SystemInfo` fields rather than the
36
+ * `loadSignalInfo` bag; taking one over means switching that built-in off.
37
+ */
38
+ readonly name: string;
39
+ /**
40
+ * Maximum ratio of overloaded snapshots in a sample before the signal
41
+ * is considered overloaded. For example, `0.2` means the signal fires
42
+ * when more than 20% of the sample window is overloaded.
43
+ */
44
+ readonly overloadedRatio: number;
45
+ /**
46
+ * Start collecting snapshots, retaining at least the sample window named in the `context`. Called when the
47
+ * {@link ConcurrencySystem} starts — which may be a *restart*, so drop anything measured before it.
48
+ */
49
+ start(context: LoadSignalStartContext): Promise<void>;
50
+ /** Stop collecting snapshots. Called when the {@link ConcurrencySystem} shuts down. */
51
+ stop(): Promise<void>;
52
+ /**
53
+ * Return snapshots for a recent time window (used for "current" status).
54
+ * @param sampleDurationMillis How far back to look, in milliseconds.
55
+ */
56
+ getSample(sampleDurationMillis?: number): LoadSnapshot[];
57
+ }
58
+ /**
59
+ * A time-pruning, time-windowed store for `LoadSnapshot` values. All four built-in signals compose with one of these,
60
+ * and so can yours — it is the only part of their machinery worth reusing.
61
+ */
62
+ export declare class SnapshotStore<T extends LoadSnapshot = LoadSnapshot> {
63
+ #private;
64
+ /** Retention window in milliseconds. Unbounded until {@link SnapshotStore.useSampleWindow|`useSampleWindow()`}. */
65
+ private historyMillis;
66
+ /**
67
+ * Sizes retention to the window the signal will be sampled over, as handed to it in
68
+ * {@link LoadSignal.start|`start()`}. Until this is called nothing is pruned at all, so a signal that ignores
69
+ * its start context grows unboundedly.
70
+ */
71
+ useSampleWindow(maxSampleWindowMillis: number): void;
72
+ /**
73
+ * Add a snapshot and prune entries older than the history window.
74
+ */
75
+ push(snapshot: T, now?: Date): void;
76
+ /**
77
+ * Return all snapshots, or only those within the given time window.
78
+ */
79
+ getSample(sampleDurationMillis?: number): T[];
80
+ /**
81
+ * Direct, unwindowed access to the underlying array — used by signals whose handler needs the previous snapshot
82
+ * to compute a delta (e.g. the event loop and client signals read the last entry to measure change since it).
83
+ */
84
+ getAll(): T[];
85
+ /**
86
+ * Discards every retained snapshot. The built-in signals do this when they *start*, so that a session neither
87
+ * samples nor diffs against measurements from before the preceding downtime — pruning is relative to the newest
88
+ * snapshot rather than the wall clock, so stale entries would otherwise survive indefinitely. Clearing on start
89
+ * rather than on stop leaves a finished session readable.
90
+ */
91
+ clear(): void;
92
+ }
93
+ /**
94
+ * Evaluate whether a sample of `LoadSnapshot` values exceeds the given
95
+ * overloaded ratio, using a time-weighted average. This is the shared
96
+ * evaluation logic used by `SystemStatus` for all signal types.
97
+ * @internal
98
+ */
99
+ export declare function evaluateLoadSignalSample(sample: LoadSnapshot[], overloadedRatio: number): ClientInfo;