@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.
- package/README.md +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +165 -320
- package/autoscaling/client_load_signal.d.ts +55 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +351 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +47 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +104 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/system_status.d.ts +62 -84
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +22 -13
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -83
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +2 -5
- package/crawlers/error_snapshotter.js +7 -8
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +143 -59
- package/crawlers/statistics.js +243 -153
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +59 -68
- package/enqueue_links/enqueue_links.js +57 -62
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +40 -27
- package/enqueue_links/shared.js +90 -68
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +10 -12
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/http.d.ts +9 -0
- package/http.js +28 -0
- package/index.d.ts +7 -4
- package/index.js +6 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +38 -0
- package/memory-storage/memory-storage.js +130 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +114 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +204 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +422 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +13 -12
- package/proxy_configuration.d.ts +24 -132
- package/proxy_configuration.js +24 -143
- package/recoverable_state.d.ts +140 -0
- package/recoverable_state.js +212 -0
- package/request.d.ts +86 -17
- package/request.js +120 -41
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +9 -11
- package/service_locator.d.ts +165 -0
- package/service_locator.js +253 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +35 -89
- package/session_pool/session.js +82 -142
- package/session_pool/session_pool.d.ts +69 -90
- package/session_pool/session_pool.js +151 -150
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +285 -144
- package/storages/index.d.ts +10 -8
- package/storages/index.js +8 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +424 -151
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -116
- package/storages/request_list.js +159 -133
- package/storages/request_loader.d.ts +101 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +757 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +137 -143
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/throttling_request_manager.d.ts +216 -0
- package/storages/throttling_request_manager.js +453 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +58 -11
- package/storages/utils.js +64 -13
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +8 -1
- package/validators.js +10 -3
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LoadSignal, LoadSignalStartContext, LoadSnapshot } from './load_signal.js';
|
|
2
|
+
/**
|
|
3
|
+
* A snapshot produced by the built-in client (rate-limit) signal.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export interface ClientSnapshot extends LoadSnapshot {
|
|
7
|
+
rateLimitErrorCount: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Tuning for the built-in **client** (rate-limit) load signal, as accepted both by {@link ClientLoadSignal} and by
|
|
11
|
+
* the {@link LoadSignalsOptions.client|`client`} shorthand on {@link LoadSignalsOptions}.
|
|
12
|
+
*/
|
|
13
|
+
export interface ClientLoadSignalOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Defines the interval of checking the current state of the remote API client, in seconds.
|
|
16
|
+
* @default 1
|
|
17
|
+
*/
|
|
18
|
+
snapshotIntervalSecs?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Defines the maximum number of new rate limit errors within the given interval.
|
|
21
|
+
* @default 3
|
|
22
|
+
*/
|
|
23
|
+
maxErrors?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Maximum ratio of overloaded snapshots in a sample before the client counts as overloaded.
|
|
26
|
+
* @default 0.3
|
|
27
|
+
*/
|
|
28
|
+
overloadedRatio?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Periodically checks the storage backend for rate-limit errors (HTTP 429) and reports overload when the error delta
|
|
32
|
+
* exceeds a threshold.
|
|
33
|
+
*
|
|
34
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
35
|
+
*
|
|
36
|
+
* Switch it off entirely ({@link LoadSignalsOptions.client|`client: false`}) if the storage backend reports no
|
|
37
|
+
* rate-limit statistics, since it otherwise polls it every second to no purpose.
|
|
38
|
+
*
|
|
39
|
+
* @category Scaling
|
|
40
|
+
*/
|
|
41
|
+
export declare class ClientLoadSignal implements LoadSignal {
|
|
42
|
+
#private;
|
|
43
|
+
readonly name = "clientInfo";
|
|
44
|
+
readonly overloadedRatio: number;
|
|
45
|
+
constructor(options?: ClientLoadSignalOptions);
|
|
46
|
+
start(context: LoadSignalStartContext): Promise<void>;
|
|
47
|
+
stop(): Promise<void>;
|
|
48
|
+
getSample(sampleDurationMillis?: number): LoadSnapshot[];
|
|
49
|
+
/**
|
|
50
|
+
* Records one snapshot, overloaded when rate-limit errors grew by more than the configured limit since the
|
|
51
|
+
* previous one.
|
|
52
|
+
* @internal Also lets tests drive the measurement without waiting on a timer.
|
|
53
|
+
*/
|
|
54
|
+
handle(intervalCallback: () => unknown): void;
|
|
55
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { betterClearInterval, betterSetInterval } from '@apify/utilities';
|
|
2
|
+
import { serviceLocator } from '../service_locator.js';
|
|
3
|
+
import { SnapshotStore } from './load_signal.js';
|
|
4
|
+
const CLIENT_RATE_LIMIT_ERROR_RETRY_COUNT = 2;
|
|
5
|
+
/**
|
|
6
|
+
* Periodically checks the storage backend for rate-limit errors (HTTP 429) and reports overload when the error delta
|
|
7
|
+
* exceeds a threshold.
|
|
8
|
+
*
|
|
9
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
10
|
+
*
|
|
11
|
+
* Switch it off entirely ({@link LoadSignalsOptions.client|`client: false`}) if the storage backend reports no
|
|
12
|
+
* rate-limit statistics, since it otherwise polls it every second to no purpose.
|
|
13
|
+
*
|
|
14
|
+
* @category Scaling
|
|
15
|
+
*/
|
|
16
|
+
export class ClientLoadSignal {
|
|
17
|
+
name = 'clientInfo';
|
|
18
|
+
overloadedRatio;
|
|
19
|
+
#store = new SnapshotStore();
|
|
20
|
+
#intervalMillis;
|
|
21
|
+
#maxErrors;
|
|
22
|
+
#interval;
|
|
23
|
+
#client;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
this.overloadedRatio = options.overloadedRatio ?? 0.3;
|
|
26
|
+
this.#intervalMillis = (options.snapshotIntervalSecs ?? 1) * 1000;
|
|
27
|
+
this.#maxErrors = options.maxErrors ?? 3;
|
|
28
|
+
this.handle = this.handle.bind(this);
|
|
29
|
+
}
|
|
30
|
+
async start(context) {
|
|
31
|
+
this.#store.useSampleWindow(context.maxSampleWindowMillis);
|
|
32
|
+
// A new session starts from a clean slate, or its first measurement diffs the error count against the previous
|
|
33
|
+
// session's — possibly against a different backend, since the client is resolved afresh just below.
|
|
34
|
+
this.#store.clear();
|
|
35
|
+
// Resolved here rather than in the constructor, where asking for the backend would instantiate a default one
|
|
36
|
+
// as a side effect - long before the crawler that owns the run has had a chance to register its own.
|
|
37
|
+
this.#client = serviceLocator.getStorageBackend();
|
|
38
|
+
this.#interval = betterSetInterval(this.handle, this.#intervalMillis);
|
|
39
|
+
}
|
|
40
|
+
async stop() {
|
|
41
|
+
if (this.#interval)
|
|
42
|
+
betterClearInterval(this.#interval);
|
|
43
|
+
this.#interval = undefined;
|
|
44
|
+
this.#client = undefined;
|
|
45
|
+
}
|
|
46
|
+
getSample(sampleDurationMillis) {
|
|
47
|
+
return this.#store.getSample(sampleDurationMillis);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Records one snapshot, overloaded when rate-limit errors grew by more than the configured limit since the
|
|
51
|
+
* previous one.
|
|
52
|
+
* @internal Also lets tests drive the measurement without waiting on a timer.
|
|
53
|
+
*/
|
|
54
|
+
handle(intervalCallback) {
|
|
55
|
+
const now = new Date();
|
|
56
|
+
const allErrorCounts = this.#client?.stats?.rateLimitErrors ?? [];
|
|
57
|
+
const currentErrCount = allErrorCounts[CLIENT_RATE_LIMIT_ERROR_RETRY_COUNT] || 0;
|
|
58
|
+
const snapshot = {
|
|
59
|
+
createdAt: now,
|
|
60
|
+
isOverloaded: false,
|
|
61
|
+
rateLimitErrorCount: currentErrCount,
|
|
62
|
+
};
|
|
63
|
+
const all = this.#store.getAll();
|
|
64
|
+
const previousSnapshot = all[all.length - 1];
|
|
65
|
+
if (previousSnapshot) {
|
|
66
|
+
const delta = currentErrCount - previousSnapshot.rateLimitErrorCount;
|
|
67
|
+
if (delta > this.#maxErrors)
|
|
68
|
+
snapshot.isOverloaded = true;
|
|
69
|
+
}
|
|
70
|
+
this.#store.push(snapshot, now);
|
|
71
|
+
intervalCallback();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import type { CrawleeLogger } from '../log.js';
|
|
2
|
+
import type { LoadSignalsOptions } from './snapshotter.js';
|
|
3
|
+
import type { SystemInfo } from './system_status.js';
|
|
4
|
+
export interface ConcurrencySystemOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The minimum number of tasks running in parallel.
|
|
7
|
+
*
|
|
8
|
+
* *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.
|
|
9
|
+
* If you're not sure, just keep the default value and the concurrency will scale up automatically.
|
|
10
|
+
* @default 1
|
|
11
|
+
*/
|
|
12
|
+
minConcurrency?: number;
|
|
13
|
+
/**
|
|
14
|
+
* The maximum number of tasks running in parallel.
|
|
15
|
+
* @default 200
|
|
16
|
+
*/
|
|
17
|
+
maxConcurrency?: number;
|
|
18
|
+
/**
|
|
19
|
+
* The desired number of tasks that should be running parallel on the start of the pool,
|
|
20
|
+
* if there is a large enough supply of them.
|
|
21
|
+
* By default, it is `minConcurrency`.
|
|
22
|
+
*/
|
|
23
|
+
desiredConcurrency?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Minimum level of desired concurrency to reach before more scaling up is allowed.
|
|
26
|
+
* @default 0.90
|
|
27
|
+
*/
|
|
28
|
+
desiredConcurrencyRatio?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Defines the fractional amount of desired concurrency to be added with each scaling up.
|
|
31
|
+
* The minimum scaling step is one.
|
|
32
|
+
* @default 0.05
|
|
33
|
+
*/
|
|
34
|
+
scaleUpStepRatio?: number;
|
|
35
|
+
/**
|
|
36
|
+
* Defines the amount of desired concurrency to be subtracted with each scaling down.
|
|
37
|
+
* The minimum scaling step is one.
|
|
38
|
+
* @default 0.05
|
|
39
|
+
*/
|
|
40
|
+
scaleDownStepRatio?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Specifies a period in which the instance logs its state, in seconds.
|
|
43
|
+
* Set to `null` to disable periodic logging.
|
|
44
|
+
* @default 60
|
|
45
|
+
*/
|
|
46
|
+
loggingIntervalSecs?: number | null;
|
|
47
|
+
/**
|
|
48
|
+
* Defines in seconds how often the system should attempt to adjust the desired concurrency
|
|
49
|
+
* based on the latest system status. Setting it lower than 1 might have a severe impact on performance.
|
|
50
|
+
* We suggest using a value from 5 to 20.
|
|
51
|
+
* @default 10
|
|
52
|
+
*/
|
|
53
|
+
autoscaleIntervalSecs?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The signals that tell the system whether the machine is overloaded: per-resource tuning for the built-in four
|
|
56
|
+
* (memory, event loop, CPU, client) plus any {@link LoadSignalsOptions.custom|`custom`} implementations of
|
|
57
|
+
* your own. See {@link LoadSignalsOptions}.
|
|
58
|
+
*/
|
|
59
|
+
loadSignals?: LoadSignalsOptions;
|
|
60
|
+
/**
|
|
61
|
+
* How far back the **autoscaling** decisions look, in seconds — the window the historical system status is
|
|
62
|
+
* evaluated over, and therefore how much history the signals retain (the memory cost of raising it).
|
|
63
|
+
* @default 30
|
|
64
|
+
*/
|
|
65
|
+
snapshotHistorySecs?: number;
|
|
66
|
+
/**
|
|
67
|
+
* How far back the **task-gating** decision looks, in seconds — the window used to judge whether the system is
|
|
68
|
+
* overloaded *right now*, before dispatching one more task. Deliberately shorter than
|
|
69
|
+
* {@link ConcurrencySystemOptions.snapshotHistorySecs|`snapshotHistorySecs`}, so that dispatch reacts to
|
|
70
|
+
* spikes quickly while scaling stays stable.
|
|
71
|
+
* @default 5
|
|
72
|
+
*/
|
|
73
|
+
currentHistorySecs?: number;
|
|
74
|
+
/**
|
|
75
|
+
* The maximum number of tasks per minute the system can run.
|
|
76
|
+
* By default, this is set to `Infinity`, but you can pass any positive, non-zero integer.
|
|
77
|
+
*/
|
|
78
|
+
maxTasksPerMinute?: number;
|
|
79
|
+
log?: CrawleeLogger;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Identifies *who* is asking a governor for capacity: one {@link AutoscaledPool}, or the crawler driving it. The
|
|
83
|
+
* same object is passed on every call a pool makes, so per-consumer state can be keyed off it or off its `id`.
|
|
84
|
+
* @category Scaling
|
|
85
|
+
*/
|
|
86
|
+
export interface ConcurrencyConsumer {
|
|
87
|
+
/** Process-unique and human-readable — a crawler's is its {@link BasicCrawlerOptions.id|`id`} option. */
|
|
88
|
+
readonly id: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The contract between an {@link AutoscaledPool} and its concurrency "governor" — the object that answers *is
|
|
92
|
+
* there free compute for one more task?* and tracks the budget that tasks are booked against.
|
|
93
|
+
* {@link ConcurrencySystem} is the canonical implementation; the interface lets alternate governors be substituted
|
|
94
|
+
* without depending on its internals.
|
|
95
|
+
*
|
|
96
|
+
* Every allocation method is told which {@link ConcurrencyConsumer|consumer} is asking, so an implementation can
|
|
97
|
+
* allocate per consumer. {@link ConcurrencySystem} does not: it serves whoever asks first, which can starve a pool
|
|
98
|
+
* that joins a saturated system late.
|
|
99
|
+
* @category Scaling
|
|
100
|
+
*/
|
|
101
|
+
export interface IConcurrencySystem {
|
|
102
|
+
/**
|
|
103
|
+
* The number of tasks that should currently be running in parallel, assuming a sufficient supply of them. How it
|
|
104
|
+
* is derived is up to the implementation, hence read-only here — but it must always be at least `1`, or a pool
|
|
105
|
+
* could never start the first task.
|
|
106
|
+
*/
|
|
107
|
+
readonly desiredConcurrency: number;
|
|
108
|
+
/** The number of parallel tasks currently booked against this governor, regardless of which pool booked them. */
|
|
109
|
+
readonly currentConcurrency: number;
|
|
110
|
+
/**
|
|
111
|
+
* Whether the governor is ready to be booked against. {@link AutoscaledPool.run|`pool.run()`} refuses to run
|
|
112
|
+
* when this is `false`. An implementation with no startup lifecycle simply reports `true`.
|
|
113
|
+
*/
|
|
114
|
+
readonly isRunning: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* May **one more** task start right now, on behalf of `consumer`? A cheap pre-check the pool consults before
|
|
117
|
+
* querying task readiness.
|
|
118
|
+
*
|
|
119
|
+
* Must **not** enforce rate limits that only make sense for ready tasks (e.g. a per-minute task cap): the pool
|
|
120
|
+
* calls this before knowing whether any task is ready, so refusing here would stall an already-empty queue.
|
|
121
|
+
*
|
|
122
|
+
* Must also return `true` whenever `consumer` has nothing in flight of its own. A `false` sends that pool straight
|
|
123
|
+
* to its finished-check **without** consulting `isTaskReadyFunction`, so a governor that starves an idle pool can
|
|
124
|
+
* make its `run()` resolve while work is still pending. Tracking bookings per consumer answers that directly;
|
|
125
|
+
* {@link ConcurrencySystem}, which does not, instead never refuses while
|
|
126
|
+
* {@link IConcurrencySystem.currentConcurrency|`currentConcurrency`} is `0`.
|
|
127
|
+
*/
|
|
128
|
+
hasCapacityForTask(consumer: ConcurrencyConsumer): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Books a task against the budget for `consumer`, returning `false` (without booking) when there is no room — the
|
|
131
|
+
* budget is spent, the consumer is over its share, or an implementation-specific rate limit was reached.
|
|
132
|
+
*
|
|
133
|
+
* Must be an *atomic* (synchronous) check-and-book: several pools may share one governor, and a check separated
|
|
134
|
+
* from the booking by an `await` lets two of them claim the last free slot at once.
|
|
135
|
+
*/
|
|
136
|
+
tryRegisterTaskStart(consumer: ConcurrencyConsumer): boolean;
|
|
137
|
+
/** Returns a task's slot to `consumer`'s budget. Called once the task settles (resolve or reject). */
|
|
138
|
+
registerTaskEnd(consumer: ConcurrencyConsumer): void;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The shareable "governor" behind an {@link AutoscaledPool}: it decides whether there is free compute for one more
|
|
142
|
+
* task by combining live system load (via an internal {@link Snapshotter}) with a concurrency budget it autoscales
|
|
143
|
+
* over time.
|
|
144
|
+
*
|
|
145
|
+
* Sharing one instance between several pools (and therefore several crawlers) caps their *combined* compute, instead
|
|
146
|
+
* of letting each scale independently and oversubscribe the machine.
|
|
147
|
+
*
|
|
148
|
+
* Whoever builds the instance owns its lifecycle: call {@link ConcurrencySystem.start|`start()`} before any
|
|
149
|
+
* borrowing pool runs and {@link ConcurrencySystem.stop|`stop()`} once they are all done (crawlers do this for the
|
|
150
|
+
* default system they build, never for an injected one). Both calls are idempotent, and the first `stop()` tears the
|
|
151
|
+
* system down for every borrower.
|
|
152
|
+
* @category Scaling
|
|
153
|
+
*/
|
|
154
|
+
export declare class ConcurrencySystem implements IConcurrencySystem {
|
|
155
|
+
#private;
|
|
156
|
+
private readonly log;
|
|
157
|
+
private readonly desiredConcurrencyRatio;
|
|
158
|
+
private readonly scaleUpStepRatio;
|
|
159
|
+
private readonly scaleDownStepRatio;
|
|
160
|
+
private readonly maxTasksPerMinute;
|
|
161
|
+
private _currentConcurrency;
|
|
162
|
+
private readonly snapshotter;
|
|
163
|
+
private readonly systemStatus;
|
|
164
|
+
constructor(options?: ConcurrencySystemOptions);
|
|
165
|
+
/**
|
|
166
|
+
* Gets the minimum number of tasks running in parallel.
|
|
167
|
+
*/
|
|
168
|
+
get minConcurrency(): number;
|
|
169
|
+
/**
|
|
170
|
+
* Sets the minimum number of tasks running in parallel.
|
|
171
|
+
*
|
|
172
|
+
* *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.
|
|
173
|
+
* If you're not sure, just keep the default value and the concurrency will scale up automatically.
|
|
174
|
+
*/
|
|
175
|
+
set minConcurrency(value: number);
|
|
176
|
+
/**
|
|
177
|
+
* Gets the maximum number of tasks running in parallel.
|
|
178
|
+
*/
|
|
179
|
+
get maxConcurrency(): number;
|
|
180
|
+
/**
|
|
181
|
+
* Sets the maximum number of tasks running in parallel. Lowering it below the current
|
|
182
|
+
* {@link ConcurrencySystem.desiredConcurrency|`desiredConcurrency`} pulls that down to the new ceiling too, so
|
|
183
|
+
* the change takes effect immediately (in-flight tasks are never cancelled — the budget simply drains to the new
|
|
184
|
+
* limit as they settle).
|
|
185
|
+
*/
|
|
186
|
+
set maxConcurrency(value: number);
|
|
187
|
+
/**
|
|
188
|
+
* Gets the desired concurrency for the system,
|
|
189
|
+
* which is an estimated number of parallel tasks that the system can currently support.
|
|
190
|
+
*/
|
|
191
|
+
get desiredConcurrency(): number;
|
|
192
|
+
/**
|
|
193
|
+
* Sets the desired concurrency for the system, i.e. the number of tasks that should be running
|
|
194
|
+
* in parallel if there's large enough supply of tasks.
|
|
195
|
+
*/
|
|
196
|
+
set desiredConcurrency(value: number);
|
|
197
|
+
/**
|
|
198
|
+
* Re-establishes `minConcurrency <= desiredConcurrency <= maxConcurrency` after any of the three is retuned.
|
|
199
|
+
* Dispatch gates on the desired value alone, so one stranded above `maxConcurrency` would make the ceiling
|
|
200
|
+
* meaningless. A contradictory pair (`minConcurrency > maxConcurrency`) resolves in favour of the maximum, since
|
|
201
|
+
* that is the limit callers set in order to protect something.
|
|
202
|
+
*/
|
|
203
|
+
private clampDesiredConcurrency;
|
|
204
|
+
get currentConcurrency(): number;
|
|
205
|
+
/** Whether the system is currently monitoring load and autoscaling the budget. */
|
|
206
|
+
get isRunning(): boolean;
|
|
207
|
+
/**
|
|
208
|
+
* Boots the underlying snapshotter and the autoscaling interval. Idempotent, so a shared system isn't restarted
|
|
209
|
+
* when handed to another consumer; concurrent callers await one startup. Rejects, leaving nothing running, if a
|
|
210
|
+
* signal fails to start.
|
|
211
|
+
*/
|
|
212
|
+
start(): Promise<void>;
|
|
213
|
+
private boot;
|
|
214
|
+
/**
|
|
215
|
+
* Stops the snapshotter and intervals. Idempotent and safe to call even if the system was never started.
|
|
216
|
+
*/
|
|
217
|
+
stop(): Promise<void>;
|
|
218
|
+
private shutDown;
|
|
219
|
+
/**
|
|
220
|
+
* Reports, once per session, that capacity is being queried on a system that isn't running — a mistake nothing
|
|
221
|
+
* else catches, since {@link AutoscaledPool.run|`pool.run()`} only checks
|
|
222
|
+
* {@link ConcurrencySystem.isRunning|`isRunning`} on the way in. Both the overload verdict and
|
|
223
|
+
* `desiredConcurrency` are frozen at that point, so the borrowing pool would otherwise just quietly mis-scale.
|
|
224
|
+
*/
|
|
225
|
+
private warnIfNotRunning;
|
|
226
|
+
/**
|
|
227
|
+
* May **one more** task start right now? Returns `false` when the shared budget is spent (desired concurrency
|
|
228
|
+
* reached) or when the machine is overloaded past `minConcurrency`.
|
|
229
|
+
*
|
|
230
|
+
* One budget for the whole machine, so the asking consumer is ignored — and therefore optional here, unlike in the
|
|
231
|
+
* interface, letting the answer be queried directly.
|
|
232
|
+
*/
|
|
233
|
+
hasCapacityForTask(_consumer?: ConcurrencyConsumer): boolean;
|
|
234
|
+
/** Whether the per-minute task cap has been reached. */
|
|
235
|
+
private get isOverMaxRequestLimit();
|
|
236
|
+
/**
|
|
237
|
+
* Atomically books a task against the shared budget: re-checks
|
|
238
|
+
* {@link ConcurrencySystem.hasCapacityForTask|`hasCapacityForTask()`} plus the per-minute task cap and
|
|
239
|
+
* increments the current concurrency in one synchronous step, returning `false` (without booking) when there is no
|
|
240
|
+
* room. Call right before the task actually runs.
|
|
241
|
+
*
|
|
242
|
+
* The cap is enforced here rather than in the pre-check so that an empty queue never blocks the pool for a whole
|
|
243
|
+
* extra minute.
|
|
244
|
+
*/
|
|
245
|
+
tryRegisterTaskStart(consumer?: ConcurrencyConsumer): boolean;
|
|
246
|
+
/** Returns a slot to the shared budget, whoever booked it. */
|
|
247
|
+
registerTaskEnd(_consumer?: ConcurrencyConsumer): void;
|
|
248
|
+
/**
|
|
249
|
+
* What the system currently makes of the machine: the per-signal overload verdicts, evaluated over the
|
|
250
|
+
* task-gating window, exactly as {@link ConcurrencySystem.hasCapacityForTask|`hasCapacityForTask()`} sees them.
|
|
251
|
+
* The one public window into load monitoring — useful for answering *why* a crawl is not scaling up.
|
|
252
|
+
*/
|
|
253
|
+
getCurrentStatus(): SystemInfo;
|
|
254
|
+
/**
|
|
255
|
+
* Evaluates the historical system status and scales the shared desired concurrency up or down accordingly. Driven
|
|
256
|
+
* by the autoscaling interval started in {@link ConcurrencySystem.start|`start()`}.
|
|
257
|
+
*/
|
|
258
|
+
private autoscale;
|
|
259
|
+
/**
|
|
260
|
+
* Scales the system up by increasing the desired concurrency by the scaleUpStepRatio.
|
|
261
|
+
*/
|
|
262
|
+
private scaleUp;
|
|
263
|
+
/**
|
|
264
|
+
* Scales the system down by decreasing the desired concurrency by the scaleDownStepRatio.
|
|
265
|
+
*/
|
|
266
|
+
private scaleDown;
|
|
267
|
+
private incrementTasksDonePerSecond;
|
|
268
|
+
}
|