@naman_deep_singh/communication-core 1.0.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 +345 -0
- package/dist/cjs/abstract/BaseCircuitBreaker.js +253 -0
- package/dist/cjs/abstract/BaseClient.js +298 -0
- package/dist/cjs/abstract/BaseCompressionManager.js +377 -0
- package/dist/cjs/abstract/BaseConnectionPool.js +543 -0
- package/dist/cjs/abstract/BaseInterceptor.js +235 -0
- package/dist/cjs/abstract/BaseLoadBalancer.js +269 -0
- package/dist/cjs/abstract/BaseProtocol.js +269 -0
- package/dist/cjs/abstract/BaseRetryStrategy.js +255 -0
- package/dist/cjs/abstract/BaseSerializer.js +341 -0
- package/dist/cjs/abstract/BaseServiceDiscoverer.js +254 -0
- package/dist/cjs/abstract/BaseTimeoutManager.js +295 -0
- package/dist/cjs/abstract/index.js +25 -0
- package/dist/cjs/errors/CircuitBreakerError.js +16 -0
- package/dist/cjs/errors/CommunicationError.js +15 -0
- package/dist/cjs/errors/ConnectionError.js +15 -0
- package/dist/cjs/errors/DiscoveryError.js +15 -0
- package/dist/cjs/errors/LoadBalancerError.js +15 -0
- package/dist/cjs/errors/ProtocolError.js +15 -0
- package/dist/cjs/errors/RetryError.js +16 -0
- package/dist/cjs/errors/SerializationError.js +15 -0
- package/dist/cjs/errors/ServiceUnavailableError.js +15 -0
- package/dist/cjs/errors/TimeoutError.js +16 -0
- package/dist/cjs/errors/communicationErrorCodes.js +35 -0
- package/dist/cjs/errors/index.js +31 -0
- package/dist/cjs/index.js +38 -0
- package/dist/cjs/interfaces/CircuitBreaker.interface.js +6 -0
- package/dist/cjs/interfaces/Client.interface.js +6 -0
- package/dist/cjs/interfaces/Compression.interface.js +6 -0
- package/dist/cjs/interfaces/ConnectionPool.interface.js +6 -0
- package/dist/cjs/interfaces/Interceptor.interface.js +6 -0
- package/dist/cjs/interfaces/LoadBalancer.interface.js +2 -0
- package/dist/cjs/interfaces/Protocol.interface.js +6 -0
- package/dist/cjs/interfaces/RetryStrategy.interface.js +6 -0
- package/dist/cjs/interfaces/Serializer.interface.js +2 -0
- package/dist/cjs/interfaces/ServiceDiscovery.interface.js +6 -0
- package/dist/cjs/interfaces/Timeout.interface.js +6 -0
- package/dist/cjs/interfaces/index.js +6 -0
- package/dist/cjs/types/config.js +6 -0
- package/dist/cjs/types/events.js +6 -0
- package/dist/cjs/types/index.js +6 -0
- package/dist/cjs/types/request.js +6 -0
- package/dist/cjs/types/response.js +6 -0
- package/dist/cjs/types/service.js +6 -0
- package/dist/cjs/utils.js +200 -0
- package/dist/esm/abstract/BaseCircuitBreaker.js +249 -0
- package/dist/esm/abstract/BaseClient.js +294 -0
- package/dist/esm/abstract/BaseCompressionManager.js +373 -0
- package/dist/esm/abstract/BaseConnectionPool.js +539 -0
- package/dist/esm/abstract/BaseInterceptor.js +231 -0
- package/dist/esm/abstract/BaseLoadBalancer.js +265 -0
- package/dist/esm/abstract/BaseProtocol.js +265 -0
- package/dist/esm/abstract/BaseRetryStrategy.js +251 -0
- package/dist/esm/abstract/BaseSerializer.js +337 -0
- package/dist/esm/abstract/BaseServiceDiscoverer.js +250 -0
- package/dist/esm/abstract/BaseTimeoutManager.js +291 -0
- package/dist/esm/abstract/index.js +11 -0
- package/dist/esm/errors/CircuitBreakerError.js +12 -0
- package/dist/esm/errors/CommunicationError.js +11 -0
- package/dist/esm/errors/ConnectionError.js +11 -0
- package/dist/esm/errors/DiscoveryError.js +11 -0
- package/dist/esm/errors/LoadBalancerError.js +11 -0
- package/dist/esm/errors/ProtocolError.js +11 -0
- package/dist/esm/errors/RetryError.js +12 -0
- package/dist/esm/errors/SerializationError.js +11 -0
- package/dist/esm/errors/ServiceUnavailableError.js +11 -0
- package/dist/esm/errors/TimeoutError.js +12 -0
- package/dist/esm/errors/communicationErrorCodes.js +32 -0
- package/dist/esm/errors/index.js +17 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/interfaces/CircuitBreaker.interface.js +5 -0
- package/dist/esm/interfaces/Client.interface.js +5 -0
- package/dist/esm/interfaces/Compression.interface.js +5 -0
- package/dist/esm/interfaces/ConnectionPool.interface.js +5 -0
- package/dist/esm/interfaces/Interceptor.interface.js +5 -0
- package/dist/esm/interfaces/LoadBalancer.interface.js +1 -0
- package/dist/esm/interfaces/Protocol.interface.js +5 -0
- package/dist/esm/interfaces/RetryStrategy.interface.js +5 -0
- package/dist/esm/interfaces/Serializer.interface.js +1 -0
- package/dist/esm/interfaces/ServiceDiscovery.interface.js +5 -0
- package/dist/esm/interfaces/Timeout.interface.js +5 -0
- package/dist/esm/interfaces/index.js +5 -0
- package/dist/esm/types/config.js +5 -0
- package/dist/esm/types/events.js +5 -0
- package/dist/esm/types/index.js +5 -0
- package/dist/esm/types/request.js +5 -0
- package/dist/esm/types/response.js +5 -0
- package/dist/esm/types/service.js +5 -0
- package/dist/esm/utils.js +193 -0
- package/dist/types/abstract/BaseCircuitBreaker.d.ts +167 -0
- package/dist/types/abstract/BaseClient.d.ts +197 -0
- package/dist/types/abstract/BaseCompressionManager.d.ts +180 -0
- package/dist/types/abstract/BaseConnectionPool.d.ts +210 -0
- package/dist/types/abstract/BaseInterceptor.d.ts +150 -0
- package/dist/types/abstract/BaseLoadBalancer.d.ts +167 -0
- package/dist/types/abstract/BaseProtocol.d.ts +163 -0
- package/dist/types/abstract/BaseRetryStrategy.d.ts +130 -0
- package/dist/types/abstract/BaseSerializer.d.ts +181 -0
- package/dist/types/abstract/BaseServiceDiscoverer.d.ts +161 -0
- package/dist/types/abstract/BaseTimeoutManager.d.ts +145 -0
- package/dist/types/abstract/index.d.ts +11 -0
- package/dist/types/errors/CircuitBreakerError.d.ts +8 -0
- package/dist/types/errors/CommunicationError.d.ts +10 -0
- package/dist/types/errors/ConnectionError.d.ts +9 -0
- package/dist/types/errors/DiscoveryError.d.ts +9 -0
- package/dist/types/errors/LoadBalancerError.d.ts +9 -0
- package/dist/types/errors/ProtocolError.d.ts +9 -0
- package/dist/types/errors/RetryError.d.ts +11 -0
- package/dist/types/errors/SerializationError.d.ts +9 -0
- package/dist/types/errors/ServiceUnavailableError.d.ts +12 -0
- package/dist/types/errors/TimeoutError.d.ts +11 -0
- package/dist/types/errors/communicationErrorCodes.d.ts +27 -0
- package/dist/types/errors/index.d.ts +11 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/interfaces/CircuitBreaker.interface.d.ts +150 -0
- package/dist/types/interfaces/Client.interface.d.ts +153 -0
- package/dist/types/interfaces/Compression.interface.d.ts +190 -0
- package/dist/types/interfaces/ConnectionPool.interface.d.ts +191 -0
- package/dist/types/interfaces/Interceptor.interface.d.ts +220 -0
- package/dist/types/interfaces/LoadBalancer.interface.d.ts +153 -0
- package/dist/types/interfaces/Protocol.interface.d.ts +117 -0
- package/dist/types/interfaces/RetryStrategy.interface.d.ts +160 -0
- package/dist/types/interfaces/Serializer.interface.d.ts +176 -0
- package/dist/types/interfaces/ServiceDiscovery.interface.d.ts +189 -0
- package/dist/types/interfaces/Timeout.interface.d.ts +135 -0
- package/dist/types/interfaces/index.d.ts +15 -0
- package/dist/types/types/config.d.ts +540 -0
- package/dist/types/types/events.d.ts +204 -0
- package/dist/types/types/index.d.ts +9 -0
- package/dist/types/types/request.d.ts +143 -0
- package/dist/types/types/response.d.ts +155 -0
- package/dist/types/types/service.d.ts +279 -0
- package/dist/types/utils.d.ts +179 -0
- package/package.json +88 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interceptor interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { CommunicationError } from '../errors/CommunicationError.js';
|
|
6
|
+
import type { InterceptorEvent } from '../types/events.js';
|
|
7
|
+
import type { Request, RequestContext } from '../types/request.js';
|
|
8
|
+
import type { Response } from '../types/response.js';
|
|
9
|
+
/**
|
|
10
|
+
* Interceptor execution order
|
|
11
|
+
*/
|
|
12
|
+
export interface InterceptorOrder {
|
|
13
|
+
/** Phase order within request phase */
|
|
14
|
+
request?: number;
|
|
15
|
+
/** Phase order within response phase */
|
|
16
|
+
response?: number;
|
|
17
|
+
/** Phase order within error phase */
|
|
18
|
+
error?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Interceptor interface for intercepting and modifying requests/responses
|
|
22
|
+
*/
|
|
23
|
+
export interface IInterceptor<TRequest = Request, TResponse = Response> {
|
|
24
|
+
/** Interceptor name */
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/** Interceptor version */
|
|
27
|
+
readonly version?: string;
|
|
28
|
+
/** Interceptor description */
|
|
29
|
+
readonly description?: string;
|
|
30
|
+
/** Whether interceptor is enabled */
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
/** Interceptor execution order */
|
|
33
|
+
readonly order: InterceptorOrder;
|
|
34
|
+
/**
|
|
35
|
+
* Intercept request before it's sent
|
|
36
|
+
* @param request Original request
|
|
37
|
+
* @param context Request context
|
|
38
|
+
* @returns Modified request or void
|
|
39
|
+
*/
|
|
40
|
+
onRequest?(request: TRequest, context: RequestContext): TRequest | void | Promise<TRequest | void>;
|
|
41
|
+
/**
|
|
42
|
+
* Intercept response after it's received
|
|
43
|
+
* @param response Original response
|
|
44
|
+
* @param context Request context
|
|
45
|
+
* @returns Modified response or void
|
|
46
|
+
*/
|
|
47
|
+
onResponse?(response: TResponse, context: RequestContext): TResponse | void | Promise<TResponse | void>;
|
|
48
|
+
/**
|
|
49
|
+
* Intercept error when request fails
|
|
50
|
+
* @param error Error that occurred
|
|
51
|
+
* @param context Request context
|
|
52
|
+
* @returns Modified error, new response, or void
|
|
53
|
+
*/
|
|
54
|
+
onError?(error: CommunicationError, context: RequestContext): CommunicationError | TResponse | void | Promise<CommunicationError | TResponse | void>;
|
|
55
|
+
/**
|
|
56
|
+
* Initialize interceptor (called when added to client/protocol)
|
|
57
|
+
* @param options Initialization options
|
|
58
|
+
*/
|
|
59
|
+
initialize?(options?: Record<string, unknown>): void | Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Cleanup interceptor (called when removed from client/protocol)
|
|
62
|
+
*/
|
|
63
|
+
cleanup?(): void | Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Get interceptor configuration
|
|
66
|
+
*/
|
|
67
|
+
getConfig(): Record<string, unknown>;
|
|
68
|
+
/**
|
|
69
|
+
* Update interceptor configuration
|
|
70
|
+
* @param config New configuration
|
|
71
|
+
*/
|
|
72
|
+
updateConfig(config: Record<string, unknown>): void;
|
|
73
|
+
/**
|
|
74
|
+
* Health check for the interceptor
|
|
75
|
+
*/
|
|
76
|
+
healthCheck?(): {
|
|
77
|
+
healthy: boolean;
|
|
78
|
+
message?: string;
|
|
79
|
+
details?: Record<string, unknown>;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Get interceptor statistics
|
|
83
|
+
*/
|
|
84
|
+
getStats?(): {
|
|
85
|
+
totalRequests: number;
|
|
86
|
+
totalResponses: number;
|
|
87
|
+
totalErrors: number;
|
|
88
|
+
modifications: number;
|
|
89
|
+
averageProcessingTime: number;
|
|
90
|
+
lastError?: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Reset interceptor statistics
|
|
94
|
+
*/
|
|
95
|
+
resetStats?(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Event emitter for interceptor events
|
|
98
|
+
*/
|
|
99
|
+
readonly events?: {
|
|
100
|
+
/**
|
|
101
|
+
* Subscribe to interceptor events
|
|
102
|
+
* @param event Event name or array of events
|
|
103
|
+
* @param listener Event listener
|
|
104
|
+
*/
|
|
105
|
+
on(event: string | string[], listener: (event: InterceptorEvent) => void): void;
|
|
106
|
+
/**
|
|
107
|
+
* Unsubscribe from interceptor events
|
|
108
|
+
* @param event Event name or array of events
|
|
109
|
+
* @param listener Event listener
|
|
110
|
+
*/
|
|
111
|
+
off(event: string | string[], listener: (event: InterceptorEvent) => void): void;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Interceptor chain interface for managing multiple interceptors
|
|
116
|
+
*/
|
|
117
|
+
export interface IInterceptorChain<TRequest = Request, TResponse = Response> {
|
|
118
|
+
/** Chain name */
|
|
119
|
+
readonly name: string;
|
|
120
|
+
/** All interceptors in the chain */
|
|
121
|
+
readonly interceptors: IInterceptor<TRequest, TResponse>[];
|
|
122
|
+
/**
|
|
123
|
+
* Add an interceptor to the chain
|
|
124
|
+
* @param interceptor Interceptor to add
|
|
125
|
+
*/
|
|
126
|
+
add(interceptor: IInterceptor<TRequest, TResponse>): void;
|
|
127
|
+
/**
|
|
128
|
+
* Remove an interceptor from the chain
|
|
129
|
+
* @param interceptorName Interceptor name to remove
|
|
130
|
+
*/
|
|
131
|
+
remove(interceptorName: string): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Get an interceptor by name
|
|
134
|
+
* @param interceptorName Interceptor name
|
|
135
|
+
*/
|
|
136
|
+
get(interceptorName: string): IInterceptor<TRequest, TResponse> | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* Clear all interceptors from the chain
|
|
139
|
+
*/
|
|
140
|
+
clear(): void;
|
|
141
|
+
/**
|
|
142
|
+
* Execute request interceptors
|
|
143
|
+
* @param request Original request
|
|
144
|
+
* @param context Request context
|
|
145
|
+
* @returns Modified request
|
|
146
|
+
*/
|
|
147
|
+
executeRequest(request: TRequest, context: RequestContext): Promise<TRequest>;
|
|
148
|
+
/**
|
|
149
|
+
* Execute response interceptors
|
|
150
|
+
* @param response Original response
|
|
151
|
+
* @param context Request context
|
|
152
|
+
* @returns Modified response
|
|
153
|
+
*/
|
|
154
|
+
executeResponse(response: TResponse, context: RequestContext): Promise<TResponse>;
|
|
155
|
+
/**
|
|
156
|
+
* Execute error interceptors
|
|
157
|
+
* @param error Original error
|
|
158
|
+
* @param context Request context
|
|
159
|
+
* @returns Modified error or response
|
|
160
|
+
*/
|
|
161
|
+
executeError(error: CommunicationError, context: RequestContext): Promise<CommunicationError | TResponse>;
|
|
162
|
+
/**
|
|
163
|
+
* Sort interceptors by execution order
|
|
164
|
+
*/
|
|
165
|
+
sortInterceptors(): void;
|
|
166
|
+
/**
|
|
167
|
+
* Health check for the interceptor chain
|
|
168
|
+
*/
|
|
169
|
+
healthCheck(): {
|
|
170
|
+
healthy: boolean;
|
|
171
|
+
message?: string;
|
|
172
|
+
details?: {
|
|
173
|
+
totalInterceptors: number;
|
|
174
|
+
enabledInterceptors: number;
|
|
175
|
+
interceptorHealth: Array<{
|
|
176
|
+
name: string;
|
|
177
|
+
healthy: boolean;
|
|
178
|
+
message?: string;
|
|
179
|
+
}>;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Interceptor factory interface
|
|
185
|
+
*/
|
|
186
|
+
export interface IInterceptorFactory {
|
|
187
|
+
/**
|
|
188
|
+
* Create a new interceptor instance
|
|
189
|
+
* @param name Interceptor name
|
|
190
|
+
* @param options Interceptor options
|
|
191
|
+
* @returns New interceptor instance
|
|
192
|
+
*/
|
|
193
|
+
create(name: string, options?: Record<string, unknown>): IInterceptor;
|
|
194
|
+
/**
|
|
195
|
+
* Get an existing interceptor instance
|
|
196
|
+
* @param name Interceptor name
|
|
197
|
+
*/
|
|
198
|
+
get(name: string): IInterceptor | undefined;
|
|
199
|
+
/**
|
|
200
|
+
* Get all interceptor instances
|
|
201
|
+
*/
|
|
202
|
+
getAll(): IInterceptor[];
|
|
203
|
+
/**
|
|
204
|
+
* Register a custom interceptor
|
|
205
|
+
* @param name Interceptor name
|
|
206
|
+
* @param interceptor Interceptor instance or constructor
|
|
207
|
+
*/
|
|
208
|
+
register(name: string, interceptor: IInterceptor | (new (options: Record<string, unknown>) => IInterceptor)): void;
|
|
209
|
+
/**
|
|
210
|
+
* Get available interceptor names
|
|
211
|
+
*/
|
|
212
|
+
getAvailableInterceptors(): string[];
|
|
213
|
+
/**
|
|
214
|
+
* Create an interceptor chain
|
|
215
|
+
* @param name Chain name
|
|
216
|
+
* @param interceptors Initial interceptors
|
|
217
|
+
* @returns New interceptor chain
|
|
218
|
+
*/
|
|
219
|
+
createChain(name: string, interceptors?: IInterceptor[]): IInterceptorChain;
|
|
220
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load balancer interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { LoadBalancingConfig } from '../types/config.js';
|
|
6
|
+
import type { LoadBalancingEvent } from '../types/events.js';
|
|
7
|
+
import type { ServiceInstance } from '../types/service.js';
|
|
8
|
+
/**
|
|
9
|
+
* Load balancing strategy interface for selecting service instances
|
|
10
|
+
*/
|
|
11
|
+
export interface ILoadBalanceStrategy {
|
|
12
|
+
/** Strategy name */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Strategy configuration */
|
|
15
|
+
readonly config: LoadBalancingConfig;
|
|
16
|
+
/** Total selections made */
|
|
17
|
+
readonly totalSelections: number;
|
|
18
|
+
/** Last selection timestamp */
|
|
19
|
+
readonly lastSelectionTime?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Select a service instance from available instances
|
|
22
|
+
* @param instances Available service instances
|
|
23
|
+
* @param context Selection context
|
|
24
|
+
* @returns Selected service instance
|
|
25
|
+
* @throws {CommunicationError} If no instances available
|
|
26
|
+
*/
|
|
27
|
+
select(instances: ServiceInstance[], context?: Record<string, unknown>): ServiceInstance;
|
|
28
|
+
/**
|
|
29
|
+
* Select multiple service instances
|
|
30
|
+
* @param instances Available service instances
|
|
31
|
+
* @param count Number of instances to select
|
|
32
|
+
* @param context Selection context
|
|
33
|
+
* @returns Selected service instances
|
|
34
|
+
*/
|
|
35
|
+
selectMultiple(instances: ServiceInstance[], count: number, context?: Record<string, unknown>): ServiceInstance[];
|
|
36
|
+
/**
|
|
37
|
+
* Update instance statistics (for adaptive strategies)
|
|
38
|
+
* @param instance Service instance
|
|
39
|
+
* @param success Whether the request was successful
|
|
40
|
+
* @param responseTime Response time in milliseconds
|
|
41
|
+
*/
|
|
42
|
+
updateStats(instance: ServiceInstance, success: boolean, responseTime?: number): void;
|
|
43
|
+
/**
|
|
44
|
+
* Reset instance statistics
|
|
45
|
+
* @param instanceId Optional instance ID to reset, or reset all
|
|
46
|
+
*/
|
|
47
|
+
resetStats(instanceId?: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get instance statistics
|
|
50
|
+
* @param instanceId Optional instance ID to get stats for
|
|
51
|
+
*/
|
|
52
|
+
getStats(instanceId?: string): Record<string, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* Check if an instance is currently eligible for selection
|
|
55
|
+
* @param instance Service instance to check
|
|
56
|
+
*/
|
|
57
|
+
isEligible(instance: ServiceInstance): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Filter instances based on eligibility
|
|
60
|
+
* @param instances Instances to filter
|
|
61
|
+
*/
|
|
62
|
+
filterEligible(instances: ServiceInstance[]): ServiceInstance[];
|
|
63
|
+
/**
|
|
64
|
+
* Sort instances by selection priority
|
|
65
|
+
* @param instances Instances to sort
|
|
66
|
+
*/
|
|
67
|
+
sortByPriority(instances: ServiceInstance[]): ServiceInstance[];
|
|
68
|
+
/**
|
|
69
|
+
* Update load balancer configuration
|
|
70
|
+
* @param config New configuration
|
|
71
|
+
*/
|
|
72
|
+
updateConfig(config: Partial<LoadBalancingConfig>): void;
|
|
73
|
+
/**
|
|
74
|
+
* Get load balancer statistics
|
|
75
|
+
*/
|
|
76
|
+
getStrategyStats(): {
|
|
77
|
+
totalSelections: number;
|
|
78
|
+
successfulSelections: number;
|
|
79
|
+
failedSelections: number;
|
|
80
|
+
averageSelectionTime: number;
|
|
81
|
+
instanceStats: Record<string, {
|
|
82
|
+
selections: number;
|
|
83
|
+
successes: number;
|
|
84
|
+
failures: number;
|
|
85
|
+
averageResponseTime?: number;
|
|
86
|
+
}>;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Reset load balancer statistics
|
|
90
|
+
*/
|
|
91
|
+
resetStrategyStats(): void;
|
|
92
|
+
/**
|
|
93
|
+
* Health check for the load balancer
|
|
94
|
+
*/
|
|
95
|
+
healthCheck(): {
|
|
96
|
+
healthy: boolean;
|
|
97
|
+
message?: string;
|
|
98
|
+
details?: Record<string, unknown>;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Event emitter for load balancing events
|
|
102
|
+
*/
|
|
103
|
+
readonly events?: {
|
|
104
|
+
/**
|
|
105
|
+
* Subscribe to load balancing events
|
|
106
|
+
* @param event Event name or array of events
|
|
107
|
+
* @param listener Event listener
|
|
108
|
+
*/
|
|
109
|
+
on(event: string | string[], listener: (event: LoadBalancingEvent) => void): void;
|
|
110
|
+
/**
|
|
111
|
+
* Unsubscribe from load balancing events
|
|
112
|
+
* @param event Event name or array of events
|
|
113
|
+
* @param listener Event listener
|
|
114
|
+
*/
|
|
115
|
+
off(event: string | string[], listener: (event: LoadBalancingEvent) => void): void;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Load balancer factory interface
|
|
120
|
+
*/
|
|
121
|
+
export interface ILoadBalancerFactory {
|
|
122
|
+
/**
|
|
123
|
+
* Create a new load balancer instance
|
|
124
|
+
* @param name Load balancer name
|
|
125
|
+
* @param strategy Load balancing strategy
|
|
126
|
+
* @param config Load balancer configuration
|
|
127
|
+
* @returns New load balancer instance
|
|
128
|
+
*/
|
|
129
|
+
create(name: string, strategy: string, config?: Partial<LoadBalancingConfig>): ILoadBalanceStrategy;
|
|
130
|
+
/**
|
|
131
|
+
* Get an existing load balancer instance
|
|
132
|
+
* @param name Load balancer name
|
|
133
|
+
*/
|
|
134
|
+
get(name: string): ILoadBalanceStrategy | undefined;
|
|
135
|
+
/**
|
|
136
|
+
* Get all load balancer instances
|
|
137
|
+
*/
|
|
138
|
+
getAll(): ILoadBalanceStrategy[];
|
|
139
|
+
/**
|
|
140
|
+
* Register a custom load balancing strategy
|
|
141
|
+
* @param name Strategy name
|
|
142
|
+
* @param strategy Strategy instance or constructor
|
|
143
|
+
*/
|
|
144
|
+
register(name: string, strategy: ILoadBalanceStrategy | (new (config: LoadBalancingConfig) => ILoadBalanceStrategy)): void;
|
|
145
|
+
/**
|
|
146
|
+
* Get available load balancing strategies
|
|
147
|
+
*/
|
|
148
|
+
getAvailableStrategies(): string[];
|
|
149
|
+
/**
|
|
150
|
+
* Close all load balancer instances
|
|
151
|
+
*/
|
|
152
|
+
closeAll(): Promise<void>;
|
|
153
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { BaseProtocolConfig } from '../types/config.js';
|
|
6
|
+
import type { Request } from '../types/request.js';
|
|
7
|
+
import type { Response } from '../types/response.js';
|
|
8
|
+
import type { IInterceptor } from './Interceptor.interface.js';
|
|
9
|
+
/**
|
|
10
|
+
* Protocol interface for all communication protocols
|
|
11
|
+
* This is the base contract that all protocols must implement
|
|
12
|
+
*/
|
|
13
|
+
export interface IProtocol<TRequest = Request, TResponse = Response> {
|
|
14
|
+
/** Protocol name identifier */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** Protocol version */
|
|
17
|
+
readonly version?: string;
|
|
18
|
+
/** Protocol configuration */
|
|
19
|
+
readonly config: BaseProtocolConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Send a request using this protocol
|
|
22
|
+
* @param request The request to send
|
|
23
|
+
* @returns Promise resolving to the response
|
|
24
|
+
* @throws {CommunicationError} If the request fails
|
|
25
|
+
*/
|
|
26
|
+
send(request: TRequest): Promise<TResponse>;
|
|
27
|
+
/**
|
|
28
|
+
* Configure the protocol with new settings
|
|
29
|
+
* @param config Configuration object
|
|
30
|
+
*/
|
|
31
|
+
configure(config: Partial<BaseProtocolConfig>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Connect to the service (for stateful protocols)
|
|
34
|
+
* @returns Promise that resolves when connected
|
|
35
|
+
*/
|
|
36
|
+
connect?(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Disconnect from the service (for stateful protocols)
|
|
39
|
+
* @returns Promise that resolves when disconnected
|
|
40
|
+
*/
|
|
41
|
+
disconnect?(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Check if the protocol is currently connected
|
|
44
|
+
* @returns True if connected, false otherwise
|
|
45
|
+
*/
|
|
46
|
+
isConnected?(): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Add an interceptor to the protocol
|
|
49
|
+
* @param interceptor Interceptor to add
|
|
50
|
+
*/
|
|
51
|
+
addInterceptor?(interceptor: IInterceptor<TRequest, TResponse>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Remove an interceptor from the protocol
|
|
54
|
+
* @param interceptorId Interceptor identifier
|
|
55
|
+
*/
|
|
56
|
+
removeInterceptor?(interceptorId: string): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get all interceptors
|
|
59
|
+
*/
|
|
60
|
+
getInterceptors?(): IInterceptor<TRequest, TResponse>[];
|
|
61
|
+
/**
|
|
62
|
+
* Event emitter for protocol events
|
|
63
|
+
*/
|
|
64
|
+
readonly events?: {
|
|
65
|
+
/**
|
|
66
|
+
* Subscribe to protocol events
|
|
67
|
+
* @param event Event name
|
|
68
|
+
* @param listener Event listener
|
|
69
|
+
*/
|
|
70
|
+
on(event: string, listener: (data: any) => void): void;
|
|
71
|
+
/**
|
|
72
|
+
* Unsubscribe from protocol events
|
|
73
|
+
* @param event Event name
|
|
74
|
+
* @param listener Event listener
|
|
75
|
+
*/
|
|
76
|
+
off(event: string, listener: (data: any) => void): void;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Get protocol metrics
|
|
80
|
+
*/
|
|
81
|
+
getMetrics?(): Record<string, unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* Reset protocol metrics
|
|
84
|
+
*/
|
|
85
|
+
resetMetrics?(): void;
|
|
86
|
+
/**
|
|
87
|
+
* Health check for the protocol
|
|
88
|
+
* @returns Promise resolving to health status
|
|
89
|
+
*/
|
|
90
|
+
healthCheck?(): Promise<{
|
|
91
|
+
healthy: boolean;
|
|
92
|
+
message?: string;
|
|
93
|
+
details?: Record<string, unknown>;
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Protocol factory interface for creating protocol instances
|
|
98
|
+
*/
|
|
99
|
+
export interface IProtocolFactory {
|
|
100
|
+
/**
|
|
101
|
+
* Create a new protocol instance
|
|
102
|
+
* @param config Protocol configuration
|
|
103
|
+
* @returns New protocol instance
|
|
104
|
+
*/
|
|
105
|
+
create(config: BaseProtocolConfig): IProtocol;
|
|
106
|
+
/**
|
|
107
|
+
* Register a protocol implementation
|
|
108
|
+
* @param name Protocol name
|
|
109
|
+
* @param constructor Protocol constructor
|
|
110
|
+
*/
|
|
111
|
+
register?(name: string, constructor: new (config: BaseProtocolConfig) => IProtocol): void;
|
|
112
|
+
/**
|
|
113
|
+
* Get available protocol names
|
|
114
|
+
* @returns Array of protocol names
|
|
115
|
+
*/
|
|
116
|
+
getAvailableProtocols?(): string[];
|
|
117
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry strategy interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { CommunicationError } from '../errors/CommunicationError.js';
|
|
6
|
+
import type { RetryPolicyConfig } from '../types/config.js';
|
|
7
|
+
import type { RetryEvent } from '../types/events.js';
|
|
8
|
+
/**
|
|
9
|
+
* Retry decision result
|
|
10
|
+
*/
|
|
11
|
+
export interface RetryDecision {
|
|
12
|
+
/** Whether to retry */
|
|
13
|
+
shouldRetry: boolean;
|
|
14
|
+
/** Delay before next retry in milliseconds */
|
|
15
|
+
delay?: number;
|
|
16
|
+
/** Reason for decision */
|
|
17
|
+
reason?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retry context for each attempt
|
|
21
|
+
*/
|
|
22
|
+
export interface RetryContext {
|
|
23
|
+
/** Current attempt number (starting from 1) */
|
|
24
|
+
attempt: number;
|
|
25
|
+
/** Maximum allowed attempts */
|
|
26
|
+
maxAttempts: number;
|
|
27
|
+
/** Last error that occurred */
|
|
28
|
+
lastError?: CommunicationError;
|
|
29
|
+
/** Start time of first attempt */
|
|
30
|
+
startTime: number;
|
|
31
|
+
/** Total elapsed time in milliseconds */
|
|
32
|
+
elapsedTime: number;
|
|
33
|
+
/** ✅ ADD THIS: Is this a retry attempt? */
|
|
34
|
+
isRetry: boolean;
|
|
35
|
+
/** Custom context data */
|
|
36
|
+
data?: Map<string, unknown>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Retry strategy interface for handling retry logic
|
|
40
|
+
*/
|
|
41
|
+
export interface IRetryStrategy {
|
|
42
|
+
/** Strategy name */
|
|
43
|
+
readonly name: string;
|
|
44
|
+
/** Strategy configuration */
|
|
45
|
+
readonly config: RetryPolicyConfig;
|
|
46
|
+
/** Total retry attempts made */
|
|
47
|
+
readonly totalAttempts: number;
|
|
48
|
+
/** Successful retries */
|
|
49
|
+
readonly successfulRetries: number;
|
|
50
|
+
/** Failed retries */
|
|
51
|
+
readonly failedRetries: number;
|
|
52
|
+
/**
|
|
53
|
+
* Execute a function with retry logic
|
|
54
|
+
* @param fn Function to execute
|
|
55
|
+
* @param context Initial retry context
|
|
56
|
+
* @returns Promise resolving to function result
|
|
57
|
+
* @throws {CommunicationError} If all retry attempts fail
|
|
58
|
+
*/
|
|
59
|
+
execute<T>(fn: () => Promise<T>, context?: Partial<RetryContext>): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute a function with retry logic and custom decision function
|
|
62
|
+
* @param fn Function to execute
|
|
63
|
+
* @param shouldRetry Custom retry decision function
|
|
64
|
+
* @param context Initial retry context
|
|
65
|
+
* @returns Promise resolving to function result
|
|
66
|
+
*/
|
|
67
|
+
executeWithDecision<T>(fn: () => Promise<T>, shouldRetry: (error: CommunicationError, context: RetryContext) => RetryDecision, context?: Partial<RetryContext>): Promise<T>;
|
|
68
|
+
/**
|
|
69
|
+
* Determine if a retry should be attempted
|
|
70
|
+
* @param error Error that occurred
|
|
71
|
+
* @param context Current retry context
|
|
72
|
+
* @returns Retry decision
|
|
73
|
+
*/
|
|
74
|
+
shouldRetry(error: CommunicationError, context: RetryContext): RetryDecision;
|
|
75
|
+
/**
|
|
76
|
+
* Calculate delay for next retry attempt
|
|
77
|
+
* @param attempt Current attempt number
|
|
78
|
+
* @param context Current retry context
|
|
79
|
+
* @returns Delay in milliseconds
|
|
80
|
+
*/
|
|
81
|
+
calculateDelay(attempt: number, context: RetryContext): number;
|
|
82
|
+
/**
|
|
83
|
+
* Update retry strategy configuration
|
|
84
|
+
* @param config New configuration
|
|
85
|
+
*/
|
|
86
|
+
updateConfig(config: Partial<RetryPolicyConfig>): void;
|
|
87
|
+
/**
|
|
88
|
+
* Get retry strategy statistics
|
|
89
|
+
*/
|
|
90
|
+
getStats(): {
|
|
91
|
+
totalExecutions: number;
|
|
92
|
+
totalAttempts: number;
|
|
93
|
+
successfulRetries: number;
|
|
94
|
+
failedRetries: number;
|
|
95
|
+
successRate: number;
|
|
96
|
+
averageAttempts: number;
|
|
97
|
+
averageDelay: number;
|
|
98
|
+
totalTimeSpent: number;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Reset retry strategy statistics
|
|
102
|
+
*/
|
|
103
|
+
resetStats(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Health check for the retry strategy
|
|
106
|
+
*/
|
|
107
|
+
healthCheck(): {
|
|
108
|
+
healthy: boolean;
|
|
109
|
+
message?: string;
|
|
110
|
+
details?: Record<string, unknown>;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Event emitter for retry events
|
|
114
|
+
*/
|
|
115
|
+
readonly events?: {
|
|
116
|
+
/**
|
|
117
|
+
* Subscribe to retry events
|
|
118
|
+
* @param event Event name or array of events
|
|
119
|
+
* @param listener Event listener
|
|
120
|
+
*/
|
|
121
|
+
on(event: string | string[], listener: (event: RetryEvent) => void): void;
|
|
122
|
+
/**
|
|
123
|
+
* Unsubscribe from retry events
|
|
124
|
+
* @param event Event name or array of events
|
|
125
|
+
* @param listener Event listener
|
|
126
|
+
*/
|
|
127
|
+
off(event: string | string[], listener: (event: RetryEvent) => void): void;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Retry strategy factory interface
|
|
132
|
+
*/
|
|
133
|
+
export interface IRetryStrategyFactory {
|
|
134
|
+
/**
|
|
135
|
+
* Create a new retry strategy instance
|
|
136
|
+
* @param name Strategy name
|
|
137
|
+
* @param config Strategy configuration
|
|
138
|
+
* @returns New retry strategy instance
|
|
139
|
+
*/
|
|
140
|
+
create(name: string, config?: Partial<RetryPolicyConfig>): IRetryStrategy;
|
|
141
|
+
/**
|
|
142
|
+
* Get an existing retry strategy instance
|
|
143
|
+
* @param name Strategy name
|
|
144
|
+
*/
|
|
145
|
+
get(name: string): IRetryStrategy | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Get all retry strategy instances
|
|
148
|
+
*/
|
|
149
|
+
getAll(): IRetryStrategy[];
|
|
150
|
+
/**
|
|
151
|
+
* Register a custom retry strategy
|
|
152
|
+
* @param name Strategy name
|
|
153
|
+
* @param strategy Strategy instance or constructor
|
|
154
|
+
*/
|
|
155
|
+
register(name: string, strategy: IRetryStrategy | (new (config: RetryPolicyConfig) => IRetryStrategy)): void;
|
|
156
|
+
/**
|
|
157
|
+
* Get available retry strategy names
|
|
158
|
+
*/
|
|
159
|
+
getAvailableStrategies(): string[];
|
|
160
|
+
}
|