@gravito/ripple 3.1.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.
Files changed (190) hide show
  1. package/README.md +260 -19
  2. package/dist/atlas/src/DB.d.ts +348 -0
  3. package/dist/atlas/src/OrbitAtlas.d.ts +9 -0
  4. package/dist/atlas/src/config/defineConfig.d.ts +14 -0
  5. package/dist/atlas/src/config/index.d.ts +7 -0
  6. package/dist/atlas/src/config/loadConfig.d.ts +41 -0
  7. package/dist/atlas/src/connection/Connection.d.ts +112 -0
  8. package/dist/atlas/src/connection/ConnectionManager.d.ts +180 -0
  9. package/dist/atlas/src/connection/ReplicaConnectionPool.d.ts +54 -0
  10. package/dist/atlas/src/drivers/BunSQLDriver.d.ts +32 -0
  11. package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +118 -0
  12. package/dist/atlas/src/drivers/MongoDBDriver.d.ts +36 -0
  13. package/dist/atlas/src/drivers/MySQLDriver.d.ts +79 -0
  14. package/dist/atlas/src/drivers/PostgresDriver.d.ts +96 -0
  15. package/dist/atlas/src/drivers/RedisDriver.d.ts +43 -0
  16. package/dist/atlas/src/drivers/SQLiteDriver.d.ts +45 -0
  17. package/dist/atlas/src/drivers/types.d.ts +260 -0
  18. package/dist/atlas/src/errors/index.d.ts +45 -0
  19. package/dist/atlas/src/grammar/Grammar.d.ts +342 -0
  20. package/dist/atlas/src/grammar/MongoGrammar.d.ts +47 -0
  21. package/dist/atlas/src/grammar/MySQLGrammar.d.ts +54 -0
  22. package/dist/atlas/src/grammar/NullGrammar.d.ts +35 -0
  23. package/dist/atlas/src/grammar/PostgresGrammar.d.ts +62 -0
  24. package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +32 -0
  25. package/dist/atlas/src/index.d.ts +79 -0
  26. package/dist/atlas/src/migration/Migration.d.ts +64 -0
  27. package/dist/atlas/src/migration/MigrationRepository.d.ts +65 -0
  28. package/dist/atlas/src/migration/Migrator.d.ts +110 -0
  29. package/dist/atlas/src/migration/index.d.ts +6 -0
  30. package/dist/atlas/src/observability/AtlasMetrics.d.ts +33 -0
  31. package/dist/atlas/src/observability/AtlasObservability.d.ts +15 -0
  32. package/dist/atlas/src/observability/AtlasTracer.d.ts +12 -0
  33. package/dist/atlas/src/observability/index.d.ts +9 -0
  34. package/dist/atlas/src/orm/Repository.d.ts +247 -0
  35. package/dist/atlas/src/orm/index.d.ts +6 -0
  36. package/dist/atlas/src/orm/model/DirtyTracker.d.ts +121 -0
  37. package/dist/atlas/src/orm/model/Model.d.ts +458 -0
  38. package/dist/atlas/src/orm/model/ModelRegistry.d.ts +20 -0
  39. package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +150 -0
  40. package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +36 -0
  41. package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +92 -0
  42. package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +117 -0
  43. package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +64 -0
  44. package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +15 -0
  45. package/dist/atlas/src/orm/model/concerns/index.d.ts +12 -0
  46. package/dist/atlas/src/orm/model/decorators.d.ts +138 -0
  47. package/dist/atlas/src/orm/model/errors.d.ts +52 -0
  48. package/dist/atlas/src/orm/model/index.d.ts +10 -0
  49. package/dist/atlas/src/orm/model/relationships.d.ts +207 -0
  50. package/dist/atlas/src/orm/model/types.d.ts +12 -0
  51. package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +124 -0
  52. package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +54 -0
  53. package/dist/atlas/src/orm/schema/index.d.ts +6 -0
  54. package/dist/atlas/src/orm/schema/types.d.ts +85 -0
  55. package/dist/atlas/src/pool/AdaptivePoolManager.d.ts +98 -0
  56. package/dist/atlas/src/pool/PoolHealthChecker.d.ts +91 -0
  57. package/dist/atlas/src/pool/PoolStrategy.d.ts +129 -0
  58. package/dist/atlas/src/pool/PoolWarmer.d.ts +92 -0
  59. package/dist/atlas/src/query/Expression.d.ts +60 -0
  60. package/dist/atlas/src/query/NPlusOneDetector.d.ts +10 -0
  61. package/dist/atlas/src/query/QueryBuilder.d.ts +643 -0
  62. package/dist/atlas/src/query/RelationshipResolver.d.ts +23 -0
  63. package/dist/atlas/src/query/clauses/GroupByClause.d.ts +51 -0
  64. package/dist/atlas/src/query/clauses/HavingClause.d.ts +70 -0
  65. package/dist/atlas/src/query/clauses/JoinClause.d.ts +87 -0
  66. package/dist/atlas/src/query/clauses/LimitClause.d.ts +82 -0
  67. package/dist/atlas/src/query/clauses/OrderByClause.d.ts +69 -0
  68. package/dist/atlas/src/query/clauses/SelectClause.d.ts +71 -0
  69. package/dist/atlas/src/query/clauses/WhereClause.d.ts +167 -0
  70. package/dist/atlas/src/query/clauses/index.d.ts +11 -0
  71. package/dist/atlas/src/schema/Blueprint.d.ts +276 -0
  72. package/dist/atlas/src/schema/ColumnDefinition.d.ts +154 -0
  73. package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +37 -0
  74. package/dist/atlas/src/schema/MigrationGenerator.d.ts +45 -0
  75. package/dist/atlas/src/schema/Schema.d.ts +131 -0
  76. package/dist/atlas/src/schema/SchemaDiff.d.ts +73 -0
  77. package/dist/atlas/src/schema/TypeGenerator.d.ts +57 -0
  78. package/dist/atlas/src/schema/TypeWriter.d.ts +42 -0
  79. package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +23 -0
  80. package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +26 -0
  81. package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +28 -0
  82. package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +97 -0
  83. package/dist/atlas/src/schema/grammars/index.d.ts +7 -0
  84. package/dist/atlas/src/schema/index.d.ts +8 -0
  85. package/dist/atlas/src/seed/Factory.d.ts +90 -0
  86. package/dist/atlas/src/seed/Seeder.d.ts +28 -0
  87. package/dist/atlas/src/seed/SeederRunner.d.ts +74 -0
  88. package/dist/atlas/src/seed/index.d.ts +6 -0
  89. package/dist/atlas/src/sharding/ShardingManager.d.ts +59 -0
  90. package/dist/atlas/src/types/index.d.ts +1182 -0
  91. package/dist/atlas/src/utils/CursorEncoding.d.ts +63 -0
  92. package/dist/atlas/src/utils/levenshtein.d.ts +9 -0
  93. package/dist/core/src/CommandKernel.d.ts +33 -0
  94. package/dist/core/src/ConfigManager.d.ts +39 -0
  95. package/dist/core/src/Container/RequestScopeManager.d.ts +62 -0
  96. package/dist/core/src/Container/RequestScopeMetrics.d.ts +144 -0
  97. package/dist/core/src/Container.d.ts +86 -11
  98. package/dist/core/src/ErrorHandler.d.ts +3 -0
  99. package/dist/core/src/HookManager.d.ts +511 -4
  100. package/dist/core/src/PlanetCore.d.ts +89 -0
  101. package/dist/core/src/RequestContext.d.ts +97 -0
  102. package/dist/core/src/Router.d.ts +1 -5
  103. package/dist/core/src/ServiceProvider.d.ts +22 -0
  104. package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +1 -0
  105. package/dist/core/src/adapters/PhotonAdapter.d.ts +5 -0
  106. package/dist/core/src/adapters/bun/BunContext.d.ts +4 -0
  107. package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +1 -0
  108. package/dist/core/src/adapters/types.d.ts +27 -0
  109. package/dist/core/src/cli/queue-commands.d.ts +6 -0
  110. package/dist/core/src/engine/AOTRouter.d.ts +7 -12
  111. package/dist/core/src/engine/FastContext.d.ts +27 -2
  112. package/dist/core/src/engine/Gravito.d.ts +0 -1
  113. package/dist/core/src/engine/MinimalContext.d.ts +25 -2
  114. package/dist/core/src/engine/types.d.ts +9 -1
  115. package/dist/core/src/error-handling/RequestScopeErrorContext.d.ts +126 -0
  116. package/dist/core/src/events/BackpressureManager.d.ts +215 -0
  117. package/dist/core/src/events/CircuitBreaker.d.ts +229 -0
  118. package/dist/core/src/events/DeadLetterQueue.d.ts +219 -0
  119. package/dist/core/src/events/EventBackend.d.ts +12 -0
  120. package/dist/core/src/events/EventOptions.d.ts +204 -0
  121. package/dist/core/src/events/EventPriorityQueue.d.ts +301 -0
  122. package/dist/core/src/events/FlowControlStrategy.d.ts +109 -0
  123. package/dist/core/src/events/IdempotencyCache.d.ts +60 -0
  124. package/dist/core/src/events/MessageQueueBridge.d.ts +184 -0
  125. package/dist/core/src/events/PriorityEscalationManager.d.ts +82 -0
  126. package/dist/core/src/events/RetryScheduler.d.ts +104 -0
  127. package/dist/core/src/events/WorkerPool.d.ts +98 -0
  128. package/dist/core/src/events/WorkerPoolConfig.d.ts +153 -0
  129. package/dist/core/src/events/WorkerPoolMetrics.d.ts +65 -0
  130. package/dist/core/src/events/aggregation/AggregationWindow.d.ts +77 -0
  131. package/dist/core/src/events/aggregation/DeduplicationManager.d.ts +135 -0
  132. package/dist/core/src/events/aggregation/EventAggregationManager.d.ts +108 -0
  133. package/dist/core/src/events/aggregation/EventBatcher.d.ts +99 -0
  134. package/dist/core/src/events/aggregation/types.d.ts +117 -0
  135. package/dist/core/src/events/index.d.ts +25 -0
  136. package/dist/core/src/events/observability/EventMetrics.d.ts +132 -0
  137. package/dist/core/src/events/observability/EventTracer.d.ts +68 -0
  138. package/dist/core/src/events/observability/EventTracing.d.ts +161 -0
  139. package/dist/core/src/events/observability/OTelEventMetrics.d.ts +332 -0
  140. package/dist/core/src/events/observability/ObservableHookManager.d.ts +108 -0
  141. package/dist/core/src/events/observability/StreamWorkerMetrics.d.ts +76 -0
  142. package/dist/core/src/events/observability/index.d.ts +24 -0
  143. package/dist/core/src/events/observability/metrics-types.d.ts +16 -0
  144. package/dist/core/src/events/types.d.ts +134 -0
  145. package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
  146. package/dist/core/src/exceptions/index.d.ts +1 -0
  147. package/dist/core/src/health/HealthProvider.d.ts +67 -0
  148. package/dist/core/src/http/types.d.ts +40 -0
  149. package/dist/core/src/index.d.ts +25 -4
  150. package/dist/core/src/instrumentation/index.d.ts +35 -0
  151. package/dist/core/src/instrumentation/opentelemetry.d.ts +178 -0
  152. package/dist/core/src/instrumentation/types.d.ts +182 -0
  153. package/dist/core/src/observability/Metrics.d.ts +244 -0
  154. package/dist/core/src/observability/QueueDashboard.d.ts +136 -0
  155. package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +350 -0
  156. package/dist/core/src/reliability/RetryPolicy.d.ts +217 -0
  157. package/dist/core/src/reliability/index.d.ts +6 -0
  158. package/dist/core/src/router/ControllerDispatcher.d.ts +12 -0
  159. package/dist/core/src/router/RequestValidator.d.ts +20 -0
  160. package/dist/index.js +6709 -9888
  161. package/dist/index.js.map +64 -62
  162. package/dist/photon/src/index.d.ts +19 -0
  163. package/dist/photon/src/middleware/ratelimit-redis.d.ts +50 -0
  164. package/dist/photon/src/middleware/ratelimit.d.ts +161 -0
  165. package/dist/photon/src/openapi.d.ts +19 -0
  166. package/dist/proto/ripple.proto +120 -0
  167. package/dist/ripple/src/RippleServer.d.ts +64 -445
  168. package/dist/ripple/src/channels/ChannelManager.d.ts +25 -10
  169. package/dist/ripple/src/drivers/NATSDriver.d.ts +87 -0
  170. package/dist/ripple/src/drivers/RedisDriver.d.ts +30 -1
  171. package/dist/ripple/src/drivers/index.d.ts +1 -0
  172. package/dist/ripple/src/engines/BunEngine.d.ts +98 -0
  173. package/dist/ripple/src/engines/IRippleEngine.d.ts +205 -0
  174. package/dist/ripple/src/engines/UWebSocketsEngine.d.ts +97 -0
  175. package/dist/ripple/src/engines/WsEngine.d.ts +69 -0
  176. package/dist/ripple/src/engines/index.d.ts +15 -0
  177. package/dist/ripple/src/index.d.ts +2 -0
  178. package/dist/ripple/src/middleware/InterceptorManager.d.ts +21 -0
  179. package/dist/ripple/src/observability/RippleMetrics.d.ts +24 -0
  180. package/dist/ripple/src/reliability/AckManager.d.ts +48 -0
  181. package/dist/ripple/src/serializers/ISerializer.d.ts +39 -0
  182. package/dist/ripple/src/serializers/JsonSerializer.d.ts +19 -0
  183. package/dist/ripple/src/serializers/ProtobufSerializer.d.ts +41 -0
  184. package/dist/ripple/src/serializers/index.d.ts +3 -0
  185. package/dist/ripple/src/tracking/SessionManager.d.ts +104 -0
  186. package/dist/ripple/src/tracking/index.d.ts +1 -0
  187. package/dist/ripple/src/types.d.ts +188 -12
  188. package/dist/ripple/src/utils/MessageSerializer.d.ts +33 -23
  189. package/dist/ripple/src/utils/TokenBucket.d.ts +25 -0
  190. package/package.json +25 -8
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Event dispatch options for async event handling.
3
+ * @public
4
+ */
5
+ export interface EventOptions {
6
+ /**
7
+ * Whether to dispatch the event asynchronously.
8
+ * @default false
9
+ */
10
+ async?: boolean;
11
+ /**
12
+ * Priority level for event processing.
13
+ * - 'critical': Immediate processing, bypass queue (< 1ms)
14
+ * - 'high': High priority events (< 50ms)
15
+ * - 'normal': Standard events (< 200ms)
16
+ * - 'low': Non-critical events (< 500ms)
17
+ * @default 'normal'
18
+ */
19
+ priority?: 'critical' | 'high' | 'normal' | 'low';
20
+ /**
21
+ * Automatic priority escalation configuration.
22
+ * Events can be automatically upgraded to higher priority based on wait time.
23
+ */
24
+ escalation?: {
25
+ /**
26
+ * Whether to enable automatic priority escalation.
27
+ * @default true
28
+ */
29
+ enabled?: boolean;
30
+ /**
31
+ * Escalation thresholds in milliseconds.
32
+ * Events exceeding these wait times are promoted.
33
+ */
34
+ thresholds?: {
35
+ /**
36
+ * Wait time before LOW events are promoted to NORMAL.
37
+ * @default 200
38
+ */
39
+ lowToNormal?: number;
40
+ /**
41
+ * Wait time before NORMAL events are promoted to HIGH.
42
+ * @default 100
43
+ */
44
+ normalToHigh?: number;
45
+ /**
46
+ * Wait time before HIGH events are promoted to CRITICAL.
47
+ * @default 50
48
+ */
49
+ highToCritical?: number;
50
+ };
51
+ /**
52
+ * Maximum wait time before forcing CRITICAL priority.
53
+ * @default 500
54
+ */
55
+ maxWaitTimeMs?: number;
56
+ };
57
+ /**
58
+ * Execution timeout in milliseconds.
59
+ * If a listener exceeds this timeout, it will be terminated.
60
+ * @default 5000
61
+ */
62
+ timeout?: number;
63
+ /**
64
+ * Ordering guarantee strategy.
65
+ * - 'strict': Global strict ordering (slow, serialized)
66
+ * - 'partition': Partition-based ordering (recommended, balanced)
67
+ * - 'none': No ordering guarantee (fastest, parallel)
68
+ * @default 'none'
69
+ */
70
+ ordering?: 'strict' | 'partition' | 'none';
71
+ /**
72
+ * Partition key for partition-based ordering.
73
+ * Events with the same partition key are processed in order.
74
+ * Only used when ordering is 'partition'.
75
+ * @example 'order:123' or 'user:456'
76
+ */
77
+ partitionKey?: string;
78
+ /**
79
+ * Idempotency key for deduplication.
80
+ * Events with the same idempotency key within the TTL window
81
+ * will be processed only once.
82
+ * @example 'order:123:created'
83
+ */
84
+ idempotencyKey?: string;
85
+ /**
86
+ * Time-to-live for idempotency key in milliseconds.
87
+ * @default 3600000 (1 hour)
88
+ */
89
+ ttl?: number;
90
+ /**
91
+ * Retry policy for failed event processing.
92
+ */
93
+ retry?: {
94
+ /**
95
+ * Maximum number of retry attempts.
96
+ * @default 0
97
+ */
98
+ maxRetries?: number;
99
+ /**
100
+ * Backoff strategy for retries.
101
+ * - 'exponential': Delay doubles with each retry (1s, 2s, 4s, 8s...)
102
+ * - 'linear': Fixed delay between retries
103
+ * @default 'exponential'
104
+ */
105
+ backoff?: 'exponential' | 'linear';
106
+ /**
107
+ * Initial delay in milliseconds before first retry.
108
+ * @default 1000
109
+ */
110
+ initialDelayMs?: number;
111
+ /**
112
+ * Maximum delay in milliseconds between retries.
113
+ * @default 30000
114
+ */
115
+ maxDelayMs?: number;
116
+ /**
117
+ * Whether to send failed events to Dead Letter Queue after max retries.
118
+ * @default false
119
+ */
120
+ dlqAfterMaxRetries?: boolean;
121
+ };
122
+ /**
123
+ * Circuit breaker options for this event.
124
+ */
125
+ circuitBreaker?: {
126
+ /**
127
+ * Number of consecutive failures before opening the circuit.
128
+ * @default 5
129
+ */
130
+ failureThreshold?: number;
131
+ /**
132
+ * Time in milliseconds to wait before attempting to close the circuit.
133
+ * @default 30000
134
+ */
135
+ resetTimeout?: number;
136
+ /**
137
+ * Number of test requests to allow in half-open state.
138
+ * @default 3
139
+ */
140
+ halfOpenRequests?: number;
141
+ };
142
+ /**
143
+ * Event aggregation configuration (FS-102).
144
+ * Enables deduplication and micro-batching for improved throughput.
145
+ * @default undefined (disabled)
146
+ */
147
+ aggregation?: {
148
+ /**
149
+ * Enable event aggregation.
150
+ * @default false
151
+ */
152
+ enabled?: boolean;
153
+ /**
154
+ * Aggregation window size in milliseconds.
155
+ * Backpressure-aware adjustment: 50-500ms
156
+ * @default 200
157
+ */
158
+ windowMs?: number;
159
+ /**
160
+ * Batch size threshold for auto-flush.
161
+ * @default 50
162
+ */
163
+ batchSize?: number;
164
+ /**
165
+ * Deduplication strategy.
166
+ * @default 'pattern'
167
+ */
168
+ deduplication?: 'pattern' | 'idempotencyKey' | 'off';
169
+ /**
170
+ * Deduplication pattern (string or function).
171
+ * String: hook-based pattern
172
+ * Function: custom pattern from event args
173
+ */
174
+ pattern?: string | ((args: unknown) => string);
175
+ /**
176
+ * Priority merge strategy.
177
+ * - 'highest': keep highest priority event
178
+ * - 'earliest': keep earliest event
179
+ * - 'latest': keep latest event
180
+ * @default 'highest'
181
+ */
182
+ mergePriority?: 'highest' | 'earliest' | 'latest';
183
+ /**
184
+ * Enable automatic cleanup of expired entries.
185
+ * @default true
186
+ */
187
+ enableCleanup?: boolean;
188
+ /**
189
+ * Cleanup interval in milliseconds.
190
+ * @default 300000 (5 minutes)
191
+ */
192
+ cleanupIntervalMs?: number;
193
+ /**
194
+ * TTL for entries in milliseconds.
195
+ * @default 600000 (10 minutes)
196
+ */
197
+ ttlMs?: number;
198
+ };
199
+ }
200
+ /**
201
+ * Default event options.
202
+ * @internal
203
+ */
204
+ export declare const DEFAULT_EVENT_OPTIONS: Required<EventOptions>;
@@ -0,0 +1,301 @@
1
+ import type { Span } from '@opentelemetry/api';
2
+ import type { ActionCallback } from '../HookManager';
3
+ import { BackpressureManager } from './BackpressureManager';
4
+ import { CircuitBreaker } from './CircuitBreaker';
5
+ import type { DeadLetterQueue } from './DeadLetterQueue';
6
+ import type { EventBackend } from './EventBackend';
7
+ import type { EventOptions } from './EventOptions';
8
+ import type { EventMetrics } from './observability/EventMetrics';
9
+ import type { EventTracing } from './observability/EventTracing';
10
+ import type { OTelEventMetrics } from './observability/OTelEventMetrics';
11
+ import { type PriorityStatistics } from './PriorityEscalationManager';
12
+ import type { RetryScheduler } from './RetryScheduler';
13
+ import type { BackpressureStrategy, EventQueueConfig, EventTask, MultiPriorityQueueDepth } from './types';
14
+ import type { WorkerPool } from './WorkerPool';
15
+ export type { EventTask, EventQueueConfig, BackpressureStrategy };
16
+ /**
17
+ * Priority queue for event processing.
18
+ * Events are processed based on their priority level:
19
+ * - Critical priority events are processed first (< 1ms)
20
+ * - High priority events are processed second (< 50ms)
21
+ * - Normal priority events are processed third (< 200ms)
22
+ * - Low priority events are processed last (< 500ms)
23
+ *
24
+ * Supports automatic priority escalation based on wait time.
25
+ *
26
+ * @internal
27
+ */
28
+ export declare class EventPriorityQueue implements EventBackend {
29
+ private criticalPriority;
30
+ private highPriority;
31
+ private normalPriority;
32
+ private lowPriority;
33
+ private processing;
34
+ private taskIdCounter;
35
+ private dlq?;
36
+ private persistentDLQHandler?;
37
+ private config;
38
+ private processingPartitions;
39
+ private eventCircuitBreakers;
40
+ private eventMetrics?;
41
+ private otelEventMetrics?;
42
+ private eventTracing?;
43
+ private currentDispatchSpan?;
44
+ private backpressureManager?;
45
+ private workerPool?;
46
+ private retryScheduler?;
47
+ private priorityStats?;
48
+ constructor(config?: EventQueueConfig);
49
+ /**
50
+ * Set the Dead Letter Queue for failed events.
51
+ *
52
+ * @param dlq - Dead Letter Queue instance
53
+ */
54
+ setDeadLetterQueue(dlq: DeadLetterQueue): void;
55
+ /**
56
+ * Set the persistent DLQ handler for failed events.
57
+ *
58
+ * @param handler - Async handler function for persistent DLQ
59
+ */
60
+ setPersistentDLQHandler(handler: (hook: string, args: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number) => Promise<void>): void;
61
+ /**
62
+ * Set the EventMetrics instance for recording circuit breaker metrics.
63
+ *
64
+ * @param metrics - EventMetrics instance
65
+ * @internal
66
+ */
67
+ setEventMetrics(metrics: EventMetrics): void;
68
+ /**
69
+ * Set the OTelEventMetrics instance for recording DLQ and backpressure metrics.
70
+ *
71
+ * @param metrics - OTelEventMetrics instance
72
+ * @internal
73
+ */
74
+ setOTelEventMetrics(metrics: OTelEventMetrics): void;
75
+ /**
76
+ * Set the PriorityStatistics instance for tracking priority distribution.
77
+ *
78
+ * @param stats - PriorityStatistics instance
79
+ * @internal
80
+ */
81
+ setPriorityStatistics(stats: PriorityStatistics): void;
82
+ /**
83
+ * Get the PriorityStatistics instance.
84
+ *
85
+ * @returns PriorityStatistics instance or undefined
86
+ * @internal
87
+ */
88
+ getPriorityStatistics(): PriorityStatistics | undefined;
89
+ /**
90
+ * Set the EventTracing instance for distributed tracing.
91
+ *
92
+ * @param tracing - EventTracing instance
93
+ * @internal
94
+ */
95
+ setEventTracing(tracing: EventTracing): void;
96
+ /**
97
+ * Set the current dispatch span for creating child spans.
98
+ *
99
+ * @param span - Parent dispatch span
100
+ * @internal
101
+ */
102
+ setCurrentDispatchSpan(span: Span | undefined): void;
103
+ /**
104
+ * Get the current dispatch span.
105
+ *
106
+ * @returns Current dispatch span or undefined
107
+ * @internal
108
+ */
109
+ getCurrentDispatchSpan(): Span | undefined;
110
+ /**
111
+ * Set the RetryScheduler for async distributed retries.
112
+ *
113
+ * @param scheduler - RetryScheduler instance
114
+ * @internal
115
+ */
116
+ setRetryScheduler(scheduler: RetryScheduler): void;
117
+ /**
118
+ * Get the RetryScheduler instance.
119
+ *
120
+ * @returns RetryScheduler instance or undefined
121
+ * @internal
122
+ */
123
+ getRetryScheduler(): RetryScheduler | undefined;
124
+ /**
125
+ * Get or create a circuit breaker for an event hook.
126
+ *
127
+ * @param hook - Event hook name
128
+ * @param config - Circuit breaker configuration
129
+ * @returns Circuit breaker instance or undefined if not configured
130
+ * @internal
131
+ */
132
+ private getOrCreateEventCircuitBreaker;
133
+ /**
134
+ * Enqueue an event task for processing.
135
+ *
136
+ * @param hook - Event hook name
137
+ * @param args - Event arguments
138
+ * @param callbacks - Callbacks to execute
139
+ * @param options - Event options
140
+ * @returns Task ID
141
+ */
142
+ enqueue(task: EventTask): string;
143
+ enqueue(hook: string, args: unknown, callbacks: ActionCallback[], options: EventOptions): string;
144
+ /**
145
+ * Handle backpressure when queue is full.
146
+ * @returns True if space was made, false if event should be dropped
147
+ */
148
+ private handleBackpressure;
149
+ /**
150
+ * Drop the oldest event from the queue, prioritizing low priority events.
151
+ * Drops in order: LOW → NORMAL → HIGH → CRITICAL
152
+ */
153
+ private dropOldest;
154
+ /**
155
+ * Process the next task in the queue.
156
+ * Tasks are processed in priority order: high > normal > low
157
+ * If WorkerPool is configured, tasks are submitted to the pool for concurrent execution.
158
+ *
159
+ * @internal
160
+ */
161
+ private processNext;
162
+ /**
163
+ * Dequeue the next task based on priority and partition ordering.
164
+ * Priority order: CRITICAL > HIGH > NORMAL > LOW
165
+ *
166
+ * @returns Next task to process, or undefined if queue is empty
167
+ * @internal
168
+ */
169
+ private dequeue;
170
+ /**
171
+ * Dequeue a task from a priority queue, respecting partition ordering.
172
+ *
173
+ * @param queue - Priority queue to dequeue from
174
+ * @returns Next task to process, or undefined if all tasks are blocked by partition locks
175
+ * @internal
176
+ */
177
+ private dequeueFromPriority;
178
+ /**
179
+ * Execute an event task by running all its callbacks.
180
+ * Implements circuit breaker protection, retry logic with exponential backoff, and DLQ integration.
181
+ * Also handles partition ordering by acquiring and releasing partition locks.
182
+ *
183
+ * @param task - Event task to execute
184
+ * @internal
185
+ */
186
+ private executeTask;
187
+ /**
188
+ * Calculate retry delay based on backoff strategy.
189
+ *
190
+ * @param retryCount - Current retry attempt number
191
+ * @param backoff - Backoff strategy
192
+ * @param initialDelay - Initial delay in ms
193
+ * @param maxDelay - Maximum delay in ms
194
+ * @returns Delay in milliseconds
195
+ * @internal
196
+ */
197
+ private calculateRetryDelay;
198
+ /**
199
+ * Re-enqueue a task for retry.
200
+ *
201
+ * @param task - Task to retry
202
+ * @internal
203
+ */
204
+ private enqueueRetry;
205
+ /**
206
+ * Execute a callback with timeout.
207
+ *
208
+ * @param callback - Callback to execute
209
+ * @param args - Arguments to pass to callback
210
+ * @param timeoutMs - Timeout in milliseconds
211
+ * @internal
212
+ */
213
+ private executeWithTimeout;
214
+ /**
215
+ * Get the current depth of the queue.
216
+ *
217
+ * @returns Total number of tasks in the queue
218
+ */
219
+ getDepth(): number;
220
+ /**
221
+ * Get the depth of a specific priority queue.
222
+ *
223
+ * @param priority - Priority level
224
+ * @returns Number of tasks in the specified priority queue
225
+ */
226
+ getDepthByPriority(priority: 'critical' | 'high' | 'normal' | 'low'): number;
227
+ /**
228
+ * Get the BackpressureManager instance if enabled.
229
+ *
230
+ * @returns BackpressureManager instance or undefined if not enabled
231
+ * @internal
232
+ */
233
+ getBackpressureManager(): BackpressureManager | undefined;
234
+ /**
235
+ * Set the WorkerPool for concurrent task execution.
236
+ *
237
+ * @param pool - WorkerPool instance
238
+ * @internal
239
+ */
240
+ setWorkerPool(pool: WorkerPool): void;
241
+ /**
242
+ * Get the WorkerPool instance if set.
243
+ *
244
+ * @returns WorkerPool instance or undefined if not set
245
+ * @internal
246
+ */
247
+ getWorkerPool(): WorkerPool | undefined;
248
+ /**
249
+ * Clear all tasks from the queue.
250
+ */
251
+ clear(): void;
252
+ /**
253
+ * Get circuit breaker for an event hook.
254
+ *
255
+ * @param hook - Event hook name
256
+ * @returns Circuit breaker instance or undefined
257
+ * @internal
258
+ */
259
+ getCircuitBreaker(hook: string): CircuitBreaker | undefined;
260
+ /**
261
+ * Get all circuit breakers.
262
+ *
263
+ * @returns Map of circuit breakers keyed by hook name
264
+ * @internal
265
+ */
266
+ getCircuitBreakers(): Map<string, CircuitBreaker>;
267
+ /**
268
+ * Reset a circuit breaker for an event hook.
269
+ *
270
+ * @param hook - Event hook name
271
+ * @returns True if reset, false if circuit breaker not found
272
+ * @internal
273
+ */
274
+ resetCircuitBreaker(hook: string): boolean;
275
+ /**
276
+ * Batch enqueue multiple tasks (FS-102).
277
+ * Supports aggregation layer batch submission.
278
+ *
279
+ * @param tasks - Array of tasks to enqueue
280
+ * @returns Array of task IDs
281
+ * @internal
282
+ */
283
+ enqueueBatch(tasks: EventTask[]): string[];
284
+ /**
285
+ * Get queue depth for each priority level (FS-103).
286
+ * Used by BackpressureManager for multi-priority depth monitoring.
287
+ *
288
+ * @returns Queue depth snapshot with depths for each priority
289
+ * @internal
290
+ */
291
+ getQueueDepthByPriority(): MultiPriorityQueueDepth;
292
+ /**
293
+ * Sync queue depth to BackpressureManager (FS-103).
294
+ * Called whenever queue depth changes to keep BackpressureManager
295
+ * synchronized with real-time queue state.
296
+ *
297
+ * @private
298
+ * @internal
299
+ */
300
+ private syncBackpressure;
301
+ }
@@ -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
+ }