@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,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base load balancer implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { ILoadBalanceStrategy } from '../interfaces/LoadBalancer.interface.js';
|
|
6
|
+
import type { LoadBalancerSelection } from '../types/config.js';
|
|
7
|
+
import type { LoadBalancingConfig } from '../types/config.js';
|
|
8
|
+
import type { ServiceInstance } from '../types/service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base load balancer implementation
|
|
11
|
+
* Provides common functionality for all load balancing strategy implementations
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class BaseLoadBalancer implements ILoadBalanceStrategy {
|
|
14
|
+
/** Strategy name */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** Strategy configuration */
|
|
17
|
+
config: LoadBalancingConfig;
|
|
18
|
+
/** Total selections made */
|
|
19
|
+
totalSelections: number;
|
|
20
|
+
/** Last selection timestamp */
|
|
21
|
+
lastSelectionTime?: number;
|
|
22
|
+
/** Instance statistics */
|
|
23
|
+
protected instanceStats: Map<string, {
|
|
24
|
+
selections: number;
|
|
25
|
+
successes: number;
|
|
26
|
+
failures: number;
|
|
27
|
+
totalResponseTime: number;
|
|
28
|
+
averageResponseTime?: number;
|
|
29
|
+
lastSelected?: number;
|
|
30
|
+
}>;
|
|
31
|
+
/** Load balancer statistics */
|
|
32
|
+
protected stats: {
|
|
33
|
+
totalSelections: number;
|
|
34
|
+
successfulSelections: number;
|
|
35
|
+
failedSelections: number;
|
|
36
|
+
averageSelectionTime: number;
|
|
37
|
+
instanceStats: Record<string, {
|
|
38
|
+
selections: number;
|
|
39
|
+
successes: number;
|
|
40
|
+
failures: number;
|
|
41
|
+
averageResponseTime?: number;
|
|
42
|
+
}>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Create a new base load balancer instance
|
|
46
|
+
* @param name Load balancer name
|
|
47
|
+
* @param config Load balancer configuration
|
|
48
|
+
*/
|
|
49
|
+
constructor(name: string, config: LoadBalancingConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Initialize load balancer
|
|
52
|
+
*/
|
|
53
|
+
protected initialize(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Select a service instance from available instances
|
|
56
|
+
* @param instances Available service instances
|
|
57
|
+
* @param context Selection context
|
|
58
|
+
* @returns Selected service instance
|
|
59
|
+
* @throws {CommunicationError} If no instances available
|
|
60
|
+
*/
|
|
61
|
+
abstract select(instances: ServiceInstance[], context?: Record<string, unknown>): ServiceInstance;
|
|
62
|
+
/**
|
|
63
|
+
* Select multiple service instances
|
|
64
|
+
* @param instances Available service instances
|
|
65
|
+
* @param count Number of instances to select
|
|
66
|
+
* @param context Selection context
|
|
67
|
+
* @returns Selected service instances
|
|
68
|
+
*/
|
|
69
|
+
selectMultiple(instances: ServiceInstance[], count: number, context?: Record<string, unknown>): ServiceInstance[];
|
|
70
|
+
/**
|
|
71
|
+
* Update instance statistics (for adaptive strategies)
|
|
72
|
+
* @param instance Service instance
|
|
73
|
+
* @param success Whether the request was successful
|
|
74
|
+
* @param responseTime Response time in milliseconds
|
|
75
|
+
*/
|
|
76
|
+
updateStats(instance: ServiceInstance, success: boolean, responseTime?: number): void;
|
|
77
|
+
/**
|
|
78
|
+
* Update aggregate statistics
|
|
79
|
+
* @param instanceId Instance ID
|
|
80
|
+
* @param instanceStats Instance statistics
|
|
81
|
+
*/
|
|
82
|
+
protected updateAggregateStats(instanceId: string, instanceStats: {
|
|
83
|
+
selections: number;
|
|
84
|
+
successes: number;
|
|
85
|
+
failures: number;
|
|
86
|
+
totalResponseTime: number;
|
|
87
|
+
averageResponseTime?: number;
|
|
88
|
+
lastSelected?: number;
|
|
89
|
+
}): void;
|
|
90
|
+
/**
|
|
91
|
+
* Reset instance statistics
|
|
92
|
+
* @param instanceId Optional instance ID to reset, or reset all
|
|
93
|
+
*/
|
|
94
|
+
resetStats(instanceId?: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Get instance statistics
|
|
97
|
+
* @param instanceId Optional instance ID to get stats for
|
|
98
|
+
*/
|
|
99
|
+
getStats(instanceId?: string): Record<string, unknown>;
|
|
100
|
+
/**
|
|
101
|
+
* Check if an instance is currently eligible for selection
|
|
102
|
+
* @param instance Service instance to check
|
|
103
|
+
*/
|
|
104
|
+
isEligible(instance: ServiceInstance): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Filter instances based on eligibility
|
|
107
|
+
* @param instances Instances to filter
|
|
108
|
+
*/
|
|
109
|
+
filterEligible(instances: ServiceInstance[]): ServiceInstance[];
|
|
110
|
+
/**
|
|
111
|
+
* Sort instances by selection priority
|
|
112
|
+
* @param instances Instances to sort
|
|
113
|
+
*/
|
|
114
|
+
sortByPriority(instances: ServiceInstance[]): ServiceInstance[];
|
|
115
|
+
/**
|
|
116
|
+
* Get instance weight
|
|
117
|
+
* @param instance Service instance
|
|
118
|
+
* @returns Instance weight
|
|
119
|
+
*/
|
|
120
|
+
protected getInstanceWeight(instance: ServiceInstance): number;
|
|
121
|
+
/**
|
|
122
|
+
* Record selection
|
|
123
|
+
* @param instance Selected instance
|
|
124
|
+
* @param selectionTime Selection time in milliseconds
|
|
125
|
+
*/
|
|
126
|
+
protected recordSelection(instance: ServiceInstance, selectionTime: number): void;
|
|
127
|
+
/**
|
|
128
|
+
* Create selection result
|
|
129
|
+
* @param instance Selected instance
|
|
130
|
+
* @param instances Available instances
|
|
131
|
+
* @param selectionTime Selection time in milliseconds
|
|
132
|
+
* @returns Selection result
|
|
133
|
+
*/
|
|
134
|
+
protected createSelectionResult(instance: ServiceInstance, instances: ServiceInstance[], selectionTime: number): LoadBalancerSelection;
|
|
135
|
+
/**
|
|
136
|
+
* Update load balancer configuration
|
|
137
|
+
* @param config New configuration
|
|
138
|
+
*/
|
|
139
|
+
updateConfig(config: Partial<LoadBalancingConfig>): void;
|
|
140
|
+
/**
|
|
141
|
+
* Get load balancer statistics
|
|
142
|
+
*/
|
|
143
|
+
getStrategyStats(): {
|
|
144
|
+
totalSelections: number;
|
|
145
|
+
successfulSelections: number;
|
|
146
|
+
failedSelections: number;
|
|
147
|
+
averageSelectionTime: number;
|
|
148
|
+
instanceStats: Record<string, {
|
|
149
|
+
selections: number;
|
|
150
|
+
successes: number;
|
|
151
|
+
failures: number;
|
|
152
|
+
averageResponseTime?: number;
|
|
153
|
+
}>;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Reset load balancer statistics
|
|
157
|
+
*/
|
|
158
|
+
resetStrategyStats(): void;
|
|
159
|
+
/**
|
|
160
|
+
* Health check for the load balancer
|
|
161
|
+
*/
|
|
162
|
+
healthCheck(): {
|
|
163
|
+
healthy: boolean;
|
|
164
|
+
message?: string;
|
|
165
|
+
details?: Record<string, unknown>;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base protocol implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { type CommunicationErrorType } from '../errors/CommunicationError.js';
|
|
6
|
+
import type { Request } from '../types/request.js';
|
|
7
|
+
import type { BaseProtocolConfig } from '../types/config.js';
|
|
8
|
+
import type { IProtocol } from '../interfaces/Protocol.interface.js';
|
|
9
|
+
import type { IInterceptor } from '../interfaces/Interceptor.interface.js';
|
|
10
|
+
import type { Response } from '../types/response.js';
|
|
11
|
+
/**
|
|
12
|
+
* Abstract base protocol implementation
|
|
13
|
+
* Provides common functionality for all protocol implementations
|
|
14
|
+
*/
|
|
15
|
+
export declare abstract class BaseProtocol<TRequest = Request, TResponse = Response> implements IProtocol<TRequest, TResponse> {
|
|
16
|
+
/** Protocol name identifier */
|
|
17
|
+
readonly name: string;
|
|
18
|
+
/** Protocol version */
|
|
19
|
+
readonly version?: string;
|
|
20
|
+
/** Protocol configuration */
|
|
21
|
+
config: BaseProtocolConfig;
|
|
22
|
+
/** Protocol interceptors */
|
|
23
|
+
protected interceptors: IInterceptor<TRequest, TResponse>[];
|
|
24
|
+
/** Protocol metrics */
|
|
25
|
+
protected metrics: Record<string, unknown>;
|
|
26
|
+
/** Whether protocol is connected (for stateful protocols) */
|
|
27
|
+
protected connected: boolean;
|
|
28
|
+
/** Protocol start time */
|
|
29
|
+
protected readonly startTime: number;
|
|
30
|
+
/** Total requests sent */
|
|
31
|
+
protected totalRequests: number;
|
|
32
|
+
/** Total successful requests */
|
|
33
|
+
protected successfulRequests: number;
|
|
34
|
+
/** Total failed requests */
|
|
35
|
+
protected failedRequests: number;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new base protocol instance
|
|
38
|
+
* @param name Protocol name
|
|
39
|
+
* @param config Protocol configuration
|
|
40
|
+
*/
|
|
41
|
+
constructor(name: string, config: BaseProtocolConfig);
|
|
42
|
+
/**
|
|
43
|
+
* Send a request using this protocol
|
|
44
|
+
* @param request The request to send
|
|
45
|
+
* @returns Promise resolving to the response
|
|
46
|
+
* @throws {CommunicationError} If the request fails
|
|
47
|
+
*/
|
|
48
|
+
abstract send(request: TRequest): Promise<TResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* Configure the protocol with new settings
|
|
51
|
+
* @param config Configuration object
|
|
52
|
+
*/
|
|
53
|
+
configure(config: Partial<BaseProtocolConfig>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Hook for protocol configuration changes
|
|
56
|
+
* @param newConfig New configuration
|
|
57
|
+
*/
|
|
58
|
+
protected onConfigure(newConfig: Partial<BaseProtocolConfig>): void;
|
|
59
|
+
/**
|
|
60
|
+
* Connect to the service (for stateful protocols)
|
|
61
|
+
* @returns Promise that resolves when connected
|
|
62
|
+
*/
|
|
63
|
+
connect(): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Hook for connection logic
|
|
66
|
+
*/
|
|
67
|
+
protected onConnect?(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Disconnect from the service (for stateful protocols)
|
|
70
|
+
* @returns Promise that resolves when disconnected
|
|
71
|
+
*/
|
|
72
|
+
disconnect(): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Hook for disconnection logic
|
|
75
|
+
*/
|
|
76
|
+
protected onDisconnect?(): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Check if the protocol is currently connected
|
|
79
|
+
* @returns True if connected, false otherwise
|
|
80
|
+
*/
|
|
81
|
+
isConnected(): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Add an interceptor to the protocol
|
|
84
|
+
* @param interceptor Interceptor to add
|
|
85
|
+
*/
|
|
86
|
+
addInterceptor(interceptor: IInterceptor<TRequest, TResponse>): void;
|
|
87
|
+
/**
|
|
88
|
+
* Hook for interceptor addition
|
|
89
|
+
* @param interceptor Added interceptor
|
|
90
|
+
*/
|
|
91
|
+
protected onInterceptorAdded(interceptor: IInterceptor<TRequest, TResponse>): void;
|
|
92
|
+
/**
|
|
93
|
+
* Remove an interceptor from the protocol
|
|
94
|
+
* @param interceptorId Interceptor identifier
|
|
95
|
+
*/
|
|
96
|
+
removeInterceptor(interceptorId: string): void;
|
|
97
|
+
/**
|
|
98
|
+
* Hook for interceptor removal
|
|
99
|
+
* @param interceptorId Removed interceptor ID
|
|
100
|
+
*/
|
|
101
|
+
protected onInterceptorRemoved(interceptorId: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* Get all interceptors
|
|
104
|
+
*/
|
|
105
|
+
getInterceptors(): IInterceptor<TRequest, TResponse>[];
|
|
106
|
+
/**
|
|
107
|
+
* Execute request interceptors
|
|
108
|
+
* @param request Original request
|
|
109
|
+
* @param context Request context
|
|
110
|
+
* @returns Modified request
|
|
111
|
+
*/
|
|
112
|
+
protected executeRequestInterceptors(request: TRequest, context: any): Promise<TRequest>;
|
|
113
|
+
/**
|
|
114
|
+
* Execute response interceptors
|
|
115
|
+
* @param response Original response
|
|
116
|
+
* @param context Request context
|
|
117
|
+
* @returns Modified response
|
|
118
|
+
*/
|
|
119
|
+
protected executeResponseInterceptors(response: TResponse, context: any): Promise<TResponse>;
|
|
120
|
+
/**
|
|
121
|
+
* Execute error interceptors
|
|
122
|
+
* @param error Original error
|
|
123
|
+
* @param context Request context
|
|
124
|
+
* @returns Modified error or response
|
|
125
|
+
*/
|
|
126
|
+
protected executeErrorInterceptors(error: CommunicationErrorType, context: any): Promise<CommunicationErrorType | TResponse>;
|
|
127
|
+
/**
|
|
128
|
+
* Update request metrics
|
|
129
|
+
* @param success Whether request was successful
|
|
130
|
+
* @param duration Request duration in milliseconds
|
|
131
|
+
*/
|
|
132
|
+
protected updateMetrics(success: boolean, duration: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Calculate average response time
|
|
135
|
+
* @param newDuration New request duration
|
|
136
|
+
* @returns Average response time
|
|
137
|
+
*/
|
|
138
|
+
private calculateAverageResponseTime;
|
|
139
|
+
/**
|
|
140
|
+
* Get protocol metrics
|
|
141
|
+
*/
|
|
142
|
+
getMetrics(): Record<string, unknown>;
|
|
143
|
+
/**
|
|
144
|
+
* Reset protocol metrics
|
|
145
|
+
*/
|
|
146
|
+
resetMetrics(): void;
|
|
147
|
+
/**
|
|
148
|
+
* Health check for the protocol
|
|
149
|
+
* @returns Promise resolving to health status
|
|
150
|
+
*/
|
|
151
|
+
healthCheck(): Promise<{
|
|
152
|
+
healthy: boolean;
|
|
153
|
+
message?: string;
|
|
154
|
+
details?: Record<string, unknown>;
|
|
155
|
+
}>;
|
|
156
|
+
/**
|
|
157
|
+
* Create request context
|
|
158
|
+
* @param request Request object
|
|
159
|
+
* @param attempt Attempt number
|
|
160
|
+
* @returns Request context
|
|
161
|
+
*/
|
|
162
|
+
protected createRequestContext(request: TRequest, attempt?: number): any;
|
|
163
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base retry strategy implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { CommunicationErrorType } from '../errors/CommunicationError.js';
|
|
6
|
+
import type { IRetryStrategy, RetryContext, RetryDecision } from '../interfaces/RetryStrategy.interface.js';
|
|
7
|
+
import type { RetryPolicyConfig } from '../types/config.js';
|
|
8
|
+
/**
|
|
9
|
+
* Abstract base retry strategy implementation
|
|
10
|
+
* Provides common functionality for all retry strategy implementations
|
|
11
|
+
*/
|
|
12
|
+
export declare abstract class BaseRetryStrategy implements IRetryStrategy {
|
|
13
|
+
/** Strategy name */
|
|
14
|
+
readonly name: string;
|
|
15
|
+
/** Strategy configuration */
|
|
16
|
+
config: RetryPolicyConfig;
|
|
17
|
+
/** Total retry attempts made */
|
|
18
|
+
totalAttempts: number;
|
|
19
|
+
/** Successful retries */
|
|
20
|
+
successfulRetries: number;
|
|
21
|
+
/** Failed retries */
|
|
22
|
+
failedRetries: number;
|
|
23
|
+
/** Retry statistics */
|
|
24
|
+
protected stats: {
|
|
25
|
+
totalExecutions: number;
|
|
26
|
+
totalAttempts: number;
|
|
27
|
+
successfulRetries: number;
|
|
28
|
+
failedRetries: number;
|
|
29
|
+
successRate: number;
|
|
30
|
+
averageAttempts: number;
|
|
31
|
+
averageDelay: number;
|
|
32
|
+
totalTimeSpent: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Create a new base retry strategy instance
|
|
36
|
+
* @param name Strategy name
|
|
37
|
+
* @param config Strategy configuration
|
|
38
|
+
*/
|
|
39
|
+
constructor(name: string, config: RetryPolicyConfig);
|
|
40
|
+
/**
|
|
41
|
+
* Initialize retry strategy
|
|
42
|
+
*/
|
|
43
|
+
protected initialize(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Execute a function with retry logic
|
|
46
|
+
* @param fn Function to execute
|
|
47
|
+
* @param context Initial retry context
|
|
48
|
+
* @returns Promise resolving to function result
|
|
49
|
+
* @throws {CommunicationError} If all retry attempts fail
|
|
50
|
+
*/
|
|
51
|
+
abstract execute<T>(fn: () => Promise<T>, context?: Partial<RetryContext>): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Execute a function with retry logic and custom decision function
|
|
54
|
+
* @param fn Function to execute
|
|
55
|
+
* @param shouldRetry Custom retry decision function
|
|
56
|
+
* @param context Initial retry context
|
|
57
|
+
* @returns Promise resolving to function result
|
|
58
|
+
*/
|
|
59
|
+
abstract executeWithDecision<T>(fn: () => Promise<T>, shouldRetry: (error: CommunicationErrorType, context: RetryContext) => RetryDecision, context?: Partial<RetryContext>): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Determine if a retry should be attempted
|
|
62
|
+
* @param error Error that occurred
|
|
63
|
+
* @param context Current retry context
|
|
64
|
+
* @returns Retry decision
|
|
65
|
+
*/
|
|
66
|
+
shouldRetry(error: CommunicationErrorType, context: RetryContext): RetryDecision;
|
|
67
|
+
/**
|
|
68
|
+
* Check if an error is retryable
|
|
69
|
+
* @param error Error to check
|
|
70
|
+
* @returns True if error is retryable
|
|
71
|
+
*/
|
|
72
|
+
protected isRetryableError(error: CommunicationErrorType): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Calculate delay for next retry attempt
|
|
75
|
+
* @param attempt Current attempt number
|
|
76
|
+
* @param context Current retry context
|
|
77
|
+
* @returns Delay in milliseconds
|
|
78
|
+
*/
|
|
79
|
+
calculateDelay(attempt: number, context: RetryContext): number;
|
|
80
|
+
/**
|
|
81
|
+
* Create retry context
|
|
82
|
+
* @param initialContext Initial context values
|
|
83
|
+
* @returns Complete retry context
|
|
84
|
+
*/
|
|
85
|
+
protected createRetryContext(initialContext?: Partial<RetryContext>): RetryContext;
|
|
86
|
+
/**
|
|
87
|
+
* Update retry context after attempt
|
|
88
|
+
* @param context Current context
|
|
89
|
+
* @param error Error from attempt (if any)
|
|
90
|
+
* @returns Updated context
|
|
91
|
+
*/
|
|
92
|
+
protected updateRetryContext(context: RetryContext, error?: CommunicationErrorType): RetryContext;
|
|
93
|
+
/**
|
|
94
|
+
* Update retry statistics
|
|
95
|
+
* @param success Whether retry was successful
|
|
96
|
+
* @param attempts Number of attempts
|
|
97
|
+
* @param totalDelay Total delay time
|
|
98
|
+
*/
|
|
99
|
+
protected updateStats(success: boolean, attempts: number, totalDelay: number): void;
|
|
100
|
+
/**
|
|
101
|
+
* Update retry strategy configuration
|
|
102
|
+
* @param config New configuration
|
|
103
|
+
*/
|
|
104
|
+
updateConfig(config: Partial<RetryPolicyConfig>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Get retry strategy statistics
|
|
107
|
+
*/
|
|
108
|
+
getStats(): {
|
|
109
|
+
totalExecutions: number;
|
|
110
|
+
totalAttempts: number;
|
|
111
|
+
successfulRetries: number;
|
|
112
|
+
failedRetries: number;
|
|
113
|
+
successRate: number;
|
|
114
|
+
averageAttempts: number;
|
|
115
|
+
averageDelay: number;
|
|
116
|
+
totalTimeSpent: number;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Reset retry strategy statistics
|
|
120
|
+
*/
|
|
121
|
+
resetStats(): void;
|
|
122
|
+
/**
|
|
123
|
+
* Health check for the retry strategy
|
|
124
|
+
*/
|
|
125
|
+
healthCheck(): {
|
|
126
|
+
healthy: boolean;
|
|
127
|
+
message?: string;
|
|
128
|
+
details?: Record<string, unknown>;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base serializer implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { DeserializationContext, ISerializer, SerializationContext } from '../interfaces/Serializer.interface.js';
|
|
6
|
+
import type { SerializationConfig, SerializationFormat } from '../types/config.js';
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base serializer implementation
|
|
9
|
+
* Provides common functionality for all serializer implementations
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class BaseSerializer implements ISerializer {
|
|
12
|
+
/** Serializer name */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Supported formats */
|
|
15
|
+
readonly supportedFormats: SerializationFormat[];
|
|
16
|
+
/** Default format */
|
|
17
|
+
readonly defaultFormat: SerializationFormat;
|
|
18
|
+
/** Serializer configuration */
|
|
19
|
+
config: SerializationConfig;
|
|
20
|
+
/** Format handlers registry */
|
|
21
|
+
protected formatHandlers: Map<SerializationFormat, {
|
|
22
|
+
serialize: (data: unknown, context?: SerializationContext) => string | Buffer | Uint8Array;
|
|
23
|
+
deserialize: (data: string | Buffer | Uint8Array, context?: DeserializationContext) => unknown;
|
|
24
|
+
contentType?: string;
|
|
25
|
+
}>;
|
|
26
|
+
/** Serializer statistics */
|
|
27
|
+
protected stats: {
|
|
28
|
+
totalSerializations: number;
|
|
29
|
+
totalDeserializations: number;
|
|
30
|
+
serializationErrors: number;
|
|
31
|
+
deserializationErrors: number;
|
|
32
|
+
averageSerializationTime: number;
|
|
33
|
+
averageDeserializationTime: number;
|
|
34
|
+
formatUsage: Record<string, number>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Create a new base serializer instance
|
|
38
|
+
* @param name Serializer name
|
|
39
|
+
* @param config Serializer configuration
|
|
40
|
+
*/
|
|
41
|
+
constructor(name: string, config: SerializationConfig);
|
|
42
|
+
/**
|
|
43
|
+
* Initialize supported formats
|
|
44
|
+
* @returns Array of supported formats
|
|
45
|
+
*/
|
|
46
|
+
protected abstract initializeSupportedFormats(): SerializationFormat[];
|
|
47
|
+
/**
|
|
48
|
+
* Initialize serializer
|
|
49
|
+
*/
|
|
50
|
+
protected initialize(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Register default format handlers
|
|
53
|
+
*/
|
|
54
|
+
protected abstract registerDefaultHandlers(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Serialize data to a specific format
|
|
57
|
+
* @param data Data to serialize
|
|
58
|
+
* @param context Serialization context
|
|
59
|
+
* @returns Serialized data
|
|
60
|
+
* @throws {CommunicationError} If serialization fails
|
|
61
|
+
*/
|
|
62
|
+
abstract serialize<T = unknown>(data: T, context?: Partial<SerializationContext>): string | Buffer | Uint8Array;
|
|
63
|
+
/**
|
|
64
|
+
* Deserialize data from a specific format
|
|
65
|
+
* @param data Data to deserialize
|
|
66
|
+
* @param context Deserialization context
|
|
67
|
+
* @returns Deserialized data
|
|
68
|
+
* @throws {CommunicationError} If deserialization fails
|
|
69
|
+
*/
|
|
70
|
+
abstract deserialize<T = unknown>(data: string | Buffer | Uint8Array, context?: Partial<DeserializationContext>): T;
|
|
71
|
+
/**
|
|
72
|
+
* Perform serialization with timing and error handling
|
|
73
|
+
* @param data Data to serialize
|
|
74
|
+
* @param context Serialization context
|
|
75
|
+
* @param handler Serialization handler
|
|
76
|
+
* @returns Serialized data
|
|
77
|
+
*/
|
|
78
|
+
protected performSerialization(data: unknown, context: SerializationContext, handler: (data: unknown, context: SerializationContext) => string | Buffer | Uint8Array): string | Buffer | Uint8Array;
|
|
79
|
+
/**
|
|
80
|
+
* Perform deserialization with timing and error handling
|
|
81
|
+
* @param data Data to deserialize
|
|
82
|
+
* @param context Deserialization context
|
|
83
|
+
* @param handler Deserialization handler
|
|
84
|
+
* @returns Deserialized data
|
|
85
|
+
*/
|
|
86
|
+
protected performDeserialization<T>(data: string | Buffer | Uint8Array, context: DeserializationContext, handler: (data: string | Buffer | Uint8Array, context: DeserializationContext) => unknown): T;
|
|
87
|
+
/**
|
|
88
|
+
* Transform data (e.g., date parsing, bigint handling)
|
|
89
|
+
* @param data Data to transform
|
|
90
|
+
* @param direction 'serialize' or 'deserialize'
|
|
91
|
+
*/
|
|
92
|
+
protected transformData(data: unknown, direction: 'serialize' | 'deserialize'): unknown;
|
|
93
|
+
/**
|
|
94
|
+
* Transform dates based on configuration
|
|
95
|
+
* @param data Data containing dates
|
|
96
|
+
* @param direction Transformation direction
|
|
97
|
+
*/
|
|
98
|
+
protected transformDates(data: unknown, direction: 'serialize' | 'deserialize'): unknown;
|
|
99
|
+
/**
|
|
100
|
+
* Transform bigints based on configuration
|
|
101
|
+
* @param data Data containing bigints
|
|
102
|
+
* @param direction Transformation direction
|
|
103
|
+
*/
|
|
104
|
+
protected transformBigInts(data: unknown, direction: 'serialize' | 'deserialize'): unknown;
|
|
105
|
+
/**
|
|
106
|
+
* Apply custom transformations
|
|
107
|
+
* @param data Data to transform
|
|
108
|
+
* @param direction Transformation direction
|
|
109
|
+
*/
|
|
110
|
+
protected applyCustomTransformations(data: unknown, direction: 'serialize' | 'deserialize'): unknown;
|
|
111
|
+
/**
|
|
112
|
+
* Update serialization statistics
|
|
113
|
+
* @param success Whether serialization succeeded
|
|
114
|
+
* @param duration Serialization duration in milliseconds
|
|
115
|
+
* @param format Format used
|
|
116
|
+
*/
|
|
117
|
+
protected updateSerializationStats(success: boolean, duration: number, format: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* Update deserialization statistics
|
|
120
|
+
* @param success Whether deserialization succeeded
|
|
121
|
+
* @param duration Deserialization duration in milliseconds
|
|
122
|
+
* @param format Format used
|
|
123
|
+
*/
|
|
124
|
+
protected updateDeserializationStats(success: boolean, duration: number, format: string): void;
|
|
125
|
+
/**
|
|
126
|
+
* Check if a format is supported
|
|
127
|
+
* @param format Format to check
|
|
128
|
+
*/
|
|
129
|
+
supportsFormat(format: SerializationFormat): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Get content type for a format
|
|
132
|
+
* @param format Format
|
|
133
|
+
* @returns Content type string
|
|
134
|
+
*/
|
|
135
|
+
getContentType(format: SerializationFormat): string;
|
|
136
|
+
/**
|
|
137
|
+
* Get format from content type
|
|
138
|
+
* @param contentType Content type
|
|
139
|
+
* @returns Format or undefined if not recognized
|
|
140
|
+
*/
|
|
141
|
+
getFormatFromContentType(contentType: string): SerializationFormat | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* Register a custom format handler
|
|
144
|
+
* @param format Format name
|
|
145
|
+
* @param handler Serialization/deserialization functions
|
|
146
|
+
*/
|
|
147
|
+
registerFormat(format: SerializationFormat, handler: {
|
|
148
|
+
serialize: (data: unknown) => string | Buffer | Uint8Array;
|
|
149
|
+
deserialize: (data: string | Buffer | Uint8Array) => unknown;
|
|
150
|
+
contentType?: string;
|
|
151
|
+
}): void;
|
|
152
|
+
/**
|
|
153
|
+
* Update serializer configuration
|
|
154
|
+
* @param config New configuration
|
|
155
|
+
*/
|
|
156
|
+
updateConfig(config: Partial<SerializationConfig>): void;
|
|
157
|
+
/**
|
|
158
|
+
* Get serializer statistics
|
|
159
|
+
*/
|
|
160
|
+
getStats(): {
|
|
161
|
+
totalSerializations: number;
|
|
162
|
+
totalDeserializations: number;
|
|
163
|
+
serializationErrors: number;
|
|
164
|
+
deserializationErrors: number;
|
|
165
|
+
averageSerializationTime: number;
|
|
166
|
+
averageDeserializationTime: number;
|
|
167
|
+
formatUsage: Record<string, number>;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Reset serializer statistics
|
|
171
|
+
*/
|
|
172
|
+
resetStats(): void;
|
|
173
|
+
/**
|
|
174
|
+
* Health check for the serializer
|
|
175
|
+
*/
|
|
176
|
+
healthCheck(): {
|
|
177
|
+
healthy: boolean;
|
|
178
|
+
message?: string;
|
|
179
|
+
details?: Record<string, unknown>;
|
|
180
|
+
};
|
|
181
|
+
}
|