@gravito/core 1.6.1 → 2.0.1
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 +100 -6
- package/README.zh-TW.md +101 -6
- package/dist/Application.d.ts +256 -0
- package/dist/CommandKernel.d.ts +33 -0
- package/dist/ConfigManager.d.ts +65 -0
- package/dist/Container/RequestScopeManager.d.ts +62 -0
- package/dist/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/Container.d.ts +153 -0
- package/dist/ErrorHandler.d.ts +66 -0
- package/dist/Event.d.ts +5 -0
- package/dist/EventManager.d.ts +123 -0
- package/dist/GlobalErrorHandlers.d.ts +47 -0
- package/dist/GravitoServer.d.ts +28 -0
- package/dist/HookManager.d.ts +435 -0
- package/dist/Listener.d.ts +4 -0
- package/dist/Logger.d.ts +20 -0
- package/dist/PlanetCore.d.ts +402 -0
- package/dist/RequestContext.d.ts +97 -0
- package/dist/Route.d.ts +36 -0
- package/dist/Router.d.ts +270 -0
- package/dist/ServiceProvider.d.ts +178 -0
- package/dist/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/adapters/bun/AdaptiveAdapter.d.ts +99 -0
- package/dist/adapters/bun/BunContext.d.ts +54 -0
- package/dist/adapters/bun/BunNativeAdapter.d.ts +66 -0
- package/dist/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/adapters/bun/RadixRouter.d.ts +32 -0
- package/dist/adapters/bun/index.d.ts +7 -0
- package/dist/adapters/bun/types.d.ts +20 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/types.d.ts +235 -0
- package/dist/binary/BinaryUtils.d.ts +105 -0
- package/dist/binary/index.d.ts +5 -0
- package/dist/cli/queue-commands.d.ts +6 -0
- package/dist/compat/async-local-storage.browser.d.ts +9 -0
- package/dist/compat/async-local-storage.d.ts +7 -0
- package/dist/compat/crypto.browser.d.ts +5 -0
- package/dist/compat/crypto.d.ts +6 -0
- package/dist/compat.d.ts +23 -1
- package/dist/compat.js +3 -0
- package/dist/compat.js.map +9 -0
- package/dist/engine/AOTRouter.d.ts +139 -0
- package/dist/engine/FastContext.d.ts +141 -0
- package/dist/engine/Gravito.d.ts +131 -0
- package/dist/engine/MinimalContext.d.ts +102 -0
- package/dist/engine/analyzer.d.ts +113 -0
- package/dist/engine/constants.d.ts +23 -0
- package/dist/engine/index.d.ts +14 -910
- package/dist/engine/index.js +623 -622
- package/dist/engine/index.js.map +23 -0
- package/dist/engine/path.d.ts +26 -0
- package/dist/engine/pool.d.ts +83 -0
- package/dist/engine/types.d.ts +149 -0
- package/dist/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/events/BackpressureManager.d.ts +215 -0
- package/dist/events/CircuitBreaker.d.ts +229 -0
- package/dist/events/DeadLetterQueue.d.ts +219 -0
- package/dist/events/EventBackend.d.ts +12 -0
- package/dist/events/EventOptions.d.ts +204 -0
- package/dist/events/EventPriorityQueue.d.ts +63 -0
- package/dist/events/FlowControlStrategy.d.ts +109 -0
- package/dist/events/IdempotencyCache.d.ts +60 -0
- package/dist/events/MessageQueueBridge.d.ts +184 -0
- package/dist/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/events/RetryScheduler.d.ts +104 -0
- package/dist/events/WorkerPool.d.ts +98 -0
- package/dist/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/events/aggregation/index.d.ts +10 -0
- package/dist/events/aggregation/types.d.ts +117 -0
- package/dist/events/index.d.ts +26 -0
- package/dist/events/observability/EventMetrics.d.ts +132 -0
- package/dist/events/observability/EventTracer.d.ts +68 -0
- package/dist/events/observability/EventTracing.d.ts +161 -0
- package/dist/events/observability/OTelEventMetrics.d.ts +332 -0
- package/dist/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/events/observability/index.d.ts +24 -0
- package/dist/events/observability/metrics-types.d.ts +16 -0
- package/dist/events/queue-core.d.ts +77 -0
- package/dist/events/task-executor.d.ts +51 -0
- package/dist/events/types.d.ts +134 -0
- package/dist/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/exceptions/GravitoException.d.ts +23 -0
- package/dist/exceptions/HttpException.d.ts +9 -0
- package/dist/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/exceptions/ValidationException.d.ts +22 -0
- package/dist/exceptions/index.d.ts +7 -0
- package/dist/ffi/NativeAccelerator.d.ts +69 -0
- package/dist/ffi/NativeHasher.d.ts +139 -0
- package/dist/ffi/cbor-fallback.d.ts +96 -0
- package/dist/ffi/hash-fallback.d.ts +33 -0
- package/dist/ffi/index.d.ts +10 -0
- package/dist/ffi/index.js +131 -0
- package/dist/ffi/index.js.map +11 -0
- package/dist/ffi/types.d.ts +135 -0
- package/dist/health/HealthProvider.d.ts +67 -0
- package/dist/helpers/Arr.d.ts +19 -0
- package/dist/helpers/Str.d.ts +38 -0
- package/dist/helpers/data.d.ts +25 -0
- package/dist/helpers/errors.d.ts +34 -0
- package/dist/helpers/response.d.ts +41 -0
- package/dist/helpers.d.ts +338 -0
- package/dist/hooks/ActionManager.d.ts +132 -0
- package/dist/hooks/AsyncDetector.d.ts +84 -0
- package/dist/hooks/FilterManager.d.ts +71 -0
- package/dist/hooks/MigrationWarner.d.ts +24 -0
- package/dist/hooks/dlq-operations.d.ts +60 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/types.d.ts +107 -0
- package/dist/http/CookieJar.d.ts +51 -0
- package/dist/http/cookie.d.ts +29 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/{compat-CI8hiulX.d.cts → http/types.d.ts} +35 -16
- package/dist/index.browser.d.ts +34 -0
- package/dist/index.d.ts +60 -10981
- package/dist/index.js +10808 -11273
- package/dist/index.js.map +166 -0
- package/dist/observability/QueueDashboard.d.ts +136 -0
- package/dist/observability/contracts.d.ts +137 -0
- package/dist/observability/index.d.ts +13 -0
- package/dist/reliability/DeadLetterQueueManager.d.ts +349 -0
- package/dist/reliability/RetryPolicy.d.ts +217 -0
- package/dist/reliability/index.d.ts +6 -0
- package/dist/router/ControllerDispatcher.d.ts +12 -0
- package/dist/router/RequestValidator.d.ts +20 -0
- package/dist/runtime/adapter-bun.d.ts +12 -0
- package/dist/runtime/adapter-deno.d.ts +12 -0
- package/dist/runtime/adapter-node.d.ts +12 -0
- package/dist/runtime/adapter-unknown.d.ts +13 -0
- package/dist/runtime/archive.d.ts +17 -0
- package/dist/runtime/compression.d.ts +21 -0
- package/dist/runtime/deep-equals.d.ts +56 -0
- package/dist/runtime/detection.d.ts +22 -0
- package/dist/runtime/escape.d.ts +34 -0
- package/dist/runtime/index.browser.d.ts +20 -0
- package/dist/runtime/index.d.ts +44 -0
- package/dist/runtime/markdown.d.ts +44 -0
- package/dist/runtime/types.d.ts +436 -0
- package/dist/runtime-helpers.d.ts +67 -0
- package/dist/runtime.d.ts +11 -0
- package/dist/security/Encrypter.d.ts +33 -0
- package/dist/security/Hasher.d.ts +29 -0
- package/dist/testing/HttpTester.d.ts +39 -0
- package/dist/testing/TestResponse.d.ts +78 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/transpiler-utils.d.ts +170 -0
- package/dist/types/events.d.ts +94 -0
- package/dist/types.d.ts +13 -0
- package/package.json +25 -56
- package/src/ffi/native/cbor.c +1148 -0
- package/dist/Metrics-VOWWRNNR.js +0 -219
- package/dist/chunk-R5U7XKVJ.js +0 -16
- package/dist/compat-CI8hiulX.d.ts +0 -376
- package/dist/compat.cjs +0 -18
- package/dist/compat.d.cts +0 -1
- package/dist/engine/index.cjs +0 -1764
- package/dist/engine/index.d.cts +0 -922
- package/dist/index.cjs +0 -14906
- package/dist/index.d.cts +0 -11008
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Span } from '@opentelemetry/api';
|
|
2
|
+
import type { ActionCallback } from '../HookManager';
|
|
3
|
+
import type { BackpressureManager } from './BackpressureManager';
|
|
4
|
+
import type { DeadLetterQueue } from './DeadLetterQueue';
|
|
5
|
+
import type { EventBackend } from './EventBackend';
|
|
6
|
+
import type { EventOptions } from './EventOptions';
|
|
7
|
+
import type { EventMetrics } from './observability/EventMetrics';
|
|
8
|
+
import type { EventTracing } from './observability/EventTracing';
|
|
9
|
+
import type { OTelEventMetrics } from './observability/OTelEventMetrics';
|
|
10
|
+
import type { PriorityStatistics } from './PriorityEscalationManager';
|
|
11
|
+
import type { RetryScheduler } from './RetryScheduler';
|
|
12
|
+
import type { BackpressureStrategy, EventQueueConfig, EventTask, MultiPriorityQueueDepth } from './types';
|
|
13
|
+
import type { WorkerPool } from './WorkerPool';
|
|
14
|
+
export type { EventTask, EventQueueConfig, BackpressureStrategy };
|
|
15
|
+
/**
|
|
16
|
+
* Priority queue for event processing.
|
|
17
|
+
* Events are processed based on their priority level:
|
|
18
|
+
* - Critical priority events are processed first (< 1ms)
|
|
19
|
+
* - High priority events are processed second (< 50ms)
|
|
20
|
+
* - Normal priority events are processed third (< 200ms)
|
|
21
|
+
* - Low priority events are processed last (< 500ms)
|
|
22
|
+
*
|
|
23
|
+
* Supports automatic priority escalation based on wait time.
|
|
24
|
+
*
|
|
25
|
+
* Composed of:
|
|
26
|
+
* - {@link QueueCore}: queue data structures and backpressure logic
|
|
27
|
+
* - {@link TaskExecutor}: task execution, retry, circuit breaker, DLQ
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare class EventPriorityQueue implements EventBackend {
|
|
32
|
+
private queueCore;
|
|
33
|
+
private taskExecutor;
|
|
34
|
+
private processing;
|
|
35
|
+
private workerPool?;
|
|
36
|
+
constructor(config?: EventQueueConfig);
|
|
37
|
+
setDeadLetterQueue(dlq: DeadLetterQueue): void;
|
|
38
|
+
setPersistentDLQHandler(handler: (hook: string, args: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number) => Promise<void>): void;
|
|
39
|
+
setEventMetrics(metrics: EventMetrics): void;
|
|
40
|
+
setOTelEventMetrics(metrics: OTelEventMetrics): void;
|
|
41
|
+
setPriorityStatistics(stats: PriorityStatistics): void;
|
|
42
|
+
getPriorityStatistics(): PriorityStatistics | undefined;
|
|
43
|
+
setEventTracing(tracing: EventTracing): void;
|
|
44
|
+
setCurrentDispatchSpan(span: Span | undefined): void;
|
|
45
|
+
getCurrentDispatchSpan(): Span | undefined;
|
|
46
|
+
setRetryScheduler(scheduler: RetryScheduler): void;
|
|
47
|
+
getRetryScheduler(): RetryScheduler | undefined;
|
|
48
|
+
getCircuitBreaker(hook: string): import("./CircuitBreaker").CircuitBreaker;
|
|
49
|
+
getCircuitBreakers(): Map<string, import("./CircuitBreaker").CircuitBreaker>;
|
|
50
|
+
resetCircuitBreaker(hook: string): boolean;
|
|
51
|
+
setWorkerPool(pool: WorkerPool): void;
|
|
52
|
+
getWorkerPool(): WorkerPool | undefined;
|
|
53
|
+
getBackpressureManager(): BackpressureManager | undefined;
|
|
54
|
+
enqueue(task: EventTask): string;
|
|
55
|
+
enqueue(hook: string, args: unknown, callbacks: ActionCallback[], options: EventOptions): string;
|
|
56
|
+
enqueueBatch(tasks: EventTask[]): string[];
|
|
57
|
+
getDepth(): number;
|
|
58
|
+
getDepthByPriority(priority: 'critical' | 'high' | 'normal' | 'low'): number;
|
|
59
|
+
getQueueDepthByPriority(): MultiPriorityQueueDepth;
|
|
60
|
+
clear(): void;
|
|
61
|
+
dequeue(): EventTask | undefined;
|
|
62
|
+
private processNext;
|
|
63
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Flow Control Strategies
|
|
3
|
+
*
|
|
4
|
+
* Implements various flow control strategies for backpressure management.
|
|
5
|
+
* Strategies can be composed to create flexible backpressure policies.
|
|
6
|
+
*
|
|
7
|
+
* 流控策略:支援多種組合策略進行靈活的背壓管理。
|
|
8
|
+
*/
|
|
9
|
+
import { type BackpressureConfig, type BackpressureDecision, BackpressureState } from './BackpressureManager';
|
|
10
|
+
/**
|
|
11
|
+
* 流控評估上下文。
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface FlowControlContext {
|
|
16
|
+
/** 當前背壓狀態 */
|
|
17
|
+
state: BackpressureState;
|
|
18
|
+
/** 事件優先級 */
|
|
19
|
+
priority: 'high' | 'normal' | 'low';
|
|
20
|
+
/** 總隊列深度 */
|
|
21
|
+
totalDepth: number;
|
|
22
|
+
/** 分優先級隊列深度 */
|
|
23
|
+
depthByPriority: {
|
|
24
|
+
high: number;
|
|
25
|
+
normal: number;
|
|
26
|
+
low: number;
|
|
27
|
+
};
|
|
28
|
+
/** 當前入隊速率(events/sec) */
|
|
29
|
+
currentRate: number;
|
|
30
|
+
/** 配置 */
|
|
31
|
+
config: Omit<BackpressureConfig, 'onRejected' | 'onStateChange'>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 流控策略介面。
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export interface FlowControlStrategy {
|
|
39
|
+
/** 策略名稱 */
|
|
40
|
+
readonly name: string;
|
|
41
|
+
/** 評估是否應該限制此事件 */
|
|
42
|
+
evaluate(context: FlowControlContext): BackpressureDecision;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 隊列深度策略。
|
|
46
|
+
*
|
|
47
|
+
* 根據總隊列深度和分優先級隊列深度決定是否限制事件入隊。
|
|
48
|
+
*/
|
|
49
|
+
export declare class QueueDepthStrategy implements FlowControlStrategy {
|
|
50
|
+
readonly name = "queue-depth";
|
|
51
|
+
evaluate(context: FlowControlContext): BackpressureDecision;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 速率限制策略。
|
|
55
|
+
*
|
|
56
|
+
* 根據每秒入隊速率限制事件入隊。
|
|
57
|
+
*/
|
|
58
|
+
export declare class RateLimitStrategy implements FlowControlStrategy {
|
|
59
|
+
readonly name = "rate-limit";
|
|
60
|
+
evaluate(context: FlowControlContext): BackpressureDecision;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 優先級平衡策略。
|
|
64
|
+
*
|
|
65
|
+
* 在 WARNING 和 CRITICAL 狀態下降級低優先級事件,防止高優先級飢餓。
|
|
66
|
+
*/
|
|
67
|
+
export declare class PriorityRebalanceStrategy implements FlowControlStrategy {
|
|
68
|
+
readonly name = "priority-rebalance";
|
|
69
|
+
evaluate(context: FlowControlContext): BackpressureDecision;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 反飢餓保護策略。
|
|
73
|
+
*
|
|
74
|
+
* 防止低優先級事件被長期壓制。
|
|
75
|
+
* 當事件等待超過 starvationTimeoutMs 時,自動提升其優先級。
|
|
76
|
+
*/
|
|
77
|
+
export declare class StarvationProtectionStrategy implements FlowControlStrategy {
|
|
78
|
+
readonly name = "starvation-protection";
|
|
79
|
+
evaluate(_context: FlowControlContext): BackpressureDecision;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 組合策略。
|
|
83
|
+
*
|
|
84
|
+
* 組合多個策略。評估時所有子策略必須允許,否則拒絕。
|
|
85
|
+
* 取最嚴格的決策(拒絕 > 延遲 > 允許)。
|
|
86
|
+
*/
|
|
87
|
+
export declare class CompositeStrategy implements FlowControlStrategy {
|
|
88
|
+
readonly name: string;
|
|
89
|
+
private strategies;
|
|
90
|
+
constructor(name: string, strategies: FlowControlStrategy[]);
|
|
91
|
+
evaluate(context: FlowControlContext): BackpressureDecision;
|
|
92
|
+
/**
|
|
93
|
+
* 新增子策略。
|
|
94
|
+
*/
|
|
95
|
+
addStrategy(strategy: FlowControlStrategy): void;
|
|
96
|
+
/**
|
|
97
|
+
* 移除指定名稱的子策略。
|
|
98
|
+
*/
|
|
99
|
+
removeStrategy(name: string): boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 工廠方法:建立預設流控策略組合。
|
|
103
|
+
*
|
|
104
|
+
* @param config 背壓配置
|
|
105
|
+
* @returns 組合策略實例
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
108
|
+
*/
|
|
109
|
+
export declare function createDefaultStrategies(_config: Omit<BackpressureConfig, 'onRejected' | 'onStateChange'>): FlowControlStrategy[];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Idempotency cache for deduplicating events.
|
|
3
|
+
* Prevents duplicate events from being processed within a configurable TTL window.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class IdempotencyCache {
|
|
7
|
+
private cache;
|
|
8
|
+
private cleanupInterval;
|
|
9
|
+
private readonly defaultCleanupIntervalMs;
|
|
10
|
+
constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Check if an event with the given idempotency key is a duplicate.
|
|
13
|
+
* @param key - Idempotency key
|
|
14
|
+
* @param ttlMs - Time-to-live in milliseconds
|
|
15
|
+
* @returns True if this is a duplicate, false if it's a new event
|
|
16
|
+
*/
|
|
17
|
+
isDuplicate(key: string, ttlMs: number): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Record an event in the cache.
|
|
20
|
+
* @param key - Idempotency key
|
|
21
|
+
*/
|
|
22
|
+
recordEvent(key: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Remove an entry from the cache.
|
|
25
|
+
* @param key - Idempotency key
|
|
26
|
+
* @returns True if entry was removed, false if not found
|
|
27
|
+
*/
|
|
28
|
+
remove(key: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Clear all entries from the cache.
|
|
31
|
+
*/
|
|
32
|
+
clear(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Get the current cache size.
|
|
35
|
+
* @returns Number of entries in cache
|
|
36
|
+
*/
|
|
37
|
+
getSize(): number;
|
|
38
|
+
/**
|
|
39
|
+
* Start periodic cleanup of expired entries.
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
private startCleanup;
|
|
43
|
+
/**
|
|
44
|
+
* Stop the periodic cleanup.
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
stopCleanup(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Clean up expired entries from the cache.
|
|
50
|
+
* This is called periodically and doesn't use strict TTL checking,
|
|
51
|
+
* so entries older than a reasonable default (24 hours) are removed.
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
private cleanup;
|
|
55
|
+
/**
|
|
56
|
+
* Destructor to clean up resources.
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
destroy(): void;
|
|
60
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Message Queue Bridge
|
|
3
|
+
*
|
|
4
|
+
* 橋接層,連接 HookManager 與 Bull Queue (via StreamEventBackend)
|
|
5
|
+
* 支持分佈式事件流:HookManager → Bull Queue → Worker → 完成/DLQ
|
|
6
|
+
*/
|
|
7
|
+
import type { HookManager } from '../HookManager';
|
|
8
|
+
import type { DeadLetterQueueManager } from '../reliability/DeadLetterQueueManager';
|
|
9
|
+
import type { EventBackend } from './EventBackend';
|
|
10
|
+
import type { EventTask } from './types';
|
|
11
|
+
import type { WorkerPool } from './WorkerPool';
|
|
12
|
+
/**
|
|
13
|
+
* 事件執行狀態
|
|
14
|
+
*/
|
|
15
|
+
export interface EventStatus {
|
|
16
|
+
eventId: string;
|
|
17
|
+
status: 'pending' | 'processing' | 'completed' | 'failed' | 'in_dlq';
|
|
18
|
+
attempts: number;
|
|
19
|
+
lastError?: string;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
processedAt?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* MessageQueueBridge 配置
|
|
25
|
+
*/
|
|
26
|
+
export interface MessageQueueBridgeConfig {
|
|
27
|
+
/**
|
|
28
|
+
* HookManager 實例
|
|
29
|
+
*/
|
|
30
|
+
hookManager: HookManager;
|
|
31
|
+
/**
|
|
32
|
+
* Worker 線程池(可選)
|
|
33
|
+
*/
|
|
34
|
+
workerPool?: WorkerPool;
|
|
35
|
+
/**
|
|
36
|
+
* 分佈式事件後端(Bull Queue)
|
|
37
|
+
*/
|
|
38
|
+
eventBackend: EventBackend;
|
|
39
|
+
/**
|
|
40
|
+
* Dead Letter Queue 管理器
|
|
41
|
+
*/
|
|
42
|
+
dlqManager: DeadLetterQueueManager;
|
|
43
|
+
/**
|
|
44
|
+
* 是否啟用 CircuitBreaker 檢查
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
enableCircuitBreaker?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* MessageQueueBridge - 連接 HookManager 與 Bull Queue 的橋接層
|
|
51
|
+
*
|
|
52
|
+
* 功能:
|
|
53
|
+
* 1. 通過 Bull Queue 分發事件(dispatchWithQueue)
|
|
54
|
+
* 2. Worker 中處理隊列事件(processQueuedEvent)
|
|
55
|
+
* 3. 失敗任務轉移至 DLQ(handleJobFailure)
|
|
56
|
+
* 4. 查詢事件執行狀態(getEventStatus)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const bridge = new MessageQueueBridge({
|
|
61
|
+
* hookManager,
|
|
62
|
+
* eventBackend: streamEventBackend,
|
|
63
|
+
* dlqManager,
|
|
64
|
+
* enableCircuitBreaker: true
|
|
65
|
+
* })
|
|
66
|
+
*
|
|
67
|
+
* // 分發事件至 Bull Queue
|
|
68
|
+
* const jobId = await bridge.dispatchWithQueue('order:created', { orderId: 123 })
|
|
69
|
+
*
|
|
70
|
+
* // Worker 處理事件
|
|
71
|
+
* await bridge.processQueuedEvent(jobId, task)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class MessageQueueBridge {
|
|
75
|
+
private config;
|
|
76
|
+
constructor(config: MessageQueueBridgeConfig);
|
|
77
|
+
/**
|
|
78
|
+
* 通過 Bull Queue 分發事件(分佈式異步處理)
|
|
79
|
+
*
|
|
80
|
+
* 流程:
|
|
81
|
+
* 1. 驗證事件 listeners 存在
|
|
82
|
+
* 2. 檢查 CircuitBreaker 狀態(如果啟用)
|
|
83
|
+
* 3. 構建 EventTask
|
|
84
|
+
* 4. 入隊到 eventBackend (Bull Queue)
|
|
85
|
+
*
|
|
86
|
+
* @param eventName - 事件名稱
|
|
87
|
+
* @param args - 事件參數
|
|
88
|
+
* @param options - 事件選項(可選)
|
|
89
|
+
* @returns Job ID
|
|
90
|
+
* @throws 如果沒有 listeners 或 CircuitBreaker 打開
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const jobId = await bridge.dispatchWithQueue('order:created', {
|
|
95
|
+
* orderId: 'ORD-123',
|
|
96
|
+
* amount: 999.99
|
|
97
|
+
* })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
dispatchWithQueue<TArgs = unknown>(eventName: string, args: TArgs, options?: any): Promise<string>;
|
|
101
|
+
/**
|
|
102
|
+
* 處理隊列事件(由 Bull Queue Worker 調用)
|
|
103
|
+
*
|
|
104
|
+
* 重要:使用 doActionSync() 避免遞迴
|
|
105
|
+
* - 不能使用 doActionAsync()(會導致事件再次入隊)
|
|
106
|
+
* - Worker 中直接同步執行 callbacks
|
|
107
|
+
*
|
|
108
|
+
* 流程:
|
|
109
|
+
* 1. 直接執行 callbacks(不使用 doActionSync,避免吞掉錯誤)
|
|
110
|
+
* 2. 記錄成功或失敗
|
|
111
|
+
* 3. 拋出錯誤讓 Bull Queue 處理重試
|
|
112
|
+
*
|
|
113
|
+
* @param jobId - Bull Queue Job ID
|
|
114
|
+
* @param task - 事件任務
|
|
115
|
+
* @throws 如果 callback 執行失敗,讓 Bull Queue 處理重試
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // 在 Bull Queue Worker 中:
|
|
120
|
+
* await bridge.processQueuedEvent(jobId, task)
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
processQueuedEvent(jobId: string, task: EventTask): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* 處理 Bull Queue 任務失敗(由 onFailed callback 調用)
|
|
126
|
+
*
|
|
127
|
+
* 流程:
|
|
128
|
+
* 1. 記錄警告日誌
|
|
129
|
+
* 2. 移至 persistent DLQ(DeadLetterQueueManager)
|
|
130
|
+
* 3. 持久化到 event_dlq 表,支持延遲重試
|
|
131
|
+
*
|
|
132
|
+
* @param jobId - Bull Queue Job ID
|
|
133
|
+
* @param task - 事件任務
|
|
134
|
+
* @param error - 導致失敗的錯誤
|
|
135
|
+
* @param retryCount - 已重試次數(可選)
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* // 在 Bull Queue onFailed callback 中:
|
|
140
|
+
* await bridge.handleJobFailure(jobId, task, error, 3)
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
handleJobFailure(jobId: string, task: EventTask, error: Error, retryCount?: number): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* 查詢事件執行狀態
|
|
146
|
+
*
|
|
147
|
+
* 支持查詢:
|
|
148
|
+
* 1. 待處理事件
|
|
149
|
+
* 2. 正在處理的事件
|
|
150
|
+
* 3. 已完成事件
|
|
151
|
+
* 4. 失敗事件(在 DLQ 中)
|
|
152
|
+
*
|
|
153
|
+
* @param eventId - 事件 ID(task.id 或 jobId)
|
|
154
|
+
* @returns 事件狀態
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const status = await bridge.getEventStatus('queue-1707000000000-abc123')
|
|
159
|
+
* console.log(status) // { eventId, status, attempts, ... }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
getEventStatus(eventId: string): Promise<EventStatus>;
|
|
163
|
+
/**
|
|
164
|
+
* 獲取 EventBackend 實例(用於高級操作)
|
|
165
|
+
*
|
|
166
|
+
* @returns EventBackend 實例
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
getEventBackend(): EventBackend;
|
|
170
|
+
/**
|
|
171
|
+
* 獲取 DLQ 管理器實例(用於高級操作)
|
|
172
|
+
*
|
|
173
|
+
* @returns DeadLetterQueueManager 實例
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
getDLQManager(): DeadLetterQueueManager;
|
|
177
|
+
/**
|
|
178
|
+
* 獲取 HookManager 實例(用於高級操作)
|
|
179
|
+
*
|
|
180
|
+
* @returns HookManager 實例
|
|
181
|
+
* @internal
|
|
182
|
+
*/
|
|
183
|
+
getHookManager(): HookManager;
|
|
184
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { EventOptions } from './EventOptions';
|
|
2
|
+
import type { EventTask } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* 優先級升級管理器
|
|
5
|
+
*
|
|
6
|
+
* 根據事件等待時間動態調整優先級,實施優先級提升機制。
|
|
7
|
+
* 確保關鍵事件不會因為隊列深度而長時間等待。
|
|
8
|
+
*
|
|
9
|
+
* 升級規則(可配置):
|
|
10
|
+
* - LOW 等待 > 200ms → NORMAL
|
|
11
|
+
* - NORMAL 等待 > 100ms → HIGH
|
|
12
|
+
* - HIGH 等待 > 50ms → CRITICAL
|
|
13
|
+
* - 超過 maxWaitTimeMs → 強制 CRITICAL
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class PriorityEscalationManager {
|
|
18
|
+
/**
|
|
19
|
+
* 計算事件當前應該的優先級
|
|
20
|
+
* 根據等待時間動態調整
|
|
21
|
+
*/
|
|
22
|
+
static calculateCurrentPriority(task: EventTask, config?: EventOptions['escalation']): 'critical' | 'high' | 'normal' | 'low';
|
|
23
|
+
/**
|
|
24
|
+
* 比較兩個事件的優先級
|
|
25
|
+
* @returns 負數表示 task1 優先級更高,正數表示 task2 優先級更高
|
|
26
|
+
*/
|
|
27
|
+
static comparePriority(task1: EventTask, task2: EventTask): number;
|
|
28
|
+
/**
|
|
29
|
+
* 判斷事件是否應該立即處理(跳過隊列)
|
|
30
|
+
*/
|
|
31
|
+
static shouldProcessImmediately(task: EventTask, config?: EventOptions['escalation']): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* 獲取事件當前的超期時間(毫秒)
|
|
34
|
+
* 正數表示已超期,0 表示未超期
|
|
35
|
+
*/
|
|
36
|
+
static calculateOverdueTime(task: EventTask, config?: EventOptions['escalation']): number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 優先級統計器
|
|
40
|
+
* 追蹤優先級分布和升級事件
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export declare class PriorityStatistics {
|
|
45
|
+
private eventCounts;
|
|
46
|
+
private escalationCounts;
|
|
47
|
+
private totalEscalations;
|
|
48
|
+
/**
|
|
49
|
+
* 記錄一個優先級的事件
|
|
50
|
+
*/
|
|
51
|
+
recordEvent(priority: 'critical' | 'high' | 'normal' | 'low'): void;
|
|
52
|
+
/**
|
|
53
|
+
* 記錄優先級升級
|
|
54
|
+
*/
|
|
55
|
+
recordEscalation(fromPriority: 'critical' | 'high' | 'normal' | 'low', toPriority: 'critical' | 'high' | 'normal' | 'low'): void;
|
|
56
|
+
/**
|
|
57
|
+
* 獲取優先級分布
|
|
58
|
+
*/
|
|
59
|
+
getDistribution(): Record<'critical' | 'high' | 'normal' | 'low', string>;
|
|
60
|
+
/**
|
|
61
|
+
* 獲取升級統計
|
|
62
|
+
*/
|
|
63
|
+
getEscalationStats(): {
|
|
64
|
+
total: number;
|
|
65
|
+
byTransition: Record<string, number>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* 重置統計信息
|
|
69
|
+
*/
|
|
70
|
+
reset(): void;
|
|
71
|
+
/**
|
|
72
|
+
* 獲取所有統計信息
|
|
73
|
+
*/
|
|
74
|
+
getStats(): {
|
|
75
|
+
eventCounts: Record<'critical' | 'high' | 'normal' | 'low', number>;
|
|
76
|
+
escalationStats: {
|
|
77
|
+
total: number;
|
|
78
|
+
byTransition: Record<string, number>;
|
|
79
|
+
};
|
|
80
|
+
distribution: Record<'critical' | 'high' | 'normal' | 'low', string>;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Retry Scheduler
|
|
3
|
+
*
|
|
4
|
+
* 分佈式重試排程器,使用 Bull Queue 進行異步延遲重試。
|
|
5
|
+
* 支援指數回退、Redis 持久化、重試失敗回呼。
|
|
6
|
+
*
|
|
7
|
+
* Distributed retry scheduler using Bull Queue for async delayed retries.
|
|
8
|
+
* Supports exponential backoff, Redis persistence, and failure callbacks.
|
|
9
|
+
*/
|
|
10
|
+
import type { EventOptions } from './EventOptions';
|
|
11
|
+
/**
|
|
12
|
+
* 重試排程器配置介面
|
|
13
|
+
*/
|
|
14
|
+
export interface RetrySchedulerConfig {
|
|
15
|
+
/** 是否啟用 Bull Queue 重試(預設 true) */
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
/** Redis 連接配置(可選,使用全域 Redis) */
|
|
18
|
+
redisUrl?: string;
|
|
19
|
+
/** 預設最大重試次數(預設 5) */
|
|
20
|
+
maxRetries?: number;
|
|
21
|
+
/** 初始延遲時間(ms,預設 1000) */
|
|
22
|
+
initialDelayMs?: number;
|
|
23
|
+
/** 指數回退倍數(預設 2.0) */
|
|
24
|
+
backoffMultiplier?: number;
|
|
25
|
+
/** 最大延遲時間(ms,預設 1h) */
|
|
26
|
+
maxDelayMs?: number;
|
|
27
|
+
/** 重試失敗回呼 */
|
|
28
|
+
onRetryFailed?: (eventName: string, error: Error, retryCount: number) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 隊列統計資訊
|
|
32
|
+
*/
|
|
33
|
+
export interface QueueStats {
|
|
34
|
+
name: string;
|
|
35
|
+
jobCounts: {
|
|
36
|
+
waiting: number;
|
|
37
|
+
active: number;
|
|
38
|
+
delayed: number;
|
|
39
|
+
failed: number;
|
|
40
|
+
};
|
|
41
|
+
completedCount: number;
|
|
42
|
+
failedCount: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 重試排程器
|
|
46
|
+
*
|
|
47
|
+
* 提供異步分佈式重試機制,利用 Bull Queue 進行延遲重試。
|
|
48
|
+
* 支援指數回退、Redis 持久化、隊列統計。
|
|
49
|
+
*/
|
|
50
|
+
export declare class RetryScheduler {
|
|
51
|
+
private enabled;
|
|
52
|
+
private config;
|
|
53
|
+
private queues;
|
|
54
|
+
private bullmqModule;
|
|
55
|
+
private bullmqLoadError;
|
|
56
|
+
constructor(config?: RetrySchedulerConfig);
|
|
57
|
+
/**
|
|
58
|
+
* 動態加載 bullmq 模組
|
|
59
|
+
*/
|
|
60
|
+
private loadBullmq;
|
|
61
|
+
/**
|
|
62
|
+
* 檢查排程器是否啟用
|
|
63
|
+
*/
|
|
64
|
+
isEnabled(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 計算指數回退延遲時間
|
|
67
|
+
*
|
|
68
|
+
* @param retryCount 當前重試次數(從 0 開始)
|
|
69
|
+
* @returns 延遲時間(毫秒)
|
|
70
|
+
*/
|
|
71
|
+
private calculateDelay;
|
|
72
|
+
/**
|
|
73
|
+
* 獲取或創建隊列
|
|
74
|
+
*
|
|
75
|
+
* @param eventName 事件名稱
|
|
76
|
+
* @returns Queue 實例
|
|
77
|
+
*/
|
|
78
|
+
private getOrCreateQueue;
|
|
79
|
+
/**
|
|
80
|
+
* 排程重試任務
|
|
81
|
+
*
|
|
82
|
+
* @param eventName 事件名稱
|
|
83
|
+
* @param payload 事件負載
|
|
84
|
+
* @param options 事件選項
|
|
85
|
+
* @param error 原始錯誤
|
|
86
|
+
* @param retryCount 當前重試次數
|
|
87
|
+
*/
|
|
88
|
+
scheduleRetry(eventName: string, payload: unknown, _options: EventOptions, error: Error, retryCount: number): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* 獲取特定事件的隊列
|
|
91
|
+
*
|
|
92
|
+
* @param eventName 事件名稱
|
|
93
|
+
* @returns Queue 實例或 undefined
|
|
94
|
+
*/
|
|
95
|
+
getQueue(eventName: string): unknown;
|
|
96
|
+
/**
|
|
97
|
+
* 取得所有隊列統計
|
|
98
|
+
*/
|
|
99
|
+
getStats(): Map<string, QueueStats>;
|
|
100
|
+
/**
|
|
101
|
+
* 關閉所有隊列並清理資源
|
|
102
|
+
*/
|
|
103
|
+
shutdown(): Promise<void>;
|
|
104
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Pool for concurrent event task processing.
|
|
3
|
+
*
|
|
4
|
+
* Manages a pool of workers for executing event tasks concurrently.
|
|
5
|
+
* Supports auto-scaling, health checks, and OpenTelemetry metrics integration.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
import type { Meter } from '@opentelemetry/api';
|
|
10
|
+
import type { EventTask } from './types';
|
|
11
|
+
import type { WorkerPoolConfig, WorkerPoolStats, WorkerStats } from './WorkerPoolConfig';
|
|
12
|
+
/**
|
|
13
|
+
* Worker Pool for managing concurrent task execution
|
|
14
|
+
*/
|
|
15
|
+
export declare class WorkerPool {
|
|
16
|
+
private workers;
|
|
17
|
+
private taskQueue;
|
|
18
|
+
private config;
|
|
19
|
+
private metrics?;
|
|
20
|
+
private healthCheckTimer?;
|
|
21
|
+
private metricsTimer?;
|
|
22
|
+
private isRunning;
|
|
23
|
+
private scaleDownCounter;
|
|
24
|
+
constructor(config?: WorkerPoolConfig, meter?: Meter);
|
|
25
|
+
/**
|
|
26
|
+
* Start the worker pool
|
|
27
|
+
*/
|
|
28
|
+
start(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Stop the worker pool
|
|
31
|
+
*/
|
|
32
|
+
stop(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Submit a task to the worker pool
|
|
35
|
+
*/
|
|
36
|
+
submitTask(task: EventTask): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Process next task in queue
|
|
39
|
+
*/
|
|
40
|
+
private processQueue;
|
|
41
|
+
/**
|
|
42
|
+
* Execute task on a worker
|
|
43
|
+
*/
|
|
44
|
+
private executeTask;
|
|
45
|
+
/**
|
|
46
|
+
* Execute task callbacks
|
|
47
|
+
*/
|
|
48
|
+
private executeTaskCallbacks;
|
|
49
|
+
/**
|
|
50
|
+
* Create a timeout promise
|
|
51
|
+
*/
|
|
52
|
+
private createTimeout;
|
|
53
|
+
/**
|
|
54
|
+
* Create a new worker
|
|
55
|
+
*/
|
|
56
|
+
private createWorker;
|
|
57
|
+
/**
|
|
58
|
+
* Remove a worker
|
|
59
|
+
*/
|
|
60
|
+
private removeWorker;
|
|
61
|
+
/**
|
|
62
|
+
* Perform health check and cleanup
|
|
63
|
+
*/
|
|
64
|
+
private performHealthCheck;
|
|
65
|
+
/**
|
|
66
|
+
* Perform auto-scaling based on load
|
|
67
|
+
*/
|
|
68
|
+
private performAutoScaling;
|
|
69
|
+
/**
|
|
70
|
+
* Collect metrics
|
|
71
|
+
*/
|
|
72
|
+
private collectMetrics;
|
|
73
|
+
/**
|
|
74
|
+
* Get number of active workers (not terminated)
|
|
75
|
+
*/
|
|
76
|
+
private getActiveWorkerCount;
|
|
77
|
+
/**
|
|
78
|
+
* Get worker utilization (0-1)
|
|
79
|
+
*/
|
|
80
|
+
private getUtilization;
|
|
81
|
+
/**
|
|
82
|
+
* Get queue depth
|
|
83
|
+
*/
|
|
84
|
+
getQueueDepth(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Get worker utilization
|
|
87
|
+
*/
|
|
88
|
+
getWorkerUtilization(): number;
|
|
89
|
+
/**
|
|
90
|
+
* Get worker statistics
|
|
91
|
+
*/
|
|
92
|
+
getWorkerStats(): WorkerStats[];
|
|
93
|
+
/**
|
|
94
|
+
* Get pool statistics
|
|
95
|
+
*/
|
|
96
|
+
getPoolStats(): WorkerPoolStats;
|
|
97
|
+
}
|
|
98
|
+
export type { WorkerPoolConfig, WorkerStats, WorkerPoolStats };
|