@crawlee/core 4.0.0-beta.9 → 4.0.0-beta.91

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 (263) hide show
  1. package/README.md +17 -13
  2. package/autoscaling/autoscaled_pool.d.ts +66 -153
  3. package/autoscaling/autoscaled_pool.js +95 -251
  4. package/autoscaling/client_load_signal.d.ts +59 -0
  5. package/autoscaling/client_load_signal.js +73 -0
  6. package/autoscaling/concurrency_system.d.ts +283 -0
  7. package/autoscaling/concurrency_system.js +350 -0
  8. package/autoscaling/cpu_load_signal.d.ts +44 -0
  9. package/autoscaling/cpu_load_signal.js +46 -0
  10. package/autoscaling/event_loop_load_signal.d.ts +54 -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 +103 -0
  16. package/autoscaling/memory_load_signal.d.ts +56 -0
  17. package/autoscaling/memory_load_signal.js +106 -0
  18. package/autoscaling/snapshotter.d.ts +61 -163
  19. package/autoscaling/snapshotter.js +45 -263
  20. package/autoscaling/system_status.d.ts +63 -83
  21. package/autoscaling/system_status.js +90 -120
  22. package/configuration.d.ts +90 -223
  23. package/configuration.js +164 -222
  24. package/cookie_utils.d.ts +4 -3
  25. package/cookie_utils.js +20 -13
  26. package/crawlers/context_pipeline.d.ts +70 -0
  27. package/crawlers/context_pipeline.js +122 -0
  28. package/crawlers/crawler_commons.d.ts +76 -33
  29. package/crawlers/crawler_commons.js +15 -24
  30. package/crawlers/crawler_utils.d.ts +2 -3
  31. package/crawlers/crawler_utils.js +1 -2
  32. package/crawlers/error_snapshotter.d.ts +3 -3
  33. package/crawlers/error_snapshotter.js +2 -3
  34. package/crawlers/error_tracker.d.ts +2 -2
  35. package/crawlers/error_tracker.js +0 -1
  36. package/crawlers/index.d.ts +1 -2
  37. package/crawlers/index.js +1 -2
  38. package/crawlers/internals/types.d.ts +7 -0
  39. package/crawlers/internals/types.js +1 -0
  40. package/crawlers/statistics.d.ts +28 -23
  41. package/crawlers/statistics.js +38 -34
  42. package/debug.d.ts +36 -0
  43. package/debug.js +70 -0
  44. package/enqueue_links/enqueue_links.d.ts +44 -22
  45. package/enqueue_links/enqueue_links.js +51 -30
  46. package/enqueue_links/index.d.ts +0 -1
  47. package/enqueue_links/index.js +0 -1
  48. package/enqueue_links/shared.d.ts +25 -9
  49. package/enqueue_links/shared.js +69 -38
  50. package/errors.d.ts +53 -4
  51. package/errors.js +70 -5
  52. package/events/event_manager.d.ts +34 -8
  53. package/events/event_manager.js +8 -10
  54. package/events/index.d.ts +0 -1
  55. package/events/index.js +0 -1
  56. package/events/local_event_manager.d.ts +15 -3
  57. package/events/local_event_manager.js +37 -11
  58. package/index.d.ts +6 -3
  59. package/index.js +5 -2
  60. package/log.d.ts +82 -3
  61. package/log.js +102 -1
  62. package/memory-storage/consts.d.ts +4 -0
  63. package/memory-storage/consts.js +4 -0
  64. package/memory-storage/index.d.ts +1 -0
  65. package/memory-storage/index.js +1 -0
  66. package/memory-storage/memory-storage.d.ts +46 -0
  67. package/memory-storage/memory-storage.js +136 -0
  68. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  69. package/memory-storage/resource-clients/common/base-client.js +6 -0
  70. package/memory-storage/resource-clients/dataset.d.ts +40 -0
  71. package/memory-storage/resource-clients/dataset.js +113 -0
  72. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  73. package/memory-storage/resource-clients/key-value-store.js +203 -0
  74. package/memory-storage/resource-clients/request-queue.d.ts +96 -0
  75. package/memory-storage/resource-clients/request-queue.js +421 -0
  76. package/memory-storage/utils.d.ts +17 -0
  77. package/memory-storage/utils.js +42 -0
  78. package/owned_or_injected.d.ts +60 -0
  79. package/owned_or_injected.js +98 -0
  80. package/package.json +12 -10
  81. package/proxy_configuration.d.ts +29 -152
  82. package/proxy_configuration.js +21 -173
  83. package/recoverable_state.d.ts +120 -0
  84. package/recoverable_state.js +143 -0
  85. package/request.d.ts +85 -15
  86. package/request.js +107 -28
  87. package/router.d.ts +144 -19
  88. package/router.js +125 -30
  89. package/serialization.d.ts +0 -1
  90. package/serialization.js +1 -2
  91. package/service_locator.d.ts +156 -0
  92. package/service_locator.js +238 -0
  93. package/session_pool/consts.d.ts +1 -2
  94. package/session_pool/consts.js +1 -2
  95. package/session_pool/errors.d.ts +0 -1
  96. package/session_pool/errors.js +0 -1
  97. package/session_pool/fingerprint.d.ts +9 -0
  98. package/session_pool/fingerprint.js +30 -0
  99. package/session_pool/index.d.ts +0 -2
  100. package/session_pool/index.js +0 -2
  101. package/session_pool/session.d.ts +37 -75
  102. package/session_pool/session.js +49 -102
  103. package/session_pool/session_pool.d.ts +85 -90
  104. package/session_pool/session_pool.js +131 -120
  105. package/storages/access_checking.d.ts +1 -2
  106. package/storages/access_checking.js +5 -2
  107. package/storages/dataset.d.ts +103 -54
  108. package/storages/dataset.js +174 -132
  109. package/storages/index.d.ts +8 -7
  110. package/storages/index.js +6 -7
  111. package/storages/key_value_store.d.ts +167 -39
  112. package/storages/key_value_store.js +274 -127
  113. package/storages/key_value_store_codec.d.ts +32 -0
  114. package/storages/key_value_store_codec.js +113 -0
  115. package/storages/request_dedup_cache.d.ts +23 -0
  116. package/storages/request_dedup_cache.js +48 -0
  117. package/storages/request_list.d.ts +54 -98
  118. package/storages/request_list.js +99 -75
  119. package/storages/request_loader.d.ts +96 -0
  120. package/storages/request_loader.js +1 -0
  121. package/storages/request_manager.d.ts +33 -0
  122. package/storages/request_manager.js +1 -0
  123. package/storages/request_manager_tandem.d.ts +106 -0
  124. package/storages/request_manager_tandem.js +197 -0
  125. package/storages/request_queue.d.ts +290 -47
  126. package/storages/request_queue.js +629 -215
  127. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +28 -24
  128. package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +49 -47
  129. package/storages/storage_instance_manager.d.ts +90 -0
  130. package/storages/storage_instance_manager.js +258 -0
  131. package/storages/storage_stats.d.ts +48 -0
  132. package/storages/storage_stats.js +29 -0
  133. package/storages/utils.d.ts +54 -9
  134. package/storages/utils.js +64 -13
  135. package/system-info/cpu-info.d.ts +67 -0
  136. package/system-info/cpu-info.js +216 -0
  137. package/system-info/memory-info.d.ts +31 -0
  138. package/system-info/memory-info.js +115 -0
  139. package/system-info/ps-tree.d.ts +17 -0
  140. package/system-info/ps-tree.js +144 -0
  141. package/system-info/runtime.d.ts +14 -0
  142. package/system-info/runtime.js +80 -0
  143. package/typedefs.d.ts +0 -6
  144. package/typedefs.js +0 -1
  145. package/validators.d.ts +8 -1
  146. package/validators.js +10 -3
  147. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  148. package/autoscaling/autoscaled_pool.js.map +0 -1
  149. package/autoscaling/index.d.ts.map +0 -1
  150. package/autoscaling/index.js.map +0 -1
  151. package/autoscaling/snapshotter.d.ts.map +0 -1
  152. package/autoscaling/snapshotter.js.map +0 -1
  153. package/autoscaling/system_status.d.ts.map +0 -1
  154. package/autoscaling/system_status.js.map +0 -1
  155. package/configuration.d.ts.map +0 -1
  156. package/configuration.js.map +0 -1
  157. package/cookie_utils.d.ts.map +0 -1
  158. package/cookie_utils.js.map +0 -1
  159. package/crawlers/crawler_commons.d.ts.map +0 -1
  160. package/crawlers/crawler_commons.js.map +0 -1
  161. package/crawlers/crawler_extension.d.ts +0 -12
  162. package/crawlers/crawler_extension.d.ts.map +0 -1
  163. package/crawlers/crawler_extension.js +0 -14
  164. package/crawlers/crawler_extension.js.map +0 -1
  165. package/crawlers/crawler_utils.d.ts.map +0 -1
  166. package/crawlers/crawler_utils.js.map +0 -1
  167. package/crawlers/error_snapshotter.d.ts.map +0 -1
  168. package/crawlers/error_snapshotter.js.map +0 -1
  169. package/crawlers/error_tracker.d.ts.map +0 -1
  170. package/crawlers/error_tracker.js.map +0 -1
  171. package/crawlers/index.d.ts.map +0 -1
  172. package/crawlers/index.js.map +0 -1
  173. package/crawlers/statistics.d.ts.map +0 -1
  174. package/crawlers/statistics.js.map +0 -1
  175. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  176. package/enqueue_links/enqueue_links.js.map +0 -1
  177. package/enqueue_links/index.d.ts.map +0 -1
  178. package/enqueue_links/index.js.map +0 -1
  179. package/enqueue_links/shared.d.ts.map +0 -1
  180. package/enqueue_links/shared.js.map +0 -1
  181. package/errors.d.ts.map +0 -1
  182. package/errors.js.map +0 -1
  183. package/events/event_manager.d.ts.map +0 -1
  184. package/events/event_manager.js.map +0 -1
  185. package/events/index.d.ts.map +0 -1
  186. package/events/index.js.map +0 -1
  187. package/events/local_event_manager.d.ts.map +0 -1
  188. package/events/local_event_manager.js.map +0 -1
  189. package/http_clients/base-http-client.d.ts +0 -134
  190. package/http_clients/base-http-client.d.ts.map +0 -1
  191. package/http_clients/base-http-client.js +0 -33
  192. package/http_clients/base-http-client.js.map +0 -1
  193. package/http_clients/form-data-like.d.ts +0 -67
  194. package/http_clients/form-data-like.d.ts.map +0 -1
  195. package/http_clients/form-data-like.js +0 -5
  196. package/http_clients/form-data-like.js.map +0 -1
  197. package/http_clients/got-scraping-http-client.d.ts +0 -15
  198. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  199. package/http_clients/got-scraping-http-client.js +0 -69
  200. package/http_clients/got-scraping-http-client.js.map +0 -1
  201. package/http_clients/index.d.ts +0 -3
  202. package/http_clients/index.d.ts.map +0 -1
  203. package/http_clients/index.js +0 -3
  204. package/http_clients/index.js.map +0 -1
  205. package/index.d.ts.map +0 -1
  206. package/index.js.map +0 -1
  207. package/log.d.ts.map +0 -1
  208. package/log.js.map +0 -1
  209. package/proxy_configuration.d.ts.map +0 -1
  210. package/proxy_configuration.js.map +0 -1
  211. package/request.d.ts.map +0 -1
  212. package/request.js.map +0 -1
  213. package/router.d.ts.map +0 -1
  214. package/router.js.map +0 -1
  215. package/serialization.d.ts.map +0 -1
  216. package/serialization.js.map +0 -1
  217. package/session_pool/consts.d.ts.map +0 -1
  218. package/session_pool/consts.js.map +0 -1
  219. package/session_pool/errors.d.ts.map +0 -1
  220. package/session_pool/errors.js.map +0 -1
  221. package/session_pool/events.d.ts +0 -3
  222. package/session_pool/events.d.ts.map +0 -1
  223. package/session_pool/events.js +0 -3
  224. package/session_pool/events.js.map +0 -1
  225. package/session_pool/index.d.ts.map +0 -1
  226. package/session_pool/index.js.map +0 -1
  227. package/session_pool/session.d.ts.map +0 -1
  228. package/session_pool/session.js.map +0 -1
  229. package/session_pool/session_pool.d.ts.map +0 -1
  230. package/session_pool/session_pool.js.map +0 -1
  231. package/storages/access_checking.d.ts.map +0 -1
  232. package/storages/access_checking.js.map +0 -1
  233. package/storages/dataset.d.ts.map +0 -1
  234. package/storages/dataset.js.map +0 -1
  235. package/storages/index.d.ts.map +0 -1
  236. package/storages/index.js.map +0 -1
  237. package/storages/key_value_store.d.ts.map +0 -1
  238. package/storages/key_value_store.js.map +0 -1
  239. package/storages/request_list.d.ts.map +0 -1
  240. package/storages/request_list.js.map +0 -1
  241. package/storages/request_provider.d.ts +0 -307
  242. package/storages/request_provider.d.ts.map +0 -1
  243. package/storages/request_provider.js +0 -555
  244. package/storages/request_provider.js.map +0 -1
  245. package/storages/request_queue.d.ts.map +0 -1
  246. package/storages/request_queue.js.map +0 -1
  247. package/storages/request_queue_v2.d.ts +0 -87
  248. package/storages/request_queue_v2.d.ts.map +0 -1
  249. package/storages/request_queue_v2.js +0 -438
  250. package/storages/request_queue_v2.js.map +0 -1
  251. package/storages/sitemap_request_list.d.ts.map +0 -1
  252. package/storages/sitemap_request_list.js.map +0 -1
  253. package/storages/storage_manager.d.ts +0 -58
  254. package/storages/storage_manager.d.ts.map +0 -1
  255. package/storages/storage_manager.js +0 -105
  256. package/storages/storage_manager.js.map +0 -1
  257. package/storages/utils.d.ts.map +0 -1
  258. package/storages/utils.js.map +0 -1
  259. package/tsconfig.build.tsbuildinfo +0 -1
  260. package/typedefs.d.ts.map +0 -1
  261. package/typedefs.js.map +0 -1
  262. package/validators.d.ts.map +0 -1
  263. package/validators.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import type { Configuration } from '../configuration.js';
2
- import { Snapshotter } from './snapshotter.js';
1
+ import type { LoadSignal } from './load_signal.js';
2
+ import type { Snapshotter } from './snapshotter.js';
3
3
  /**
4
4
  * Represents the current status of the system.
5
5
  */
@@ -10,6 +10,7 @@ export interface SystemInfo {
10
10
  eventLoopInfo: ClientInfo;
11
11
  cpuInfo: ClientInfo;
12
12
  clientInfo: ClientInfo;
13
+ memTotalBytes?: number;
13
14
  memCurrentBytes?: number;
14
15
  /**
15
16
  * Platform only property
@@ -26,7 +27,28 @@ export interface SystemInfo {
26
27
  * @internal
27
28
  */
28
29
  createdAt?: Date;
30
+ /**
31
+ * Status of additional load signals beyond the built-in four.
32
+ * Keys are `LoadSignal.name` values, values are overload info.
33
+ */
34
+ loadSignalInfo?: Record<string, ClientInfo>;
29
35
  }
36
+ /**
37
+ * How far back the *current* system status looks by default — the window that gates task dispatch.
38
+ * @internal
39
+ */
40
+ export declare const DEFAULT_CURRENT_HISTORY_SECS = 5;
41
+ /**
42
+ * How far back the *historical* system status looks by default — the window autoscaling decisions are based on, and
43
+ * therefore how much history the signals are asked to retain.
44
+ * @internal
45
+ */
46
+ export declare const DEFAULT_SNAPSHOT_HISTORY_SECS = 30;
47
+ /**
48
+ * An implementation detail of the {@link ConcurrencySystem} — configure it through
49
+ * {@link ConcurrencySystemOptions} (`loadSignals`, `currentHistorySecs` and `snapshotHistorySecs`).
50
+ * @internal
51
+ */
30
52
  export interface SystemStatusOptions {
31
53
  /**
32
54
  * Defines max age of snapshots used in the {@link SystemStatus.getCurrentStatus} measurement.
@@ -34,35 +56,23 @@ export interface SystemStatusOptions {
34
56
  */
35
57
  currentHistorySecs?: number;
36
58
  /**
37
- * Sets the maximum ratio of overloaded snapshots in a memory sample.
38
- * If the sample exceeds this ratio, the system will be overloaded.
39
- * @default 0.2
40
- */
41
- maxMemoryOverloadedRatio?: number;
42
- /**
43
- * Sets the maximum ratio of overloaded snapshots in an event loop sample.
44
- * If the sample exceeds this ratio, the system will be overloaded.
45
- * @default 0.6
59
+ * Defines max age of snapshots used in the {@link SystemStatus.getHistoricalStatus} measurement — the window
60
+ * autoscaling decisions are based on. Applied uniformly to every signal, built-in or custom, so that a signal's
61
+ * private retention cannot silently widen the window.
62
+ * @default 30
46
63
  */
47
- maxEventLoopOverloadedRatio?: number;
64
+ historySecs?: number;
48
65
  /**
49
- * Sets the maximum ratio of overloaded snapshots in a CPU sample.
50
- * If the sample exceeds this ratio, the system will be overloaded.
51
- * @default 0.4
66
+ * The `Snapshotter` whose built-in signals are evaluated.
52
67
  */
53
- maxCpuOverloadedRatio?: number;
68
+ snapshotter: Snapshotter;
54
69
  /**
55
- * Sets the maximum ratio of overloaded snapshots in a Client sample.
56
- * If the sample exceeds this ratio, the system will be overloaded.
57
- * @default 0.3
70
+ * Additional load signals to include in the system status evaluation.
71
+ * These are evaluated alongside the built-in memory, CPU, event loop,
72
+ * and client signals. If any signal reports overload, the system is
73
+ * considered overloaded. Each signal carries its own overload ratio.
58
74
  */
59
- maxClientOverloadedRatio?: number;
60
- /**
61
- * The `Snapshotter` instance to be queried for `SystemStatus`.
62
- */
63
- snapshotter?: Snapshotter;
64
- /** @internal */
65
- config?: Configuration;
75
+ loadSignals?: LoadSignal[];
66
76
  }
67
77
  export interface ClientInfo {
68
78
  isOverloaded: boolean;
@@ -82,35 +92,35 @@ export interface FinalStatistics {
82
92
  crawlerRuntimeMillis: number;
83
93
  }
84
94
  /**
85
- * Provides a simple interface to reading system status from a {@link Snapshotter} instance.
86
- * It only exposes two functions {@link SystemStatus.getCurrentStatus}
87
- * and {@link SystemStatus.getHistoricalStatus}.
88
- * The system status is calculated using a weighted average of overloaded
89
- * messages in the snapshots, with the weights being the time intervals
90
- * between the snapshots. Each resource is calculated separately
91
- * and the system is overloaded whenever at least one resource is overloaded.
92
- * The class is used by the {@link AutoscaledPool} class.
95
+ * Reads the overload verdict of every signal the {@link Snapshotter}'s built-in four plus any custom ones — and
96
+ * combines them into a {@link SystemInfo}: each signal is a time-weighted average of its snapshots, and the system
97
+ * is overloaded whenever at least one of them is.
93
98
  *
94
- * {@link SystemStatus.getCurrentStatus}
95
- * returns a boolean that represents the current status of the system.
96
- * The length of the current timeframe in seconds is configurable
97
- * by the `currentHistorySecs` option and represents the max age
98
- * of snapshots to be considered for the calculation.
99
+ * Evaluated over two windows, both requested explicitly from every signal so that a signal's private retention cannot
100
+ * widen what it contributes: a short `currentHistorySecs` one ({@link SystemStatus.getCurrentStatus}, gating task
101
+ * dispatch) and a longer `historySecs` one ({@link SystemStatus.getHistoricalStatus}, driving autoscaling).
99
102
  *
100
- * {@link SystemStatus.getHistoricalStatus}
101
- * returns a boolean that represents the long-term status
102
- * of the system. It considers the full snapshot history available
103
- * in the {@link Snapshotter} instance.
104
- * @category Scaling
103
+ * An implementation detail of the {@link ConcurrencySystem}, configured through
104
+ * {@link ConcurrencySystemOptions}.
105
+ * @internal
105
106
  */
106
107
  export declare class SystemStatus {
107
108
  private readonly currentHistoryMillis;
108
- private readonly maxMemoryOverloadedRatio;
109
- private readonly maxEventLoopOverloadedRatio;
110
- private readonly maxCpuOverloadedRatio;
111
- private readonly maxClientOverloadedRatio;
112
- private readonly snapshotter;
113
- constructor(options?: SystemStatusOptions);
109
+ private readonly historyMillis;
110
+ private readonly signals;
111
+ constructor(options: SystemStatusOptions);
112
+ /**
113
+ * The widest window any signal will be queried with, and therefore exactly how much history the signals are asked
114
+ * to retain when they start. Derived here, where the windows are resolved, so nothing has to reapply their
115
+ * defaults.
116
+ */
117
+ get maxSampleWindowMillis(): number;
118
+ /**
119
+ * Signal names are the keys of the reported {@link SystemInfo}, so a duplicate would leave a status object that
120
+ * contradicts actual behavior: both signals are still evaluated (any overloaded one holds concurrency down), but
121
+ * only the last is reported.
122
+ */
123
+ private assertUniqueSignalNames;
114
124
  /**
115
125
  * Returns an {@link SystemInfo} object with the following structure:
116
126
  *
@@ -140,42 +150,12 @@ export declare class SystemStatus {
140
150
  * }
141
151
  * ```
142
152
  *
143
- * Where the `isSystemIdle` property is set to `false` if the system
144
- * has been overloaded in the full history of the {@link Snapshotter}
145
- * (which is configurable in the {@link Snapshotter}) and `true` otherwise.
153
+ * Where the `isSystemIdle` property is set to `false` if the system has been overloaded within the last
154
+ * `historySecs` seconds and `true` otherwise.
146
155
  */
147
156
  getHistoricalStatus(): SystemInfo;
148
157
  /**
149
158
  * Returns a system status object.
150
159
  */
151
- protected _isSystemIdle(sampleDurationMillis?: number): SystemInfo;
152
- /**
153
- * Returns an object with an isOverloaded property set to true
154
- * if the memory has been overloaded in the last sampleDurationMillis.
155
- */
156
- protected _isMemoryOverloaded(sampleDurationMillis?: number): ClientInfo;
157
- /**
158
- * Returns an object with an isOverloaded property set to true
159
- * if the event loop has been overloaded in the last sampleDurationMillis.
160
- */
161
- protected _isEventLoopOverloaded(sampleDurationMillis?: number): ClientInfo;
162
- /**
163
- * Returns an object with an isOverloaded property set to true
164
- * if the CPU has been overloaded in the last sampleDurationMillis.
165
- */
166
- protected _isCpuOverloaded(sampleDurationMillis?: number): ClientInfo;
167
- /**
168
- * Returns an object with an isOverloaded property set to true
169
- * if the client has been overloaded in the last sampleDurationMillis.
170
- */
171
- protected _isClientOverloaded(sampleDurationMillis?: number): ClientInfo;
172
- /**
173
- * Returns an object with sample information and an isOverloaded property
174
- * set to true if at least the ratio of snapshots in the sample are overloaded.
175
- */
176
- protected _isSampleOverloaded<T extends {
177
- createdAt: Date;
178
- isOverloaded: boolean;
179
- }>(sample: T[], ratio: number): ClientInfo;
160
+ private isSystemIdle;
180
161
  }
181
- //# sourceMappingURL=system_status.d.ts.map
@@ -1,52 +1,72 @@
1
- import { weightedAvg } from '@crawlee/utils';
2
- import ow from 'ow';
3
- import { Snapshotter } from './snapshotter.js';
1
+ import { evaluateLoadSignalSample } from './load_signal.js';
4
2
  /**
5
- * Provides a simple interface to reading system status from a {@link Snapshotter} instance.
6
- * It only exposes two functions {@link SystemStatus.getCurrentStatus}
7
- * and {@link SystemStatus.getHistoricalStatus}.
8
- * The system status is calculated using a weighted average of overloaded
9
- * messages in the snapshots, with the weights being the time intervals
10
- * between the snapshots. Each resource is calculated separately
11
- * and the system is overloaded whenever at least one resource is overloaded.
12
- * The class is used by the {@link AutoscaledPool} class.
3
+ * How far back the *current* system status looks by default the window that gates task dispatch.
4
+ * @internal
5
+ */
6
+ export const DEFAULT_CURRENT_HISTORY_SECS = 5;
7
+ /**
8
+ * How far back the *historical* system status looks by default — the window autoscaling decisions are based on, and
9
+ * therefore how much history the signals are asked to retain.
10
+ * @internal
11
+ */
12
+ export const DEFAULT_SNAPSHOT_HISTORY_SECS = 30;
13
+ /** The four built-in signal names that map to typed `SystemInfo` fields, and the option that switches each off. */
14
+ const BUILTIN_SIGNAL_OPTION_KEYS = {
15
+ memInfo: 'memory',
16
+ eventLoopInfo: 'eventLoop',
17
+ cpuInfo: 'cpu',
18
+ clientInfo: 'client',
19
+ };
20
+ const BUILTIN_SIGNAL_NAMES = new Set(Object.keys(BUILTIN_SIGNAL_OPTION_KEYS));
21
+ /**
22
+ * Reads the overload verdict of every signal — the {@link Snapshotter}'s built-in four plus any custom ones — and
23
+ * combines them into a {@link SystemInfo}: each signal is a time-weighted average of its snapshots, and the system
24
+ * is overloaded whenever at least one of them is.
13
25
  *
14
- * {@link SystemStatus.getCurrentStatus}
15
- * returns a boolean that represents the current status of the system.
16
- * The length of the current timeframe in seconds is configurable
17
- * by the `currentHistorySecs` option and represents the max age
18
- * of snapshots to be considered for the calculation.
26
+ * Evaluated over two windows, both requested explicitly from every signal so that a signal's private retention cannot
27
+ * widen what it contributes: a short `currentHistorySecs` one ({@link SystemStatus.getCurrentStatus}, gating task
28
+ * dispatch) and a longer `historySecs` one ({@link SystemStatus.getHistoricalStatus}, driving autoscaling).
19
29
  *
20
- * {@link SystemStatus.getHistoricalStatus}
21
- * returns a boolean that represents the long-term status
22
- * of the system. It considers the full snapshot history available
23
- * in the {@link Snapshotter} instance.
24
- * @category Scaling
30
+ * An implementation detail of the {@link ConcurrencySystem}, configured through
31
+ * {@link ConcurrencySystemOptions}.
32
+ * @internal
25
33
  */
26
34
  export class SystemStatus {
27
35
  currentHistoryMillis;
28
- maxMemoryOverloadedRatio;
29
- maxEventLoopOverloadedRatio;
30
- maxCpuOverloadedRatio;
31
- maxClientOverloadedRatio;
32
- snapshotter;
33
- constructor(options = {}) {
34
- ow(options, ow.object.exactShape({
35
- currentHistorySecs: ow.optional.number,
36
- maxMemoryOverloadedRatio: ow.optional.number,
37
- maxEventLoopOverloadedRatio: ow.optional.number,
38
- maxCpuOverloadedRatio: ow.optional.number,
39
- maxClientOverloadedRatio: ow.optional.number,
40
- snapshotter: ow.optional.object,
41
- config: ow.optional.object,
42
- }));
43
- const { currentHistorySecs = 5, maxMemoryOverloadedRatio = 0.2, maxEventLoopOverloadedRatio = 0.6, maxCpuOverloadedRatio = 0.4, maxClientOverloadedRatio = 0.3, snapshotter, config, } = options;
36
+ historyMillis;
37
+ signals;
38
+ constructor(options) {
39
+ const { currentHistorySecs = DEFAULT_CURRENT_HISTORY_SECS, historySecs = DEFAULT_SNAPSHOT_HISTORY_SECS, snapshotter, loadSignals = [], } = options;
44
40
  this.currentHistoryMillis = currentHistorySecs * 1000;
45
- this.maxMemoryOverloadedRatio = maxMemoryOverloadedRatio;
46
- this.maxEventLoopOverloadedRatio = maxEventLoopOverloadedRatio;
47
- this.maxCpuOverloadedRatio = maxCpuOverloadedRatio;
48
- this.maxClientOverloadedRatio = maxClientOverloadedRatio;
49
- this.snapshotter = snapshotter || new Snapshotter({ config });
41
+ this.historyMillis = historySecs * 1000;
42
+ this.signals = [...snapshotter.getLoadSignals(), ...loadSignals];
43
+ this.assertUniqueSignalNames();
44
+ }
45
+ /**
46
+ * The widest window any signal will be queried with, and therefore exactly how much history the signals are asked
47
+ * to retain when they start. Derived here, where the windows are resolved, so nothing has to reapply their
48
+ * defaults.
49
+ */
50
+ get maxSampleWindowMillis() {
51
+ return Math.max(this.currentHistoryMillis, this.historyMillis);
52
+ }
53
+ /**
54
+ * Signal names are the keys of the reported {@link SystemInfo}, so a duplicate would leave a status object that
55
+ * contradicts actual behavior: both signals are still evaluated (any overloaded one holds concurrency down), but
56
+ * only the last is reported.
57
+ */
58
+ assertUniqueSignalNames() {
59
+ const seen = new Set();
60
+ for (const { name } of this.signals) {
61
+ if (!seen.has(name)) {
62
+ seen.add(name);
63
+ continue;
64
+ }
65
+ const hint = BUILTIN_SIGNAL_NAMES.has(name)
66
+ ? `it is the name of a built-in signal. To replace that signal, switch it off with \`loadSignals: { ${BUILTIN_SIGNAL_OPTION_KEYS[name]}: false }\` and keep your implementation in \`loadSignals.custom\`; to run yours alongside it, give it a different name.`
67
+ : 'two custom signals cannot share a name - rename one of them.';
68
+ throw new Error(`Duplicate load signal name ${JSON.stringify(name)}: ${hint}`);
69
+ }
50
70
  }
51
71
  /**
52
72
  * Returns an {@link SystemInfo} object with the following structure:
@@ -65,7 +85,7 @@ export class SystemStatus {
65
85
  * and `true` otherwise.
66
86
  */
67
87
  getCurrentStatus() {
68
- return this._isSystemIdle(this.currentHistoryMillis);
88
+ return this.isSystemIdle(this.currentHistoryMillis);
69
89
  }
70
90
  /**
71
91
  * Returns an {@link SystemInfo} object with the following structure:
@@ -79,91 +99,41 @@ export class SystemStatus {
79
99
  * }
80
100
  * ```
81
101
  *
82
- * Where the `isSystemIdle` property is set to `false` if the system
83
- * has been overloaded in the full history of the {@link Snapshotter}
84
- * (which is configurable in the {@link Snapshotter}) and `true` otherwise.
102
+ * Where the `isSystemIdle` property is set to `false` if the system has been overloaded within the last
103
+ * `historySecs` seconds and `true` otherwise.
85
104
  */
86
105
  getHistoricalStatus() {
87
- return this._isSystemIdle();
106
+ return this.isSystemIdle(this.historyMillis);
88
107
  }
89
108
  /**
90
109
  * Returns a system status object.
91
110
  */
92
- _isSystemIdle(sampleDurationMillis) {
93
- const memInfo = this._isMemoryOverloaded(sampleDurationMillis);
94
- const eventLoopInfo = this._isEventLoopOverloaded(sampleDurationMillis);
95
- const cpuInfo = this._isCpuOverloaded(sampleDurationMillis);
96
- const clientInfo = this._isClientOverloaded(sampleDurationMillis);
97
- return {
98
- isSystemIdle: !memInfo.isOverloaded &&
99
- !eventLoopInfo.isOverloaded &&
100
- !cpuInfo.isOverloaded &&
101
- !clientInfo.isOverloaded,
102
- memInfo,
103
- eventLoopInfo,
104
- cpuInfo,
105
- clientInfo,
111
+ isSystemIdle(sampleDurationMillis) {
112
+ const result = {
113
+ isSystemIdle: true,
114
+ memInfo: { isOverloaded: false, limitRatio: 0, actualRatio: 0 },
115
+ eventLoopInfo: { isOverloaded: false, limitRatio: 0, actualRatio: 0 },
116
+ cpuInfo: { isOverloaded: false, limitRatio: 0, actualRatio: 0 },
117
+ clientInfo: { isOverloaded: false, limitRatio: 0, actualRatio: 0 },
106
118
  };
107
- }
108
- /**
109
- * Returns an object with an isOverloaded property set to true
110
- * if the memory has been overloaded in the last sampleDurationMillis.
111
- */
112
- _isMemoryOverloaded(sampleDurationMillis) {
113
- const sample = this.snapshotter.getMemorySample(sampleDurationMillis);
114
- return this._isSampleOverloaded(sample, this.maxMemoryOverloadedRatio);
115
- }
116
- /**
117
- * Returns an object with an isOverloaded property set to true
118
- * if the event loop has been overloaded in the last sampleDurationMillis.
119
- */
120
- _isEventLoopOverloaded(sampleDurationMillis) {
121
- const sample = this.snapshotter.getEventLoopSample(sampleDurationMillis);
122
- return this._isSampleOverloaded(sample, this.maxEventLoopOverloadedRatio);
123
- }
124
- /**
125
- * Returns an object with an isOverloaded property set to true
126
- * if the CPU has been overloaded in the last sampleDurationMillis.
127
- */
128
- _isCpuOverloaded(sampleDurationMillis) {
129
- const sample = this.snapshotter.getCpuSample(sampleDurationMillis);
130
- return this._isSampleOverloaded(sample, this.maxCpuOverloadedRatio);
131
- }
132
- /**
133
- * Returns an object with an isOverloaded property set to true
134
- * if the client has been overloaded in the last sampleDurationMillis.
135
- */
136
- _isClientOverloaded(sampleDurationMillis) {
137
- const sample = this.snapshotter.getClientSample(sampleDurationMillis);
138
- return this._isSampleOverloaded(sample, this.maxClientOverloadedRatio);
139
- }
140
- /**
141
- * Returns an object with sample information and an isOverloaded property
142
- * set to true if at least the ratio of snapshots in the sample are overloaded.
143
- */
144
- _isSampleOverloaded(sample, ratio) {
145
- if (sample.length === 0) {
146
- return {
147
- isOverloaded: false,
148
- limitRatio: ratio,
149
- actualRatio: 0,
150
- };
119
+ let loadSignalInfo;
120
+ for (const signal of this.signals) {
121
+ const sample = signal.getSample(sampleDurationMillis);
122
+ const info = evaluateLoadSignalSample(sample, signal.overloadedRatio);
123
+ if (info.isOverloaded) {
124
+ result.isSystemIdle = false;
125
+ }
126
+ if (BUILTIN_SIGNAL_NAMES.has(signal.name)) {
127
+ result[signal.name] = info;
128
+ }
129
+ else {
130
+ loadSignalInfo ??= {};
131
+ loadSignalInfo[signal.name] = info;
132
+ }
151
133
  }
152
- const weights = [];
153
- const values = [];
154
- for (let i = 1; i < sample.length; i++) {
155
- const previous = sample[i - 1];
156
- const current = sample[i];
157
- const weight = +current.createdAt - +previous.createdAt;
158
- weights.push(weight || 1); // Prevent errors from 0ms long intervals (sync) between snapshots.
159
- values.push(+current.isOverloaded);
134
+ if (loadSignalInfo) {
135
+ result.loadSignalInfo = loadSignalInfo;
160
136
  }
161
- const wAvg = sample.length === 1 ? +sample[0].isOverloaded : weightedAvg(values, weights);
162
- return {
163
- isOverloaded: wAvg > ratio,
164
- limitRatio: ratio,
165
- actualRatio: Math.round(wAvg * 1000) / 1000,
166
- };
137
+ return result;
167
138
  }
168
139
  }
169
- //# sourceMappingURL=system_status.js.map