@gravito/echo 3.0.0 → 3.1.0
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 +211 -0
- package/dist/atlas/src/DB.d.ts +301 -0
- package/dist/atlas/src/OrbitAtlas.d.ts +9 -0
- package/dist/atlas/src/config/defineConfig.d.ts +14 -0
- package/dist/atlas/src/config/index.d.ts +7 -0
- package/dist/atlas/src/config/loadConfig.d.ts +48 -0
- package/dist/atlas/src/connection/Connection.d.ts +108 -0
- package/dist/atlas/src/connection/ConnectionManager.d.ts +111 -0
- package/dist/atlas/src/drivers/BunSQLDriver.d.ts +32 -0
- package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +118 -0
- package/dist/atlas/src/drivers/MongoDBDriver.d.ts +36 -0
- package/dist/atlas/src/drivers/MySQLDriver.d.ts +66 -0
- package/dist/atlas/src/drivers/PostgresDriver.d.ts +83 -0
- package/dist/atlas/src/drivers/RedisDriver.d.ts +43 -0
- package/dist/atlas/src/drivers/SQLiteDriver.d.ts +45 -0
- package/dist/atlas/src/drivers/types.d.ts +260 -0
- package/dist/atlas/src/errors/index.d.ts +45 -0
- package/dist/atlas/src/grammar/Grammar.d.ts +342 -0
- package/dist/atlas/src/grammar/MongoGrammar.d.ts +47 -0
- package/dist/atlas/src/grammar/MySQLGrammar.d.ts +54 -0
- package/dist/atlas/src/grammar/NullGrammar.d.ts +35 -0
- package/dist/atlas/src/grammar/PostgresGrammar.d.ts +62 -0
- package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +32 -0
- package/dist/atlas/src/index.d.ts +67 -0
- package/dist/atlas/src/migration/Migration.d.ts +64 -0
- package/dist/atlas/src/migration/MigrationRepository.d.ts +65 -0
- package/dist/atlas/src/migration/Migrator.d.ts +110 -0
- package/dist/atlas/src/migration/index.d.ts +6 -0
- package/dist/atlas/src/observability/AtlasMetrics.d.ts +11 -0
- package/dist/atlas/src/observability/AtlasObservability.d.ts +15 -0
- package/dist/atlas/src/observability/AtlasTracer.d.ts +12 -0
- package/dist/atlas/src/observability/index.d.ts +9 -0
- package/dist/atlas/src/orm/index.d.ts +5 -0
- package/dist/atlas/src/orm/model/DirtyTracker.d.ts +121 -0
- package/dist/atlas/src/orm/model/Model.d.ts +449 -0
- package/dist/atlas/src/orm/model/ModelRegistry.d.ts +20 -0
- package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +136 -0
- package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +36 -0
- package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +87 -0
- package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +117 -0
- package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +64 -0
- package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +15 -0
- package/dist/atlas/src/orm/model/concerns/index.d.ts +12 -0
- package/dist/atlas/src/orm/model/decorators.d.ts +109 -0
- package/dist/atlas/src/orm/model/errors.d.ts +52 -0
- package/dist/atlas/src/orm/model/index.d.ts +10 -0
- package/dist/atlas/src/orm/model/relationships.d.ts +207 -0
- package/dist/atlas/src/orm/model/types.d.ts +12 -0
- package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +123 -0
- package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +54 -0
- package/dist/atlas/src/orm/schema/index.d.ts +6 -0
- package/dist/atlas/src/orm/schema/types.d.ts +85 -0
- package/dist/atlas/src/query/Expression.d.ts +60 -0
- package/dist/atlas/src/query/NPlusOneDetector.d.ts +10 -0
- package/dist/atlas/src/query/QueryBuilder.d.ts +573 -0
- package/dist/atlas/src/query/clauses/GroupByClause.d.ts +51 -0
- package/dist/atlas/src/query/clauses/HavingClause.d.ts +70 -0
- package/dist/atlas/src/query/clauses/JoinClause.d.ts +87 -0
- package/dist/atlas/src/query/clauses/LimitClause.d.ts +82 -0
- package/dist/atlas/src/query/clauses/OrderByClause.d.ts +69 -0
- package/dist/atlas/src/query/clauses/SelectClause.d.ts +71 -0
- package/dist/atlas/src/query/clauses/WhereClause.d.ts +167 -0
- package/dist/atlas/src/query/clauses/index.d.ts +11 -0
- package/dist/atlas/src/schema/Blueprint.d.ts +276 -0
- package/dist/atlas/src/schema/ColumnDefinition.d.ts +154 -0
- package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +37 -0
- package/dist/atlas/src/schema/Schema.d.ts +131 -0
- package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +23 -0
- package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +26 -0
- package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +28 -0
- package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +97 -0
- package/dist/atlas/src/schema/grammars/index.d.ts +7 -0
- package/dist/atlas/src/schema/index.d.ts +8 -0
- package/dist/atlas/src/seed/Factory.d.ts +90 -0
- package/dist/atlas/src/seed/Seeder.d.ts +28 -0
- package/dist/atlas/src/seed/SeederRunner.d.ts +74 -0
- package/dist/atlas/src/seed/index.d.ts +6 -0
- package/dist/atlas/src/types/index.d.ts +1100 -0
- package/dist/atlas/src/utils/levenshtein.d.ts +9 -0
- package/dist/core/src/Application.d.ts +215 -0
- package/dist/core/src/CommandKernel.d.ts +33 -0
- package/dist/core/src/ConfigManager.d.ts +26 -0
- package/dist/core/src/Container.d.ts +108 -0
- package/dist/core/src/ErrorHandler.d.ts +63 -0
- package/dist/core/src/Event.d.ts +5 -0
- package/dist/core/src/EventManager.d.ts +123 -0
- package/dist/core/src/GlobalErrorHandlers.d.ts +47 -0
- package/dist/core/src/GravitoServer.d.ts +28 -0
- package/dist/core/src/HookManager.d.ts +496 -0
- package/dist/core/src/Listener.d.ts +4 -0
- package/dist/core/src/Logger.d.ts +20 -0
- package/dist/core/src/PlanetCore.d.ts +289 -0
- package/dist/core/src/Route.d.ts +36 -0
- package/dist/core/src/Router.d.ts +284 -0
- package/dist/core/src/ServiceProvider.d.ts +156 -0
- package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/core/src/adapters/PhotonAdapter.d.ts +171 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +45 -0
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +31 -0
- package/dist/core/src/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/core/src/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +31 -0
- package/dist/core/src/adapters/bun/types.d.ts +20 -0
- package/dist/core/src/adapters/photon-types.d.ts +73 -0
- package/dist/core/src/adapters/types.d.ts +235 -0
- package/dist/core/src/engine/AOTRouter.d.ts +124 -0
- package/dist/core/src/engine/FastContext.d.ts +100 -0
- package/dist/core/src/engine/Gravito.d.ts +137 -0
- package/dist/core/src/engine/MinimalContext.d.ts +79 -0
- package/dist/core/src/engine/analyzer.d.ts +27 -0
- package/dist/core/src/engine/constants.d.ts +23 -0
- package/dist/core/src/engine/index.d.ts +26 -0
- package/dist/core/src/engine/path.d.ts +26 -0
- package/dist/core/src/engine/pool.d.ts +83 -0
- package/dist/core/src/engine/types.d.ts +143 -0
- package/dist/core/src/events/CircuitBreaker.d.ts +229 -0
- package/dist/core/src/events/DeadLetterQueue.d.ts +145 -0
- package/dist/core/src/events/EventBackend.d.ts +11 -0
- package/dist/core/src/events/EventOptions.d.ts +109 -0
- package/dist/core/src/events/EventPriorityQueue.d.ts +202 -0
- package/dist/core/src/events/IdempotencyCache.d.ts +60 -0
- package/dist/core/src/events/index.d.ts +14 -0
- package/dist/core/src/events/observability/EventMetrics.d.ts +132 -0
- package/dist/core/src/events/observability/EventTracer.d.ts +68 -0
- package/dist/core/src/events/observability/EventTracing.d.ts +161 -0
- package/dist/core/src/events/observability/OTelEventMetrics.d.ts +240 -0
- package/dist/core/src/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/core/src/events/observability/index.d.ts +20 -0
- package/dist/core/src/events/observability/metrics-types.d.ts +16 -0
- package/dist/core/src/events/types.d.ts +75 -0
- package/dist/core/src/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/core/src/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/core/src/exceptions/GravitoException.d.ts +23 -0
- package/dist/core/src/exceptions/HttpException.d.ts +9 -0
- package/dist/core/src/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/core/src/exceptions/ValidationException.d.ts +22 -0
- package/dist/core/src/exceptions/index.d.ts +7 -0
- package/dist/core/src/helpers/Arr.d.ts +19 -0
- package/dist/core/src/helpers/Str.d.ts +23 -0
- package/dist/core/src/helpers/data.d.ts +25 -0
- package/dist/core/src/helpers/errors.d.ts +34 -0
- package/dist/core/src/helpers/response.d.ts +41 -0
- package/dist/core/src/helpers.d.ts +338 -0
- package/dist/core/src/http/CookieJar.d.ts +51 -0
- package/dist/core/src/http/cookie.d.ts +29 -0
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +16 -0
- package/dist/core/src/http/middleware/Cors.d.ts +24 -0
- package/dist/core/src/http/middleware/Csrf.d.ts +23 -0
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +28 -0
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +29 -0
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +18 -0
- package/dist/core/src/http/types.d.ts +355 -0
- package/dist/core/src/index.d.ts +76 -0
- package/dist/core/src/instrumentation/index.d.ts +35 -0
- package/dist/core/src/instrumentation/opentelemetry.d.ts +178 -0
- package/dist/core/src/instrumentation/types.d.ts +182 -0
- package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +316 -0
- package/dist/core/src/reliability/RetryPolicy.d.ts +217 -0
- package/dist/core/src/reliability/index.d.ts +6 -0
- package/dist/core/src/router/ControllerDispatcher.d.ts +12 -0
- package/dist/core/src/router/RequestValidator.d.ts +20 -0
- package/dist/core/src/runtime.d.ts +119 -0
- package/dist/core/src/security/Encrypter.d.ts +33 -0
- package/dist/core/src/security/Hasher.d.ts +29 -0
- package/dist/core/src/testing/HttpTester.d.ts +39 -0
- package/dist/core/src/testing/TestResponse.d.ts +78 -0
- package/dist/core/src/testing/index.d.ts +2 -0
- package/dist/core/src/types/events.d.ts +94 -0
- package/dist/echo/src/OrbitEcho.d.ts +115 -0
- package/dist/echo/src/dlq/DeadLetterQueue.d.ts +94 -0
- package/dist/echo/src/dlq/MemoryDeadLetterQueue.d.ts +36 -0
- package/dist/echo/src/dlq/index.d.ts +2 -0
- package/dist/echo/src/index.d.ts +64 -0
- package/dist/echo/src/middleware/RequestBufferMiddleware.d.ts +62 -0
- package/dist/echo/src/middleware/index.d.ts +8 -0
- package/dist/echo/src/observability/index.d.ts +3 -0
- package/dist/echo/src/observability/logging/ConsoleEchoLogger.d.ts +37 -0
- package/dist/echo/src/observability/logging/EchoLogger.d.ts +38 -0
- package/dist/echo/src/observability/logging/index.d.ts +2 -0
- package/dist/echo/src/observability/metrics/MetricsProvider.d.ts +69 -0
- package/dist/echo/src/observability/metrics/NoopMetricsProvider.d.ts +17 -0
- package/dist/echo/src/observability/metrics/PrometheusMetricsProvider.d.ts +39 -0
- package/dist/echo/src/observability/metrics/index.d.ts +3 -0
- package/dist/echo/src/observability/tracing/NoopTracer.d.ts +33 -0
- package/dist/echo/src/observability/tracing/Tracer.d.ts +75 -0
- package/dist/echo/src/observability/tracing/index.d.ts +2 -0
- package/dist/echo/src/providers/GenericProvider.d.ts +53 -0
- package/dist/echo/src/providers/GitHubProvider.d.ts +35 -0
- package/dist/echo/src/providers/LinearProvider.d.ts +27 -0
- package/dist/echo/src/providers/PaddleProvider.d.ts +31 -0
- package/dist/echo/src/providers/ShopifyProvider.d.ts +27 -0
- package/dist/echo/src/providers/SlackProvider.d.ts +27 -0
- package/dist/echo/src/providers/StripeProvider.d.ts +38 -0
- package/dist/echo/src/providers/TwilioProvider.d.ts +31 -0
- package/dist/echo/src/providers/base/BaseProvider.d.ts +87 -0
- package/dist/echo/src/providers/base/HeaderUtils.d.ts +34 -0
- package/dist/echo/src/providers/index.d.ts +14 -0
- package/dist/echo/src/receive/SignatureValidator.d.ts +67 -0
- package/dist/echo/src/receive/WebhookReceiver.d.ts +185 -0
- package/dist/echo/src/receive/index.d.ts +2 -0
- package/dist/echo/src/replay/WebhookReplayService.d.ts +35 -0
- package/dist/echo/src/replay/index.d.ts +1 -0
- package/dist/echo/src/resilience/CircuitBreaker.d.ts +117 -0
- package/dist/echo/src/resilience/index.d.ts +10 -0
- package/dist/echo/src/rotation/KeyRotationManager.d.ts +127 -0
- package/dist/echo/src/rotation/index.d.ts +10 -0
- package/dist/echo/src/send/WebhookDispatcher.d.ts +198 -0
- package/dist/echo/src/send/index.d.ts +1 -0
- package/dist/echo/src/storage/MemoryWebhookStore.d.ts +14 -0
- package/dist/echo/src/storage/WebhookStore.d.ts +236 -0
- package/dist/echo/src/storage/index.d.ts +2 -0
- package/dist/echo/src/types.d.ts +756 -0
- package/dist/index.js +1332 -190
- package/dist/index.js.map +28 -10
- package/dist/photon/src/index.d.ts +84 -0
- package/dist/photon/src/middleware/binary.d.ts +31 -0
- package/dist/photon/src/middleware/htmx.d.ts +39 -0
- package/dist/photon/src/middleware/ratelimit.d.ts +157 -0
- package/dist/photon/src/openapi.d.ts +19 -0
- package/package.json +7 -5
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - OpenTelemetry 類型定義
|
|
3
|
+
*
|
|
4
|
+
* 定義 OpenTelemetry SDK 集成所需的類型與介面。
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Tracing(追蹤)導出器類型
|
|
8
|
+
*/
|
|
9
|
+
export type TracingExporter = 'jaeger' | 'otlp' | 'console' | 'none';
|
|
10
|
+
/**
|
|
11
|
+
* Metrics(指標)導出器類型
|
|
12
|
+
*/
|
|
13
|
+
export type MetricsExporter = 'prometheus' | 'otlp' | 'console' | 'none';
|
|
14
|
+
/**
|
|
15
|
+
* Tracing 配置選項
|
|
16
|
+
*/
|
|
17
|
+
export interface TracingConfig {
|
|
18
|
+
/** 是否啟用追蹤(預設:true) */
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
/** 導出器類型(預設:otlp) */
|
|
21
|
+
exporter?: TracingExporter;
|
|
22
|
+
/** Jaeger HTTP 端點(使用 jaeger 導出器時) */
|
|
23
|
+
jaegerEndpoint?: string;
|
|
24
|
+
/** OTLP 端點(使用 otlp 導出器時) */
|
|
25
|
+
otlpEndpoint?: string;
|
|
26
|
+
/** 採樣率(0-1,預設:1.0 = 100%) */
|
|
27
|
+
samplingRate?: number;
|
|
28
|
+
/** 是否啟用自動儀器化(預設:false) */
|
|
29
|
+
autoInstrumentation?: boolean;
|
|
30
|
+
/** Batch Span Processor 配置 */
|
|
31
|
+
batchConfig?: {
|
|
32
|
+
/** 最大批次大小(預設:512) */
|
|
33
|
+
maxExportBatchSize?: number;
|
|
34
|
+
/** 導出間隔(毫秒,預設:5000) */
|
|
35
|
+
scheduledDelayMillis?: number;
|
|
36
|
+
/** 導出超時(毫秒,預設:30000) */
|
|
37
|
+
exportTimeoutMillis?: number;
|
|
38
|
+
/** 最大隊列大小(預設:2048) */
|
|
39
|
+
maxQueueSize?: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Metrics 配置選項
|
|
44
|
+
*/
|
|
45
|
+
export interface MetricsConfig {
|
|
46
|
+
/** 是否啟用指標(預設:true) */
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
/** 導出器類型(預設:otlp) */
|
|
49
|
+
exporter?: MetricsExporter;
|
|
50
|
+
/** Prometheus 端口(使用 prometheus 導出器時,預設:9090) */
|
|
51
|
+
prometheusPort?: number;
|
|
52
|
+
/** Prometheus 端點路徑(預設:/metrics) */
|
|
53
|
+
prometheusEndpoint?: string;
|
|
54
|
+
/** OTLP 端點(使用 otlp 導出器時) */
|
|
55
|
+
otlpEndpoint?: string;
|
|
56
|
+
/** 導出間隔(毫秒,預設:60000) */
|
|
57
|
+
exportIntervalMillis?: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* OpenTelemetry SDK 完整配置
|
|
61
|
+
*/
|
|
62
|
+
export interface OpenTelemetryConfig {
|
|
63
|
+
/** 是否啟用 OpenTelemetry(預設:true) */
|
|
64
|
+
enabled?: boolean;
|
|
65
|
+
/** 服務名稱(預設:gravito-app) */
|
|
66
|
+
serviceName?: string;
|
|
67
|
+
/** 服務版本(預設:1.0.0) */
|
|
68
|
+
serviceVersion?: string;
|
|
69
|
+
/** 環境名稱(預設:development) */
|
|
70
|
+
environment?: 'development' | 'staging' | 'production' | string;
|
|
71
|
+
/** 自定義 Resource Attributes */
|
|
72
|
+
resourceAttributes?: Record<string, string | number | boolean>;
|
|
73
|
+
/** Tracing 配置 */
|
|
74
|
+
tracing?: TracingConfig;
|
|
75
|
+
/** Metrics 配置 */
|
|
76
|
+
metrics?: MetricsConfig;
|
|
77
|
+
/** 是否在控制台輸出初始化日誌(預設:true) */
|
|
78
|
+
logInitialization?: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* OpenTelemetry SDK 實例包裝
|
|
82
|
+
*
|
|
83
|
+
* 封裝初始化後的 SDK 實例與相關資源。
|
|
84
|
+
*/
|
|
85
|
+
export interface OpenTelemetrySDK {
|
|
86
|
+
/** SDK 是否已啟動 */
|
|
87
|
+
isStarted: boolean;
|
|
88
|
+
/** SDK 是否已關閉 */
|
|
89
|
+
isShutdown: boolean;
|
|
90
|
+
/** 服務名稱 */
|
|
91
|
+
serviceName: string;
|
|
92
|
+
/** 服務版本 */
|
|
93
|
+
serviceVersion: string;
|
|
94
|
+
/** Tracer Provider(如果啟用追蹤) */
|
|
95
|
+
tracerProvider: unknown | null;
|
|
96
|
+
/** Meter Provider(如果啟用指標) */
|
|
97
|
+
meterProvider: unknown | null;
|
|
98
|
+
/** 原始配置 */
|
|
99
|
+
config: OpenTelemetryConfig;
|
|
100
|
+
/** 關閉 SDK */
|
|
101
|
+
shutdown: () => Promise<void>;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 支援的 OpenTelemetry 環境變數
|
|
105
|
+
*
|
|
106
|
+
* 參考:https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/
|
|
107
|
+
*/
|
|
108
|
+
export declare const OTEL_ENV_VARS: {
|
|
109
|
+
/** 服務名稱 */
|
|
110
|
+
readonly SERVICE_NAME: "OTEL_SERVICE_NAME";
|
|
111
|
+
/** 服務版本 */
|
|
112
|
+
readonly SERVICE_VERSION: "OTEL_SERVICE_VERSION";
|
|
113
|
+
/** 環境名稱 */
|
|
114
|
+
readonly DEPLOYMENT_ENVIRONMENT: "OTEL_DEPLOYMENT_ENVIRONMENT";
|
|
115
|
+
/** Resource Attributes */
|
|
116
|
+
readonly RESOURCE_ATTRIBUTES: "OTEL_RESOURCE_ATTRIBUTES";
|
|
117
|
+
/** OTLP 端點(通用) */
|
|
118
|
+
readonly OTLP_ENDPOINT: "OTEL_EXPORTER_OTLP_ENDPOINT";
|
|
119
|
+
/** OTLP Traces 端點 */
|
|
120
|
+
readonly OTLP_TRACES_ENDPOINT: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
|
|
121
|
+
/** OTLP Metrics 端點 */
|
|
122
|
+
readonly OTLP_METRICS_ENDPOINT: "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
|
|
123
|
+
/** 採樣器類型 */
|
|
124
|
+
readonly TRACES_SAMPLER: "OTEL_TRACES_SAMPLER";
|
|
125
|
+
/** 採樣率參數 */
|
|
126
|
+
readonly TRACES_SAMPLER_ARG: "OTEL_TRACES_SAMPLER_ARG";
|
|
127
|
+
/** 是否啟用 OpenTelemetry */
|
|
128
|
+
readonly GRAVITO_ENABLED: "GRAVITO_OTEL_ENABLED";
|
|
129
|
+
/** Jaeger 端點 */
|
|
130
|
+
readonly JAEGER_ENDPOINT: "JAEGER_ENDPOINT";
|
|
131
|
+
/** Prometheus 端口 */
|
|
132
|
+
readonly PROMETHEUS_PORT: "PROMETHEUS_PORT";
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* 完整的 BatchConfig 類型(所有屬性必填)
|
|
136
|
+
*/
|
|
137
|
+
export interface BatchConfigRequired {
|
|
138
|
+
maxExportBatchSize: number;
|
|
139
|
+
scheduledDelayMillis: number;
|
|
140
|
+
exportTimeoutMillis: number;
|
|
141
|
+
maxQueueSize: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 完整的 TracingConfig 類型(所有屬性必填)
|
|
145
|
+
*/
|
|
146
|
+
export interface TracingConfigRequired {
|
|
147
|
+
enabled: boolean;
|
|
148
|
+
exporter: TracingExporter;
|
|
149
|
+
jaegerEndpoint: string;
|
|
150
|
+
otlpEndpoint: string;
|
|
151
|
+
samplingRate: number;
|
|
152
|
+
autoInstrumentation: boolean;
|
|
153
|
+
batchConfig: BatchConfigRequired;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 完整的 MetricsConfig 類型(所有屬性必填)
|
|
157
|
+
*/
|
|
158
|
+
export interface MetricsConfigRequired {
|
|
159
|
+
enabled: boolean;
|
|
160
|
+
exporter: MetricsExporter;
|
|
161
|
+
prometheusPort: number;
|
|
162
|
+
prometheusEndpoint: string;
|
|
163
|
+
otlpEndpoint: string;
|
|
164
|
+
exportIntervalMillis: number;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 完整的 OpenTelemetryConfig 類型(所有屬性必填)
|
|
168
|
+
*/
|
|
169
|
+
export interface OpenTelemetryConfigRequired {
|
|
170
|
+
enabled: boolean;
|
|
171
|
+
serviceName: string;
|
|
172
|
+
serviceVersion: string;
|
|
173
|
+
environment: 'development' | 'staging' | 'production';
|
|
174
|
+
resourceAttributes: Record<string, string | number | boolean>;
|
|
175
|
+
tracing: TracingConfigRequired;
|
|
176
|
+
metrics: MetricsConfigRequired;
|
|
177
|
+
logInitialization: boolean;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 預設配置值
|
|
181
|
+
*/
|
|
182
|
+
export declare const DEFAULT_CONFIG: OpenTelemetryConfigRequired;
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Dead Letter Queue Manager
|
|
3
|
+
*
|
|
4
|
+
* 管理失敗事件的持久化存儲
|
|
5
|
+
* 支持 CRUD、重新入隊、批量重試和統計功能
|
|
6
|
+
*/
|
|
7
|
+
import type { ConnectionContract } from '@gravito/atlas';
|
|
8
|
+
import type { EventOptions } from '../events/EventOptions';
|
|
9
|
+
import type { RetryPolicy } from './RetryPolicy';
|
|
10
|
+
/**
|
|
11
|
+
* DLQ 事件的數據庫記錄
|
|
12
|
+
*/
|
|
13
|
+
export interface DLQRecord {
|
|
14
|
+
id: number;
|
|
15
|
+
dlq_id: string;
|
|
16
|
+
event_name: string;
|
|
17
|
+
event_payload: unknown;
|
|
18
|
+
event_options: unknown;
|
|
19
|
+
attempt_count: number;
|
|
20
|
+
max_retries: number;
|
|
21
|
+
next_retry_at: string | null;
|
|
22
|
+
last_error: unknown;
|
|
23
|
+
status: 'pending' | 'requeued' | 'resolved' | 'abandoned';
|
|
24
|
+
resolution_notes: string | null;
|
|
25
|
+
failed_at: string;
|
|
26
|
+
created_at: string;
|
|
27
|
+
updated_at: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* DLQ 事件查詢過濾選項(for DeadLetterQueueManager)
|
|
31
|
+
*/
|
|
32
|
+
export interface DLQManagerFilter {
|
|
33
|
+
/** 按事件名稱篩選 */
|
|
34
|
+
eventName?: string;
|
|
35
|
+
/** 按狀態篩選 */
|
|
36
|
+
status?: 'pending' | 'requeued' | 'resolved' | 'abandoned';
|
|
37
|
+
/** 開始時間 */
|
|
38
|
+
from?: Date;
|
|
39
|
+
/** 結束時間 */
|
|
40
|
+
to?: Date;
|
|
41
|
+
/** 結果數量限制 */
|
|
42
|
+
limit?: number;
|
|
43
|
+
/** 分頁偏移 */
|
|
44
|
+
offset?: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* DLQ 統計信息
|
|
48
|
+
*/
|
|
49
|
+
export interface DLQStats {
|
|
50
|
+
/** 事件總數 */
|
|
51
|
+
total: number;
|
|
52
|
+
/** 按事件名稱的統計 */
|
|
53
|
+
byEvent: Record<string, number>;
|
|
54
|
+
/** 按狀態的統計 */
|
|
55
|
+
byStatus: Record<string, number>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Dead Letter Queue 管理器
|
|
59
|
+
*
|
|
60
|
+
* 負責管理失敗事件的持久化存儲,支持:
|
|
61
|
+
* - 自動將失敗事件移至 DLQ
|
|
62
|
+
* - 查詢和篩選 DLQ 事件
|
|
63
|
+
* - 重新入隊單個或批量事件
|
|
64
|
+
* - 標記事件為已解決或已放棄
|
|
65
|
+
* - 查看統計信息
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const db = container.make('db') as ConnectionContract
|
|
70
|
+
* const dlqManager = new DeadLetterQueueManager(db)
|
|
71
|
+
*
|
|
72
|
+
* // 將失敗事件移至 DLQ
|
|
73
|
+
* const dlqId = await dlqManager.moveToDlq(
|
|
74
|
+
* 'order:created',
|
|
75
|
+
* { orderId: '123' },
|
|
76
|
+
* { retry: {...} },
|
|
77
|
+
* error,
|
|
78
|
+
* 3
|
|
79
|
+
* )
|
|
80
|
+
*
|
|
81
|
+
* // 查詢 DLQ 事件
|
|
82
|
+
* const events = await dlqManager.list({ event: 'order:created', status: 'pending' })
|
|
83
|
+
*
|
|
84
|
+
* // 重新入隊
|
|
85
|
+
* await dlqManager.requeue(dlqId)
|
|
86
|
+
*
|
|
87
|
+
* // 批量重試
|
|
88
|
+
* const result = await dlqManager.retryBatch({ eventName: 'order:created' })
|
|
89
|
+
*
|
|
90
|
+
* // 統計
|
|
91
|
+
* const stats = await dlqManager.getStats()
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare class DeadLetterQueueManager {
|
|
95
|
+
private db;
|
|
96
|
+
private retryEngine;
|
|
97
|
+
constructor(db: ConnectionContract);
|
|
98
|
+
/**
|
|
99
|
+
* 將失敗事件移至死信隊列
|
|
100
|
+
*
|
|
101
|
+
* @param eventName - 事件名稱
|
|
102
|
+
* @param payload - 事件負載
|
|
103
|
+
* @param options - 事件選項
|
|
104
|
+
* @param error - 導致失敗的錯誤
|
|
105
|
+
* @param attemptCount - 當前嘗試次數
|
|
106
|
+
* @param retryPolicy - 重試策略配置
|
|
107
|
+
* @returns DLQ 記錄的 UUID
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const dlqId = await dlqManager.moveToDlq(
|
|
112
|
+
* 'order:created',
|
|
113
|
+
* { orderId: 'ORD-123' },
|
|
114
|
+
* { retry: { maxRetries: 3, backoff: 'exponential' } },
|
|
115
|
+
* new Error('Service unavailable'),
|
|
116
|
+
* 3,
|
|
117
|
+
* { maxRetries: 3, backoff: 'exponential', initialDelayMs: 1000, maxDelayMs: 30000 }
|
|
118
|
+
* )
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
moveToDlq(eventName: string, payload: unknown, options: EventOptions, error: Error, attemptCount: number, retryPolicy?: RetryPolicy): Promise<string>;
|
|
122
|
+
/**
|
|
123
|
+
* 按 DLQ ID 獲取單個事件
|
|
124
|
+
*
|
|
125
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
126
|
+
* @returns DLQ 記錄,若不存在則返回 undefined
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const event = await dlqManager.getById('550e8400-e29b-41d4-a716-446655440000')
|
|
131
|
+
* if (event) {
|
|
132
|
+
* console.log(event.event_name, event.status)
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
getById(dlqId: string): Promise<DLQRecord | undefined>;
|
|
137
|
+
/**
|
|
138
|
+
* 查詢 DLQ 事件
|
|
139
|
+
*
|
|
140
|
+
* @param filter - 查詢過濾條件
|
|
141
|
+
* @returns DLQ 記錄列表(按失敗時間倒序)
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* // 查詢特定事件的待處理事件
|
|
146
|
+
* const events = await dlqManager.list({
|
|
147
|
+
* eventName: 'order:created',
|
|
148
|
+
* status: 'pending',
|
|
149
|
+
* limit: 50
|
|
150
|
+
* })
|
|
151
|
+
*
|
|
152
|
+
* // 查詢時間範圍內的所有失敗事件
|
|
153
|
+
* const eventsInRange = await dlqManager.list({
|
|
154
|
+
* from: new Date('2026-02-01'),
|
|
155
|
+
* to: new Date('2026-02-03'),
|
|
156
|
+
* limit: 100
|
|
157
|
+
* })
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
list(filter?: DLQManagerFilter): Promise<DLQRecord[]>;
|
|
161
|
+
/**
|
|
162
|
+
* 重新入隊單個 DLQ 事件
|
|
163
|
+
*
|
|
164
|
+
* 重新入隊不會自動派發事件,而是標記狀態為 'requeued'
|
|
165
|
+
* 實際的事件派發應由調用者負責處理
|
|
166
|
+
*
|
|
167
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
168
|
+
* @throws 如果事件不存在
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const event = await dlqManager.getById(dlqId)
|
|
173
|
+
* if (event) {
|
|
174
|
+
* // 由調用者決定如何派發事件
|
|
175
|
+
* await eventSystem.doActionAsync(
|
|
176
|
+
* event.event_name,
|
|
177
|
+
* event.event_payload,
|
|
178
|
+
* event.event_options
|
|
179
|
+
* )
|
|
180
|
+
*
|
|
181
|
+
* // 標記為已重新入隊
|
|
182
|
+
* await dlqManager.requeue(dlqId)
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
requeue(dlqId: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* 批量重新入隊 DLQ 事件
|
|
189
|
+
*
|
|
190
|
+
* @param filter - 查詢過濾條件
|
|
191
|
+
* @returns 包含處理結果的統計對象
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const result = await dlqManager.retryBatch({
|
|
196
|
+
* eventName: 'order:created',
|
|
197
|
+
* status: 'pending'
|
|
198
|
+
* })
|
|
199
|
+
*
|
|
200
|
+
* console.log(`Success: ${result.succeeded}, Failed: ${result.failed}`)
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
retryBatch(filter?: DLQManagerFilter): Promise<{
|
|
204
|
+
total: number;
|
|
205
|
+
succeeded: number;
|
|
206
|
+
failed: number;
|
|
207
|
+
}>;
|
|
208
|
+
/**
|
|
209
|
+
* 標記 DLQ 事件為已解決
|
|
210
|
+
*
|
|
211
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
212
|
+
* @param notes - 解決說明
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* await dlqManager.resolve(dlqId, 'Manual fix applied: Database issue resolved')
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
resolve(dlqId: string, notes?: string): Promise<void>;
|
|
220
|
+
/**
|
|
221
|
+
* 放棄 DLQ 事件(不再重試)
|
|
222
|
+
*
|
|
223
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
224
|
+
* @param reason - 放棄原因
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* await dlqManager.abandon(dlqId, 'Data corrupted, cannot recover')
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
abandon(dlqId: string, reason?: string): Promise<void>;
|
|
232
|
+
/**
|
|
233
|
+
* 更新 DLQ 事件狀態
|
|
234
|
+
*
|
|
235
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
236
|
+
* @param status - 新狀態
|
|
237
|
+
* @param notes - 狀態變更說明
|
|
238
|
+
* @throws 如果事件不存在
|
|
239
|
+
*
|
|
240
|
+
* @internal
|
|
241
|
+
*/
|
|
242
|
+
updateStatus(dlqId: string, status: 'pending' | 'requeued' | 'resolved' | 'abandoned', notes?: string): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* 刪除單個 DLQ 事件
|
|
245
|
+
*
|
|
246
|
+
* @param dlqId - DLQ 事件的 UUID
|
|
247
|
+
* @returns true 如果刪除成功,false 如果事件不存在
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const deleted = await dlqManager.deleteEntry(dlqId)
|
|
252
|
+
* if (deleted) {
|
|
253
|
+
* console.log('Event deleted')
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
deleteEntry(dlqId: string): Promise<boolean>;
|
|
258
|
+
/**
|
|
259
|
+
* 刪除多個 DLQ 事件
|
|
260
|
+
*
|
|
261
|
+
* @param dlqIds - DLQ 事件 UUID 列表
|
|
262
|
+
* @returns 刪除的事件數量
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```typescript
|
|
266
|
+
* const deletedCount = await dlqManager.deleteEntries([id1, id2, id3])
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
deleteEntries(dlqIds: string[]): Promise<number>;
|
|
270
|
+
/**
|
|
271
|
+
* 獲取 DLQ 統計信息
|
|
272
|
+
*
|
|
273
|
+
* @returns 包含總數、按事件名稱和狀態的統計信息
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const stats = await dlqManager.getStats()
|
|
278
|
+
*
|
|
279
|
+
* console.log(`Total events: ${stats.total}`)
|
|
280
|
+
* console.log('By event:', stats.byEvent)
|
|
281
|
+
* // Output: { 'order:created': 145, 'payment:succeeded': 23 }
|
|
282
|
+
*
|
|
283
|
+
* console.log('By status:', stats.byStatus)
|
|
284
|
+
* // Output: { pending: 120, requeued: 15, resolved: 8, abandoned: 2 }
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
getStats(): Promise<DLQStats>;
|
|
288
|
+
/**
|
|
289
|
+
* 獲取某個事件名稱的 DLQ 事件數
|
|
290
|
+
*
|
|
291
|
+
* @param eventName - 事件名稱
|
|
292
|
+
* @returns 事件數量
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* const count = await dlqManager.getCountByEvent('order:created')
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
getCountByEvent(eventName: string): Promise<number>;
|
|
300
|
+
/**
|
|
301
|
+
* 清空所有 DLQ 事件(慎用!)
|
|
302
|
+
*
|
|
303
|
+
* @param includeResolved - 是否包含已解決的事件
|
|
304
|
+
* @returns 清空的事件數量
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* // 只清空待處理的事件
|
|
309
|
+
* const count = await dlqManager.clear(false)
|
|
310
|
+
*
|
|
311
|
+
* // 清空所有事件(包括已解決和已放棄)
|
|
312
|
+
* const countAll = await dlqManager.clear(true)
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
clear(includeResolved?: boolean): Promise<number>;
|
|
316
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Retry Policy
|
|
3
|
+
*
|
|
4
|
+
* 重試策略和退避算法的實現
|
|
5
|
+
* 支持指數和線性退避,包含 Jitter 防止雷鳴羊群
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 重試策略配置接口
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const policy: RetryPolicy = {
|
|
13
|
+
* maxRetries: 3,
|
|
14
|
+
* backoff: 'exponential',
|
|
15
|
+
* initialDelayMs: 1000,
|
|
16
|
+
* maxDelayMs: 30000,
|
|
17
|
+
* dlqAfterMaxRetries: true
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export interface RetryPolicy {
|
|
22
|
+
/** 最大重試次數 */
|
|
23
|
+
maxRetries: number;
|
|
24
|
+
/** 退避策略:指數或線性 */
|
|
25
|
+
backoff: 'exponential' | 'linear';
|
|
26
|
+
/** 初始延遲時間(毫秒) */
|
|
27
|
+
initialDelayMs: number;
|
|
28
|
+
/** 最大延遲時間(毫秒) */
|
|
29
|
+
maxDelayMs: number;
|
|
30
|
+
/** 超過最大重試次數後是否發送到死信隊列 */
|
|
31
|
+
dlqAfterMaxRetries?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 重試引擎
|
|
35
|
+
*
|
|
36
|
+
* 負責計算重試延遲、判斷是否應該重試等邏輯
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const engine = new RetryEngine()
|
|
41
|
+
* const delay = engine.calculateDelay(1, policy)
|
|
42
|
+
* const shouldRetry = engine.shouldRetry(3, policy)
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare class RetryEngine {
|
|
46
|
+
/**
|
|
47
|
+
* 計算下次重試的延遲時間(毫秒)
|
|
48
|
+
*
|
|
49
|
+
* @param attemptCount - 當前嘗試次數(從 1 開始)
|
|
50
|
+
* @param policy - 重試策略配置
|
|
51
|
+
* @returns 延遲時間(毫秒)
|
|
52
|
+
*
|
|
53
|
+
* @description
|
|
54
|
+
* - 指數退避:delay = initialDelay * 2^(attemptCount - 1)
|
|
55
|
+
* - 線性退避:delay = initialDelay * attemptCount
|
|
56
|
+
* - 添加隨機抖動(Jitter),防止雷鳴羊群問題
|
|
57
|
+
* - 結果不超過 maxDelayMs
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const policy: RetryPolicy = {
|
|
62
|
+
* maxRetries: 3,
|
|
63
|
+
* backoff: 'exponential',
|
|
64
|
+
* initialDelayMs: 1000,
|
|
65
|
+
* maxDelayMs: 30000
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // 第 1 次重試:1000ms * 2^0 = 1000ms (加抖動)
|
|
69
|
+
* const delay1 = engine.calculateDelay(1, policy) // ~1100ms
|
|
70
|
+
*
|
|
71
|
+
* // 第 2 次重試:1000ms * 2^1 = 2000ms (加抖動)
|
|
72
|
+
* const delay2 = engine.calculateDelay(2, policy) // ~2200ms
|
|
73
|
+
*
|
|
74
|
+
* // 第 3 次重試:1000ms * 2^2 = 4000ms (加抖動)
|
|
75
|
+
* const delay3 = engine.calculateDelay(3, policy) // ~4400ms
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
calculateDelay(attemptCount: number, policy: RetryPolicy): number;
|
|
79
|
+
/**
|
|
80
|
+
* 判斷是否應該重試
|
|
81
|
+
*
|
|
82
|
+
* @param attemptCount - 當前嘗試次數(從 1 開始)
|
|
83
|
+
* @param policy - 重試策略配置
|
|
84
|
+
* @returns 是否應該重試
|
|
85
|
+
*
|
|
86
|
+
* @description
|
|
87
|
+
* 當 attemptCount < maxRetries 時返回 true
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const policy = { maxRetries: 3, ... }
|
|
92
|
+
*
|
|
93
|
+
* engine.shouldRetry(1, policy) // true(1 < 3)
|
|
94
|
+
* engine.shouldRetry(3, policy) // false(3 >= 3)
|
|
95
|
+
* engine.shouldRetry(4, policy) // false(4 >= 3)
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
shouldRetry(attemptCount: number, policy: RetryPolicy): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* 獲取退避時間(包含延遲計算和 Jitter)
|
|
101
|
+
*
|
|
102
|
+
* @param retryCount - 重試次數(從 0 開始)
|
|
103
|
+
* @param policy - 重試策略配置
|
|
104
|
+
* @returns 退避時間(毫秒)
|
|
105
|
+
*
|
|
106
|
+
* @description
|
|
107
|
+
* 這是 calculateDelay 的別名,但接收的是從 0 開始的重試計數
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const policy = { maxRetries: 3, initialDelayMs: 1000, maxDelayMs: 30000, backoff: 'exponential' }
|
|
112
|
+
*
|
|
113
|
+
* // 第 0 次重試(第 1 次嘗試失敗)
|
|
114
|
+
* const time1 = engine.getBackoffTime(0, policy) // ~1100ms
|
|
115
|
+
*
|
|
116
|
+
* // 第 1 次重試(第 2 次嘗試失敗)
|
|
117
|
+
* const time2 = engine.getBackoffTime(1, policy) // ~2200ms
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
getBackoffTime(retryCount: number, policy: RetryPolicy): number;
|
|
121
|
+
/**
|
|
122
|
+
* 計算下次重試的絕對時間
|
|
123
|
+
*
|
|
124
|
+
* @param retryCount - 重試次數(從 0 開始)
|
|
125
|
+
* @param policy - 重試策略配置
|
|
126
|
+
* @param baseTime - 基礎時間戳(默認為當前時間)
|
|
127
|
+
* @returns 下次重試的時間戳(毫秒)
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const policy = { maxRetries: 3, initialDelayMs: 1000, maxDelayMs: 30000, backoff: 'exponential' }
|
|
132
|
+
*
|
|
133
|
+
* const now = Date.now()
|
|
134
|
+
* const nextRetryTime = engine.getNextRetryTime(0, policy, now)
|
|
135
|
+
* // 返回大約 now + 1000 + jitter
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
getNextRetryTime(retryCount: number, policy: RetryPolicy, baseTime?: number): number;
|
|
139
|
+
/**
|
|
140
|
+
* 驗證重試策略配置
|
|
141
|
+
*
|
|
142
|
+
* @param policy - 重試策略配置
|
|
143
|
+
* @returns 是否有效
|
|
144
|
+
*
|
|
145
|
+
* @description
|
|
146
|
+
* 檢查配置是否合理:
|
|
147
|
+
* - maxRetries >= 0
|
|
148
|
+
* - initialDelayMs > 0
|
|
149
|
+
* - maxDelayMs >= initialDelayMs
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const validPolicy = { maxRetries: 3, initialDelayMs: 1000, maxDelayMs: 30000, backoff: 'exponential' }
|
|
154
|
+
* engine.isValidPolicy(validPolicy) // true
|
|
155
|
+
*
|
|
156
|
+
* const invalidPolicy = { maxRetries: 3, initialDelayMs: 5000, maxDelayMs: 1000, backoff: 'exponential' }
|
|
157
|
+
* engine.isValidPolicy(invalidPolicy) // false (maxDelayMs < initialDelayMs)
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
isValidPolicy(policy: RetryPolicy): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* 獲取人類可讀的重試信息
|
|
163
|
+
*
|
|
164
|
+
* @param attemptCount - 當前嘗試次數(從 1 開始)
|
|
165
|
+
* @param policy - 重試策略配置
|
|
166
|
+
* @returns 描述性文本
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const policy = { maxRetries: 3, initialDelayMs: 1000, maxDelayMs: 30000, backoff: 'exponential' }
|
|
171
|
+
*
|
|
172
|
+
* engine.getRetryInfo(1, policy) // "Retry 1 of 3, will retry in ~1100ms"
|
|
173
|
+
* engine.getRetryInfo(3, policy) // "Retry 3 of 3 (final attempt), will retry in ~4400ms"
|
|
174
|
+
* engine.getRetryInfo(4, policy) // "Max retries exceeded (4 >= 3)"
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
getRetryInfo(attemptCount: number, policy: RetryPolicy): string;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 創建默認的重試策略
|
|
181
|
+
*
|
|
182
|
+
* @returns 默認重試策略
|
|
183
|
+
*
|
|
184
|
+
* @description
|
|
185
|
+
* 默認配置:
|
|
186
|
+
* - 最多重試 3 次
|
|
187
|
+
* - 指數退避
|
|
188
|
+
* - 初始延遲 1 秒
|
|
189
|
+
* - 最大延遲 30 秒
|
|
190
|
+
* - 超過最大重試後發送 DLQ
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const policy = getDefaultRetryPolicy()
|
|
195
|
+
* // { maxRetries: 3, backoff: 'exponential', initialDelayMs: 1000, maxDelayMs: 30000, dlqAfterMaxRetries: true }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export declare function getDefaultRetryPolicy(): RetryPolicy;
|
|
199
|
+
/**
|
|
200
|
+
* 為不同類型的操作獲取預設的重試策略
|
|
201
|
+
*
|
|
202
|
+
* @param type - 操作類型
|
|
203
|
+
* @returns 推薦的重試策略
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* // 外部 API 調用:更多重試,更長延遲
|
|
208
|
+
* const apiPolicy = getPresetRetryPolicy('external-api')
|
|
209
|
+
*
|
|
210
|
+
* // 數據庫操作:較少重試,短延遲
|
|
211
|
+
* const dbPolicy = getPresetRetryPolicy('database')
|
|
212
|
+
*
|
|
213
|
+
* // 消息隊列:適中重試
|
|
214
|
+
* const mqPolicy = getPresetRetryPolicy('message-queue')
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export declare function getPresetRetryPolicy(type: 'external-api' | 'database' | 'message-queue' | 'default'): RetryPolicy;
|