@gravito/ripple 4.0.0 → 4.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/dist/atlas/src/DB.d.ts +51 -4
- package/dist/atlas/src/config/index.d.ts +1 -1
- package/dist/atlas/src/config/loadConfig.d.ts +0 -7
- package/dist/atlas/src/connection/Connection.d.ts +4 -0
- package/dist/atlas/src/connection/ConnectionManager.d.ts +75 -6
- package/dist/atlas/src/connection/ReplicaConnectionPool.d.ts +54 -0
- package/dist/atlas/src/drivers/MySQLDriver.d.ts +16 -3
- package/dist/atlas/src/drivers/PostgresDriver.d.ts +15 -2
- package/dist/atlas/src/index.d.ts +15 -3
- package/dist/atlas/src/observability/AtlasMetrics.d.ts +22 -0
- package/dist/atlas/src/orm/Repository.d.ts +247 -0
- package/dist/atlas/src/orm/index.d.ts +1 -0
- package/dist/atlas/src/orm/model/Model.d.ts +11 -2
- package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +14 -0
- package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +5 -0
- package/dist/atlas/src/orm/model/decorators.d.ts +29 -0
- package/dist/atlas/src/orm/model/index.d.ts +1 -1
- package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +1 -0
- package/dist/atlas/src/pool/AdaptivePoolManager.d.ts +98 -0
- package/dist/atlas/src/pool/PoolHealthChecker.d.ts +91 -0
- package/dist/atlas/src/pool/PoolStrategy.d.ts +129 -0
- package/dist/atlas/src/pool/PoolWarmer.d.ts +92 -0
- package/dist/atlas/src/query/QueryBuilder.d.ts +71 -1
- package/dist/atlas/src/query/RelationshipResolver.d.ts +23 -0
- package/dist/atlas/src/schema/MigrationGenerator.d.ts +45 -0
- package/dist/atlas/src/schema/SchemaDiff.d.ts +73 -0
- package/dist/atlas/src/schema/TypeGenerator.d.ts +57 -0
- package/dist/atlas/src/schema/TypeWriter.d.ts +42 -0
- package/dist/atlas/src/sharding/ShardingManager.d.ts +59 -0
- package/dist/atlas/src/types/index.d.ts +83 -1
- package/dist/atlas/src/utils/CursorEncoding.d.ts +63 -0
- package/dist/core/src/ConfigManager.d.ts +39 -0
- package/dist/core/src/Container/RequestScopeManager.d.ts +62 -0
- package/dist/core/src/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/core/src/Container.d.ts +45 -0
- package/dist/core/src/ErrorHandler.d.ts +3 -0
- package/dist/core/src/HookManager.d.ts +95 -0
- package/dist/core/src/PlanetCore.d.ts +89 -0
- package/dist/core/src/RequestContext.d.ts +97 -0
- package/dist/core/src/ServiceProvider.d.ts +22 -0
- package/dist/core/src/adapters/PhotonAdapter.d.ts +4 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +4 -0
- package/dist/core/src/cli/queue-commands.d.ts +6 -0
- package/dist/core/src/engine/AOTRouter.d.ts +6 -1
- package/dist/core/src/engine/FastContext.d.ts +23 -0
- package/dist/core/src/engine/Gravito.d.ts +0 -1
- package/dist/core/src/engine/MinimalContext.d.ts +21 -0
- package/dist/core/src/engine/types.d.ts +3 -0
- package/dist/core/src/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/core/src/events/BackpressureManager.d.ts +215 -0
- package/dist/core/src/events/DeadLetterQueue.d.ts +75 -1
- package/dist/core/src/events/EventBackend.d.ts +2 -1
- package/dist/core/src/events/EventOptions.d.ts +99 -4
- package/dist/core/src/events/EventPriorityQueue.d.ts +105 -6
- package/dist/core/src/events/FlowControlStrategy.d.ts +109 -0
- package/dist/core/src/events/MessageQueueBridge.d.ts +184 -0
- package/dist/core/src/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/core/src/events/RetryScheduler.d.ts +104 -0
- package/dist/core/src/events/WorkerPool.d.ts +98 -0
- package/dist/core/src/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/core/src/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/core/src/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/core/src/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/core/src/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/core/src/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/core/src/events/aggregation/types.d.ts +117 -0
- package/dist/core/src/events/index.d.ts +11 -0
- package/dist/core/src/events/observability/OTelEventMetrics.d.ts +92 -0
- package/dist/core/src/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/core/src/events/observability/index.d.ts +4 -0
- package/dist/core/src/events/types.d.ts +59 -0
- package/dist/core/src/health/HealthProvider.d.ts +67 -0
- package/dist/core/src/http/types.d.ts +19 -0
- package/dist/core/src/index.d.ts +13 -1
- package/dist/core/src/observability/Metrics.d.ts +244 -0
- package/dist/core/src/observability/QueueDashboard.d.ts +136 -0
- package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +34 -0
- package/dist/index.js +403 -40
- package/dist/index.js.map +11 -9
- package/dist/photon/src/index.d.ts +5 -0
- package/dist/photon/src/middleware/ratelimit-redis.d.ts +50 -0
- package/dist/photon/src/middleware/ratelimit.d.ts +4 -0
- package/dist/ripple/src/RippleServer.d.ts +0 -1
- package/dist/ripple/src/engines/UWebSocketsEngine.d.ts +97 -0
- package/dist/ripple/src/engines/WsEngine.d.ts +69 -0
- package/dist/ripple/src/engines/index.d.ts +4 -0
- package/dist/ripple/src/serializers/ISerializer.d.ts +1 -1
- package/dist/ripple/src/serializers/JsonSerializer.d.ts +1 -1
- package/dist/ripple/src/serializers/ProtobufSerializer.d.ts +6 -3
- package/dist/ripple/src/types.d.ts +11 -0
- package/package.json +3 -2
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* @since 2.0.0
|
|
9
9
|
*/
|
|
10
10
|
import type { Context, Handler, MiddlewareHandler, Photon } from '@gravito/photon';
|
|
11
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
11
12
|
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoRequest, GravitoVariables, HttpMethod, ProxyOptions, StatusCode } from '../http/types';
|
|
12
13
|
import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
|
|
13
14
|
/**
|
|
@@ -50,6 +51,7 @@ declare class PhotonRequestWrapper implements GravitoRequest {
|
|
|
50
51
|
declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
|
|
51
52
|
private _req;
|
|
52
53
|
private photonCtx;
|
|
54
|
+
private _requestScope;
|
|
53
55
|
route: (name: string, params?: Record<string, any>, query?: Record<string, any>) => string;
|
|
54
56
|
/**
|
|
55
57
|
* Reset the wrapper for pooling
|
|
@@ -90,6 +92,8 @@ declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables
|
|
|
90
92
|
get env(): Record<string, unknown> | undefined;
|
|
91
93
|
get native(): Context;
|
|
92
94
|
forward(target: string, options?: ProxyOptions): Promise<Response>;
|
|
95
|
+
requestScope(): RequestScopeManager;
|
|
96
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
93
97
|
}
|
|
94
98
|
/**
|
|
95
99
|
* Convert a GravitoHandler to a Photon Handler
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RequestScopeManager } from '../../Container/RequestScopeManager';
|
|
1
2
|
import type { ContentfulStatusCode, GravitoContext, GravitoVariables, ProxyOptions, StatusCode } from '../../http/types';
|
|
2
3
|
import { BunRequest } from './BunRequest';
|
|
3
4
|
/**
|
|
@@ -8,6 +9,7 @@ export declare class BunContext<V extends GravitoVariables = GravitoVariables> i
|
|
|
8
9
|
readonly env: Record<string, unknown>;
|
|
9
10
|
readonly req: BunRequest;
|
|
10
11
|
private _variables;
|
|
12
|
+
private _requestScope;
|
|
11
13
|
/**
|
|
12
14
|
* URL generator helper
|
|
13
15
|
*/
|
|
@@ -42,4 +44,6 @@ export declare class BunContext<V extends GravitoVariables = GravitoVariables> i
|
|
|
42
44
|
get<K extends keyof V>(key: K): V[K];
|
|
43
45
|
set<K extends keyof V>(key: K, value: V[K]): void;
|
|
44
46
|
get executionCtx(): ExecutionContext | undefined;
|
|
47
|
+
requestScope(): RequestScopeManager;
|
|
48
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
45
49
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CommandKernel } from '../CommandKernel';
|
|
2
|
+
import type { QueueDashboard } from '../observability/QueueDashboard';
|
|
3
|
+
/**
|
|
4
|
+
* Register queue management commands with CommandKernel
|
|
5
|
+
*/
|
|
6
|
+
export declare function registerQueueCommands(kernel: CommandKernel, dashboard: QueueDashboard): void;
|
|
@@ -37,7 +37,12 @@ export declare class AOTRouter {
|
|
|
37
37
|
private dynamicRoutePatterns;
|
|
38
38
|
private middlewareCache;
|
|
39
39
|
private cacheMaxSize;
|
|
40
|
-
private
|
|
40
|
+
private _version;
|
|
41
|
+
/**
|
|
42
|
+
* Get the current version for cache invalidation
|
|
43
|
+
* Incremented whenever middleware or routes are modified
|
|
44
|
+
*/
|
|
45
|
+
get version(): number;
|
|
41
46
|
/**
|
|
42
47
|
* Register a route
|
|
43
48
|
*
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @module @gravito/core/engine
|
|
8
8
|
*/
|
|
9
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
9
10
|
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
10
11
|
/**
|
|
11
12
|
* Lazy-parsed request wrapper
|
|
@@ -23,6 +24,11 @@ declare class FastRequestImpl implements FastRequest {
|
|
|
23
24
|
private _headers;
|
|
24
25
|
private _cachedJson;
|
|
25
26
|
private _jsonParsed;
|
|
27
|
+
private _cachedText;
|
|
28
|
+
private _textParsed;
|
|
29
|
+
private _cachedFormData;
|
|
30
|
+
private _formDataParsed;
|
|
31
|
+
private _cachedQueries;
|
|
26
32
|
private _ctx;
|
|
27
33
|
constructor(ctx: FastContext);
|
|
28
34
|
/**
|
|
@@ -60,6 +66,7 @@ export declare class FastContext implements IFastContext {
|
|
|
60
66
|
readonly req: FastRequestImpl;
|
|
61
67
|
private _headers;
|
|
62
68
|
_isReleased: boolean;
|
|
69
|
+
private _requestScope;
|
|
63
70
|
/**
|
|
64
71
|
* Initialize context for a new request
|
|
65
72
|
*
|
|
@@ -94,6 +101,22 @@ export declare class FastContext implements IFastContext {
|
|
|
94
101
|
private _store;
|
|
95
102
|
get<T>(key: string): T;
|
|
96
103
|
set(key: string, value: any): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get the request-scoped service manager for this request.
|
|
106
|
+
*
|
|
107
|
+
* @returns The RequestScopeManager for this request.
|
|
108
|
+
* @throws Error if called before init() or after reset().
|
|
109
|
+
*/
|
|
110
|
+
requestScope(): RequestScopeManager;
|
|
111
|
+
/**
|
|
112
|
+
* Resolve a request-scoped service (convenience method).
|
|
113
|
+
*
|
|
114
|
+
* @template T - The service type.
|
|
115
|
+
* @param key - The service key for caching.
|
|
116
|
+
* @param factory - Factory function to create the service.
|
|
117
|
+
* @returns The cached or newly created service instance.
|
|
118
|
+
*/
|
|
119
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
97
120
|
route: (name: string, params?: any, query?: any) => string;
|
|
98
121
|
get native(): this;
|
|
99
122
|
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* @module @gravito/core/engine
|
|
13
13
|
*/
|
|
14
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
14
15
|
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
15
16
|
/**
|
|
16
17
|
* Minimal request wrapper
|
|
@@ -21,6 +22,10 @@ declare class MinimalRequest implements FastRequest {
|
|
|
21
22
|
private readonly _path;
|
|
22
23
|
private readonly _routePattern?;
|
|
23
24
|
private _searchParams;
|
|
25
|
+
private _cachedQueries;
|
|
26
|
+
private _cachedJsonPromise;
|
|
27
|
+
private _cachedTextPromise;
|
|
28
|
+
private _cachedFormDataPromise;
|
|
24
29
|
constructor(_request: Request, _params: Record<string, string>, _path: string, _routePattern?: string | undefined);
|
|
25
30
|
get url(): string;
|
|
26
31
|
get method(): string;
|
|
@@ -53,6 +58,7 @@ declare class MinimalRequest implements FastRequest {
|
|
|
53
58
|
export declare class MinimalContext implements IFastContext {
|
|
54
59
|
readonly req: MinimalRequest;
|
|
55
60
|
private _resHeaders;
|
|
61
|
+
private _requestScope;
|
|
56
62
|
constructor(request: Request, params: Record<string, string>, path: string, routePattern?: string);
|
|
57
63
|
private getHeaders;
|
|
58
64
|
json<T>(data: T, status?: number): Response;
|
|
@@ -71,6 +77,21 @@ export declare class MinimalContext implements IFastContext {
|
|
|
71
77
|
forward(target: string, _options?: any): Promise<Response>;
|
|
72
78
|
get<T>(_key: string): T;
|
|
73
79
|
set(_key: string, _value: any): void;
|
|
80
|
+
/**
|
|
81
|
+
* Get the request-scoped service manager for this request.
|
|
82
|
+
*
|
|
83
|
+
* @returns The RequestScopeManager for this request.
|
|
84
|
+
*/
|
|
85
|
+
requestScope(): RequestScopeManager;
|
|
86
|
+
/**
|
|
87
|
+
* Resolve a request-scoped service (convenience method).
|
|
88
|
+
*
|
|
89
|
+
* @template T - The service type.
|
|
90
|
+
* @param key - The service key for caching.
|
|
91
|
+
* @param factory - Factory function to create the service.
|
|
92
|
+
* @returns The cached or newly created service instance.
|
|
93
|
+
*/
|
|
94
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
74
95
|
route: (name: string, params?: any, query?: any) => string;
|
|
75
96
|
get native(): this;
|
|
76
97
|
init(_request: Request, _params?: Record<string, string>, _path?: string): this;
|
|
@@ -32,6 +32,9 @@ export interface FastContext {
|
|
|
32
32
|
/** Context Variables */
|
|
33
33
|
get<T>(key: string): T;
|
|
34
34
|
set(key: string, value: any): void;
|
|
35
|
+
/** Request Scope Management */
|
|
36
|
+
requestScope(): any;
|
|
37
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
35
38
|
/** Lifecycle helpers */
|
|
36
39
|
route: (name: string, params?: any, query?: any) => string;
|
|
37
40
|
readonly native: any;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RequestScope-Aware Error Handling
|
|
3
|
+
*
|
|
4
|
+
* Integrates RequestScope lifecycle with error handling to provide:
|
|
5
|
+
* - Error context with request-scoped resources
|
|
6
|
+
* - Automatic cleanup of scoped services on error
|
|
7
|
+
* - Request tracing and diagnostics
|
|
8
|
+
* - Resource leak detection
|
|
9
|
+
*/
|
|
10
|
+
import type { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
11
|
+
import type { RequestScopeMetrics } from '../Container/RequestScopeMetrics';
|
|
12
|
+
import type { GravitoContext } from '../http/types';
|
|
13
|
+
/**
|
|
14
|
+
* Extended error context with RequestScope information
|
|
15
|
+
*
|
|
16
|
+
* Provides error handlers access to request-scoped resources
|
|
17
|
+
* for proper resource cleanup and error diagnostics.
|
|
18
|
+
*/
|
|
19
|
+
export interface RequestScopeErrorContext {
|
|
20
|
+
/**
|
|
21
|
+
* The original error that was thrown
|
|
22
|
+
*/
|
|
23
|
+
error: unknown;
|
|
24
|
+
/**
|
|
25
|
+
* HTTP context where error occurred
|
|
26
|
+
*/
|
|
27
|
+
context: GravitoContext;
|
|
28
|
+
/**
|
|
29
|
+
* RequestScope manager for this request
|
|
30
|
+
* Allows error handlers to access or clean up scoped resources
|
|
31
|
+
*/
|
|
32
|
+
scope?: RequestScopeManager;
|
|
33
|
+
/**
|
|
34
|
+
* Metrics about the request scope state
|
|
35
|
+
* Useful for diagnostics and understanding resource usage
|
|
36
|
+
*/
|
|
37
|
+
scopeMetrics?: RequestScopeMetrics;
|
|
38
|
+
/**
|
|
39
|
+
* Number of scoped services at time of error
|
|
40
|
+
* High numbers might indicate resource leaks
|
|
41
|
+
*/
|
|
42
|
+
scopeSize?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Request processing time in milliseconds
|
|
45
|
+
* Useful for timeout errors
|
|
46
|
+
*/
|
|
47
|
+
duration?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Additional diagnostic information
|
|
50
|
+
*/
|
|
51
|
+
diagnostics?: {
|
|
52
|
+
servicesCleanedUp?: string[];
|
|
53
|
+
cleanupErrors?: Array<{
|
|
54
|
+
service: string;
|
|
55
|
+
error: unknown;
|
|
56
|
+
}>;
|
|
57
|
+
peakMemoryMb?: number;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Error that occurred during RequestScope cleanup
|
|
62
|
+
*
|
|
63
|
+
* Wraps original error with cleanup context for proper error reporting
|
|
64
|
+
*/
|
|
65
|
+
export declare class RequestScopeCleanupError extends Error {
|
|
66
|
+
originalError: unknown;
|
|
67
|
+
cleanupErrors: Array<{
|
|
68
|
+
service: string;
|
|
69
|
+
error: unknown;
|
|
70
|
+
}>;
|
|
71
|
+
constructor(message: string, originalError: unknown, cleanupErrors: Array<{
|
|
72
|
+
service: string;
|
|
73
|
+
error: unknown;
|
|
74
|
+
}>);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Helper to extract RequestScope context from GravitoContext
|
|
78
|
+
*
|
|
79
|
+
* @param ctx - Gravito context
|
|
80
|
+
* @returns RequestScope error context with available information
|
|
81
|
+
*/
|
|
82
|
+
export declare function extractRequestScopeErrorContext(ctx: GravitoContext, error: unknown): RequestScopeErrorContext;
|
|
83
|
+
/**
|
|
84
|
+
* Cleanup scoped services safely during error handling
|
|
85
|
+
*
|
|
86
|
+
* Ensures all scoped services are cleaned up even if some fail,
|
|
87
|
+
* and collects cleanup errors for diagnostics.
|
|
88
|
+
*
|
|
89
|
+
* @param scope - RequestScope manager
|
|
90
|
+
* @returns Array of cleanup errors if any occurred
|
|
91
|
+
*/
|
|
92
|
+
export declare function cleanupRequestScopeOnError(scope?: RequestScopeManager): Promise<Array<{
|
|
93
|
+
service: string;
|
|
94
|
+
error: unknown;
|
|
95
|
+
}>>;
|
|
96
|
+
/**
|
|
97
|
+
* Safe error handler wrapper that manages RequestScope cleanup
|
|
98
|
+
*
|
|
99
|
+
* Ensures scoped services are properly cleaned up before returning error response.
|
|
100
|
+
* Use this to wrap error handlers to make them RequestScope-aware.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const errorHandler = withRequestScopeCleanup(async (ctx, error) => {
|
|
105
|
+
* // Handle error...
|
|
106
|
+
* return ctx.json({ error: error.message }, 500)
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* // In your app:
|
|
110
|
+
* try {
|
|
111
|
+
* // Handle request...
|
|
112
|
+
* } catch (error) {
|
|
113
|
+
* return errorHandler(ctx, error)
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function withRequestScopeCleanup<T extends (ctx: GravitoContext, error: unknown) => Promise<Response>>(handler: T): T;
|
|
118
|
+
/**
|
|
119
|
+
* Detect potential resource leaks in RequestScope
|
|
120
|
+
*
|
|
121
|
+
* Returns diagnostic information about suspicious resource usage patterns
|
|
122
|
+
*/
|
|
123
|
+
export declare function detectRequestScopeLeaks(context: RequestScopeErrorContext): {
|
|
124
|
+
potentialLeaks: boolean;
|
|
125
|
+
warnings: string[];
|
|
126
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Event System Backpressure Management
|
|
3
|
+
*
|
|
4
|
+
* Implements a backpressure management system to prevent high-priority events
|
|
5
|
+
* from starving low-priority events when the queue is under resource constraints.
|
|
6
|
+
*
|
|
7
|
+
* 背壓管理器:在資源受限時進行智慧型流量控制,防止優先級飢餓。
|
|
8
|
+
*
|
|
9
|
+
* FS-103 增強:
|
|
10
|
+
* - 多優先級隊列深度監控
|
|
11
|
+
* - 背壓反饋迴路支持
|
|
12
|
+
* - 智能 DLQ 路由決策
|
|
13
|
+
*/
|
|
14
|
+
import type { DeadLetterDecision, MultiPriorityQueueDepth } from './types';
|
|
15
|
+
/**
|
|
16
|
+
* 背壓狀態枚舉。
|
|
17
|
+
*
|
|
18
|
+
* 狀態轉換:Normal → Warning → Critical → Overflow
|
|
19
|
+
* 恢復方向:遲滯設計(需降至觸發閾值的 80%)
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare enum BackpressureState {
|
|
24
|
+
/** 正常運作,無背壓 */
|
|
25
|
+
NORMAL = "NORMAL",
|
|
26
|
+
/** 警告狀態,開始限制低優先級事件 */
|
|
27
|
+
WARNING = "WARNING",
|
|
28
|
+
/** 危急狀態,僅允許高優先級事件 */
|
|
29
|
+
CRITICAL = "CRITICAL",
|
|
30
|
+
/** 溢位狀態,全部拒絕 */
|
|
31
|
+
OVERFLOW = "OVERFLOW"
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 背壓配置選項。
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export interface BackpressureConfig {
|
|
39
|
+
/** 是否啟用背壓管理器(預設 true) */
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
/** 總隊列深度限制(預設無限) */
|
|
42
|
+
maxQueueSize?: number;
|
|
43
|
+
/** 分優先級隊列深度限制 */
|
|
44
|
+
maxSizeByPriority?: {
|
|
45
|
+
critical?: number;
|
|
46
|
+
high?: number;
|
|
47
|
+
normal?: number;
|
|
48
|
+
low?: number;
|
|
49
|
+
};
|
|
50
|
+
/** 每秒最大入隊速率(events/sec,預設無限) */
|
|
51
|
+
maxEnqueueRate?: number;
|
|
52
|
+
/** 速率限制滑動視窗大小(ms,預設 1000) */
|
|
53
|
+
rateLimitWindowMs?: number;
|
|
54
|
+
/** 背壓狀態閾值(佔 maxQueueSize 的百分比) */
|
|
55
|
+
thresholds?: {
|
|
56
|
+
/** WARNING 觸發百分比(預設 0.6 = 60%) */
|
|
57
|
+
warning?: number;
|
|
58
|
+
/** CRITICAL 觸發百分比(預設 0.85 = 85%) */
|
|
59
|
+
critical?: number;
|
|
60
|
+
/** OVERFLOW 觸發百分比(預設 1.0 = 100%) */
|
|
61
|
+
overflow?: number;
|
|
62
|
+
};
|
|
63
|
+
/** 被拒絕事件的處理策略(預設 'drop-with-callback') */
|
|
64
|
+
rejectionPolicy?: 'throw' | 'drop-silent' | 'drop-with-callback';
|
|
65
|
+
/** 當 rejectionPolicy 為 'drop-with-callback' 時的回呼 */
|
|
66
|
+
onRejected?: (eventName: string, priority: string, reason: string) => void;
|
|
67
|
+
/** 背壓狀態變更回呼 */
|
|
68
|
+
onStateChange?: (from: BackpressureState, to: BackpressureState) => void;
|
|
69
|
+
/** 低優先級事件在 WARNING 狀態下的延遲入隊時間(ms,預設 100) */
|
|
70
|
+
lowPriorityDelayMs?: number;
|
|
71
|
+
/** 是否啟用優先級反轉防護(預設 true) */
|
|
72
|
+
enableStarvationProtection?: boolean;
|
|
73
|
+
/** 低優先級事件最大等待時間,超過則提升優先級(ms,預設 5000) */
|
|
74
|
+
starvationTimeoutMs?: number;
|
|
75
|
+
/** 當進入 OVERFLOW 狀態時,是否將被拒絕事件路由到 DLQ(預設 false) */
|
|
76
|
+
dlqOnOverflow?: boolean;
|
|
77
|
+
/** OVERFLOW 時的重試策略(預設 'dlq-only') */
|
|
78
|
+
overflowRetryStrategy?: 'immediate' | 'delayed' | 'dlq-only';
|
|
79
|
+
/** OVERFLOW 延遲重試的基礎延遲時間(ms,預設 5000) */
|
|
80
|
+
overflowRetryDelayMs?: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 背壓決策結果。
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
export interface BackpressureDecision {
|
|
88
|
+
/** 是否允許入隊 */
|
|
89
|
+
allowed: boolean;
|
|
90
|
+
/** 拒絕原因(若不允許) */
|
|
91
|
+
reason?: string;
|
|
92
|
+
/** 是否需要延遲入隊 */
|
|
93
|
+
delayed?: boolean;
|
|
94
|
+
/** 延遲時間(ms) */
|
|
95
|
+
delayMs?: number;
|
|
96
|
+
/** 建議降級後的優先級(若降級) */
|
|
97
|
+
degradedPriority?: 'high' | 'normal' | 'low';
|
|
98
|
+
/** 是否是由於 OVERFLOW 狀態被拒絕 */
|
|
99
|
+
isOverflow?: boolean;
|
|
100
|
+
/** OVERFLOW 時的重試策略建議('immediate'、'delayed'、'dlq-only') */
|
|
101
|
+
retryStrategy?: 'immediate' | 'delayed' | 'dlq-only';
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 背壓指標快照。
|
|
105
|
+
*
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
108
|
+
export interface BackpressureMetricsSnapshot {
|
|
109
|
+
state: BackpressureState;
|
|
110
|
+
totalDepth: number;
|
|
111
|
+
depthByPriority: {
|
|
112
|
+
critical: number;
|
|
113
|
+
high: number;
|
|
114
|
+
normal: number;
|
|
115
|
+
low: number;
|
|
116
|
+
};
|
|
117
|
+
enqueueRate: number;
|
|
118
|
+
rejectedCount: number;
|
|
119
|
+
degradedCount: number;
|
|
120
|
+
stateTransitions: number;
|
|
121
|
+
dlqRouteCount?: number;
|
|
122
|
+
windowAdjustmentCount?: number;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 背壓管理器。
|
|
126
|
+
*
|
|
127
|
+
* 根據隊列深度、速率、優先級等因素,決定是否允許新事件入隊,
|
|
128
|
+
* 以及是否需要降級優先級或延遲入隊。
|
|
129
|
+
*
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
export declare class BackpressureManager {
|
|
133
|
+
private enabled;
|
|
134
|
+
private config;
|
|
135
|
+
private onRejected?;
|
|
136
|
+
private onStateChange?;
|
|
137
|
+
private state;
|
|
138
|
+
private rejectedCount;
|
|
139
|
+
private degradedCount;
|
|
140
|
+
private stateTransitions;
|
|
141
|
+
private rateCounter;
|
|
142
|
+
private depthByPriority;
|
|
143
|
+
private windowAdjustmentHistory;
|
|
144
|
+
private dlqRouteCount;
|
|
145
|
+
constructor(config?: BackpressureConfig);
|
|
146
|
+
/**
|
|
147
|
+
* 評估是否允許新事件入隊。
|
|
148
|
+
*
|
|
149
|
+
* @param eventName 事件名稱
|
|
150
|
+
* @param priority 事件優先級
|
|
151
|
+
* @param queueDepth 當前隊列深度
|
|
152
|
+
* @param depthByPriority 分優先級隊列深度
|
|
153
|
+
* @returns 背壓決策結果
|
|
154
|
+
*/
|
|
155
|
+
evaluate(eventName: string, priority: 'critical' | 'high' | 'normal' | 'low', queueDepth: number, depthByPriority: {
|
|
156
|
+
critical: number;
|
|
157
|
+
high: number;
|
|
158
|
+
normal: number;
|
|
159
|
+
low: number;
|
|
160
|
+
}): BackpressureDecision;
|
|
161
|
+
/**
|
|
162
|
+
* 獲取當前背壓狀態。
|
|
163
|
+
*/
|
|
164
|
+
getState(): BackpressureState;
|
|
165
|
+
/**
|
|
166
|
+
* 獲取背壓指標快照。
|
|
167
|
+
*/
|
|
168
|
+
getMetrics(): BackpressureMetricsSnapshot;
|
|
169
|
+
/**
|
|
170
|
+
* 重置背壓管理器狀態。
|
|
171
|
+
*/
|
|
172
|
+
reset(): void;
|
|
173
|
+
/**
|
|
174
|
+
* 同步隊列深度(由 EventPriorityQueue 調用)。
|
|
175
|
+
* FS-103:多優先級隊列深度監控
|
|
176
|
+
*/
|
|
177
|
+
updateQueueDepth(depths: MultiPriorityQueueDepth): void;
|
|
178
|
+
/**
|
|
179
|
+
* 獲取各優先級的隊列深度。
|
|
180
|
+
* FS-103:提供實時隊列深度快照
|
|
181
|
+
*/
|
|
182
|
+
getQueueDepthByPriority(): MultiPriorityQueueDepth;
|
|
183
|
+
/**
|
|
184
|
+
* 獲取總隊列深度。
|
|
185
|
+
*/
|
|
186
|
+
getTotalQueueDepth(): number;
|
|
187
|
+
/**
|
|
188
|
+
* 接收來自 AggregationWindow 的窗口調整通知。
|
|
189
|
+
* FS-103:背壓反饋迴路
|
|
190
|
+
*/
|
|
191
|
+
notifyWindowAdjustment(oldWindowMs: number, newWindowMs: number): void;
|
|
192
|
+
/**
|
|
193
|
+
* 檢查是否可以從 CRITICAL 或更高級別降級。
|
|
194
|
+
* FS-103:自動狀態恢復機制
|
|
195
|
+
*/
|
|
196
|
+
private checkStateRecovery;
|
|
197
|
+
/**
|
|
198
|
+
* 決定是否應該將事件路由到死信隊列。
|
|
199
|
+
* FS-103:智能 DLQ 路由決策
|
|
200
|
+
*/
|
|
201
|
+
makeDeadLetterDecision(_eventName: string, priority: 'critical' | 'high' | 'normal' | 'low'): DeadLetterDecision;
|
|
202
|
+
/**
|
|
203
|
+
* 更新背壓狀態。
|
|
204
|
+
* 使用遲滯設計(80% 回復比例)避免邊界震盪。
|
|
205
|
+
*/
|
|
206
|
+
private updateState;
|
|
207
|
+
/**
|
|
208
|
+
* 執行狀態轉換。
|
|
209
|
+
*/
|
|
210
|
+
private transitionTo;
|
|
211
|
+
/**
|
|
212
|
+
* 建立決策結果並記錄拒絕。
|
|
213
|
+
*/
|
|
214
|
+
private createDecision;
|
|
215
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { EventOptions } from './EventOptions';
|
|
2
|
+
/**
|
|
3
|
+
* Source of DLQ entry - reason why event entered the DLQ.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type DLQEntrySource = 'retry_exhausted' | 'circuit_breaker' | 'backpressure_overflow' | 'manual';
|
|
2
7
|
/**
|
|
3
8
|
* Dead Letter Queue entry representing a failed event.
|
|
4
9
|
* @public
|
|
@@ -44,6 +49,10 @@ export interface DLQEntry {
|
|
|
44
49
|
* Timestamp when the event was last retried (if any).
|
|
45
50
|
*/
|
|
46
51
|
lastRetriedAt?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Source of the DLQ entry - reason why event entered the DLQ.
|
|
54
|
+
*/
|
|
55
|
+
source: DLQEntrySource;
|
|
47
56
|
}
|
|
48
57
|
/**
|
|
49
58
|
* Filter options for querying DLQ entries.
|
|
@@ -67,6 +76,11 @@ export interface DLQFilter {
|
|
|
67
76
|
*/
|
|
68
77
|
limit?: number;
|
|
69
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Callback type for DLQ entry events.
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
export type DLQEntryCallback = (entry: DLQEntry) => void;
|
|
70
84
|
/**
|
|
71
85
|
* Dead Letter Queue Manager for handling failed events.
|
|
72
86
|
*
|
|
@@ -78,6 +92,15 @@ export interface DLQFilter {
|
|
|
78
92
|
export declare class DeadLetterQueue {
|
|
79
93
|
private entries;
|
|
80
94
|
private entryIdCounter;
|
|
95
|
+
private maxEntries?;
|
|
96
|
+
private onEntryAdded?;
|
|
97
|
+
private onEntryRemoved?;
|
|
98
|
+
/**
|
|
99
|
+
* Create a new DeadLetterQueue instance.
|
|
100
|
+
*
|
|
101
|
+
* @param maxEntries - Maximum number of entries to keep (optional, no limit if not set)
|
|
102
|
+
*/
|
|
103
|
+
constructor(maxEntries?: number);
|
|
81
104
|
/**
|
|
82
105
|
* Add a failed event to the Dead Letter Queue.
|
|
83
106
|
*
|
|
@@ -87,9 +110,10 @@ export declare class DeadLetterQueue {
|
|
|
87
110
|
* @param error - Error that caused the failure
|
|
88
111
|
* @param retryCount - Number of retry attempts made
|
|
89
112
|
* @param firstFailedAt - Timestamp of first failure
|
|
113
|
+
* @param source - Source of the DLQ entry (default: 'retry_exhausted')
|
|
90
114
|
* @returns DLQ entry ID
|
|
91
115
|
*/
|
|
92
|
-
add(eventName: string, payload: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number): string;
|
|
116
|
+
add(eventName: string, payload: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number, source?: DLQEntrySource): string;
|
|
93
117
|
/**
|
|
94
118
|
* Get a specific DLQ entry by ID.
|
|
95
119
|
*
|
|
@@ -142,4 +166,54 @@ export declare class DeadLetterQueue {
|
|
|
142
166
|
* @internal
|
|
143
167
|
*/
|
|
144
168
|
updateLastRetried(entryId: string): void;
|
|
169
|
+
/**
|
|
170
|
+
* Evict the oldest entry from the DLQ.
|
|
171
|
+
* Used when capacity limit is reached.
|
|
172
|
+
*
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
private evictOldest;
|
|
176
|
+
/**
|
|
177
|
+
* Get the oldest entry in the DLQ.
|
|
178
|
+
*
|
|
179
|
+
* @returns Oldest DLQ entry or undefined if empty
|
|
180
|
+
*/
|
|
181
|
+
getOldestEntry(): DLQEntry | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* Get the newest entry in the DLQ.
|
|
184
|
+
*
|
|
185
|
+
* @returns Newest DLQ entry or undefined if empty
|
|
186
|
+
*/
|
|
187
|
+
getNewestEntry(): DLQEntry | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* Get all entries grouped by source.
|
|
190
|
+
*
|
|
191
|
+
* @param source - Source to filter by
|
|
192
|
+
* @returns Array of entries matching the source
|
|
193
|
+
*/
|
|
194
|
+
getEntriesBySource(source: DLQEntrySource): DLQEntry[];
|
|
195
|
+
/**
|
|
196
|
+
* Set callback for when an entry is added to the DLQ.
|
|
197
|
+
*
|
|
198
|
+
* @param callback - Callback function or undefined to clear
|
|
199
|
+
*/
|
|
200
|
+
setOnEntryAdded(callback?: DLQEntryCallback): void;
|
|
201
|
+
/**
|
|
202
|
+
* Set callback for when an entry is removed from the DLQ.
|
|
203
|
+
*
|
|
204
|
+
* @param callback - Callback function or undefined to clear
|
|
205
|
+
*/
|
|
206
|
+
setOnEntryRemoved(callback?: DLQEntryCallback): void;
|
|
207
|
+
/**
|
|
208
|
+
* Get the maximum number of entries allowed in the DLQ.
|
|
209
|
+
*
|
|
210
|
+
* @returns Max entries limit or undefined if no limit
|
|
211
|
+
*/
|
|
212
|
+
getMaxEntries(): number | undefined;
|
|
213
|
+
/**
|
|
214
|
+
* Set the maximum number of entries allowed in the DLQ.
|
|
215
|
+
*
|
|
216
|
+
* @param maxEntries - Maximum entries or undefined to remove limit
|
|
217
|
+
*/
|
|
218
|
+
setMaxEntries(maxEntries?: number): void;
|
|
145
219
|
}
|
|
@@ -6,6 +6,7 @@ export interface EventBackend {
|
|
|
6
6
|
/**
|
|
7
7
|
* Enqueue an event for processing.
|
|
8
8
|
* @param task - The event task to process
|
|
9
|
+
* @returns Task ID or 'dropped' if event was rejected (can be async for some backends)
|
|
9
10
|
*/
|
|
10
|
-
enqueue(task: EventTask):
|
|
11
|
+
enqueue(task: EventTask): string | Promise<void>;
|
|
11
12
|
}
|