@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.
Files changed (134) hide show
  1. package/README.md +345 -0
  2. package/dist/cjs/abstract/BaseCircuitBreaker.js +253 -0
  3. package/dist/cjs/abstract/BaseClient.js +298 -0
  4. package/dist/cjs/abstract/BaseCompressionManager.js +377 -0
  5. package/dist/cjs/abstract/BaseConnectionPool.js +543 -0
  6. package/dist/cjs/abstract/BaseInterceptor.js +235 -0
  7. package/dist/cjs/abstract/BaseLoadBalancer.js +269 -0
  8. package/dist/cjs/abstract/BaseProtocol.js +269 -0
  9. package/dist/cjs/abstract/BaseRetryStrategy.js +255 -0
  10. package/dist/cjs/abstract/BaseSerializer.js +341 -0
  11. package/dist/cjs/abstract/BaseServiceDiscoverer.js +254 -0
  12. package/dist/cjs/abstract/BaseTimeoutManager.js +295 -0
  13. package/dist/cjs/abstract/index.js +25 -0
  14. package/dist/cjs/errors/CircuitBreakerError.js +16 -0
  15. package/dist/cjs/errors/CommunicationError.js +15 -0
  16. package/dist/cjs/errors/ConnectionError.js +15 -0
  17. package/dist/cjs/errors/DiscoveryError.js +15 -0
  18. package/dist/cjs/errors/LoadBalancerError.js +15 -0
  19. package/dist/cjs/errors/ProtocolError.js +15 -0
  20. package/dist/cjs/errors/RetryError.js +16 -0
  21. package/dist/cjs/errors/SerializationError.js +15 -0
  22. package/dist/cjs/errors/ServiceUnavailableError.js +15 -0
  23. package/dist/cjs/errors/TimeoutError.js +16 -0
  24. package/dist/cjs/errors/communicationErrorCodes.js +35 -0
  25. package/dist/cjs/errors/index.js +31 -0
  26. package/dist/cjs/index.js +38 -0
  27. package/dist/cjs/interfaces/CircuitBreaker.interface.js +6 -0
  28. package/dist/cjs/interfaces/Client.interface.js +6 -0
  29. package/dist/cjs/interfaces/Compression.interface.js +6 -0
  30. package/dist/cjs/interfaces/ConnectionPool.interface.js +6 -0
  31. package/dist/cjs/interfaces/Interceptor.interface.js +6 -0
  32. package/dist/cjs/interfaces/LoadBalancer.interface.js +2 -0
  33. package/dist/cjs/interfaces/Protocol.interface.js +6 -0
  34. package/dist/cjs/interfaces/RetryStrategy.interface.js +6 -0
  35. package/dist/cjs/interfaces/Serializer.interface.js +2 -0
  36. package/dist/cjs/interfaces/ServiceDiscovery.interface.js +6 -0
  37. package/dist/cjs/interfaces/Timeout.interface.js +6 -0
  38. package/dist/cjs/interfaces/index.js +6 -0
  39. package/dist/cjs/types/config.js +6 -0
  40. package/dist/cjs/types/events.js +6 -0
  41. package/dist/cjs/types/index.js +6 -0
  42. package/dist/cjs/types/request.js +6 -0
  43. package/dist/cjs/types/response.js +6 -0
  44. package/dist/cjs/types/service.js +6 -0
  45. package/dist/cjs/utils.js +200 -0
  46. package/dist/esm/abstract/BaseCircuitBreaker.js +249 -0
  47. package/dist/esm/abstract/BaseClient.js +294 -0
  48. package/dist/esm/abstract/BaseCompressionManager.js +373 -0
  49. package/dist/esm/abstract/BaseConnectionPool.js +539 -0
  50. package/dist/esm/abstract/BaseInterceptor.js +231 -0
  51. package/dist/esm/abstract/BaseLoadBalancer.js +265 -0
  52. package/dist/esm/abstract/BaseProtocol.js +265 -0
  53. package/dist/esm/abstract/BaseRetryStrategy.js +251 -0
  54. package/dist/esm/abstract/BaseSerializer.js +337 -0
  55. package/dist/esm/abstract/BaseServiceDiscoverer.js +250 -0
  56. package/dist/esm/abstract/BaseTimeoutManager.js +291 -0
  57. package/dist/esm/abstract/index.js +11 -0
  58. package/dist/esm/errors/CircuitBreakerError.js +12 -0
  59. package/dist/esm/errors/CommunicationError.js +11 -0
  60. package/dist/esm/errors/ConnectionError.js +11 -0
  61. package/dist/esm/errors/DiscoveryError.js +11 -0
  62. package/dist/esm/errors/LoadBalancerError.js +11 -0
  63. package/dist/esm/errors/ProtocolError.js +11 -0
  64. package/dist/esm/errors/RetryError.js +12 -0
  65. package/dist/esm/errors/SerializationError.js +11 -0
  66. package/dist/esm/errors/ServiceUnavailableError.js +11 -0
  67. package/dist/esm/errors/TimeoutError.js +12 -0
  68. package/dist/esm/errors/communicationErrorCodes.js +32 -0
  69. package/dist/esm/errors/index.js +17 -0
  70. package/dist/esm/index.js +18 -0
  71. package/dist/esm/interfaces/CircuitBreaker.interface.js +5 -0
  72. package/dist/esm/interfaces/Client.interface.js +5 -0
  73. package/dist/esm/interfaces/Compression.interface.js +5 -0
  74. package/dist/esm/interfaces/ConnectionPool.interface.js +5 -0
  75. package/dist/esm/interfaces/Interceptor.interface.js +5 -0
  76. package/dist/esm/interfaces/LoadBalancer.interface.js +1 -0
  77. package/dist/esm/interfaces/Protocol.interface.js +5 -0
  78. package/dist/esm/interfaces/RetryStrategy.interface.js +5 -0
  79. package/dist/esm/interfaces/Serializer.interface.js +1 -0
  80. package/dist/esm/interfaces/ServiceDiscovery.interface.js +5 -0
  81. package/dist/esm/interfaces/Timeout.interface.js +5 -0
  82. package/dist/esm/interfaces/index.js +5 -0
  83. package/dist/esm/types/config.js +5 -0
  84. package/dist/esm/types/events.js +5 -0
  85. package/dist/esm/types/index.js +5 -0
  86. package/dist/esm/types/request.js +5 -0
  87. package/dist/esm/types/response.js +5 -0
  88. package/dist/esm/types/service.js +5 -0
  89. package/dist/esm/utils.js +193 -0
  90. package/dist/types/abstract/BaseCircuitBreaker.d.ts +167 -0
  91. package/dist/types/abstract/BaseClient.d.ts +197 -0
  92. package/dist/types/abstract/BaseCompressionManager.d.ts +180 -0
  93. package/dist/types/abstract/BaseConnectionPool.d.ts +210 -0
  94. package/dist/types/abstract/BaseInterceptor.d.ts +150 -0
  95. package/dist/types/abstract/BaseLoadBalancer.d.ts +167 -0
  96. package/dist/types/abstract/BaseProtocol.d.ts +163 -0
  97. package/dist/types/abstract/BaseRetryStrategy.d.ts +130 -0
  98. package/dist/types/abstract/BaseSerializer.d.ts +181 -0
  99. package/dist/types/abstract/BaseServiceDiscoverer.d.ts +161 -0
  100. package/dist/types/abstract/BaseTimeoutManager.d.ts +145 -0
  101. package/dist/types/abstract/index.d.ts +11 -0
  102. package/dist/types/errors/CircuitBreakerError.d.ts +8 -0
  103. package/dist/types/errors/CommunicationError.d.ts +10 -0
  104. package/dist/types/errors/ConnectionError.d.ts +9 -0
  105. package/dist/types/errors/DiscoveryError.d.ts +9 -0
  106. package/dist/types/errors/LoadBalancerError.d.ts +9 -0
  107. package/dist/types/errors/ProtocolError.d.ts +9 -0
  108. package/dist/types/errors/RetryError.d.ts +11 -0
  109. package/dist/types/errors/SerializationError.d.ts +9 -0
  110. package/dist/types/errors/ServiceUnavailableError.d.ts +12 -0
  111. package/dist/types/errors/TimeoutError.d.ts +11 -0
  112. package/dist/types/errors/communicationErrorCodes.d.ts +27 -0
  113. package/dist/types/errors/index.d.ts +11 -0
  114. package/dist/types/index.d.ts +13 -0
  115. package/dist/types/interfaces/CircuitBreaker.interface.d.ts +150 -0
  116. package/dist/types/interfaces/Client.interface.d.ts +153 -0
  117. package/dist/types/interfaces/Compression.interface.d.ts +190 -0
  118. package/dist/types/interfaces/ConnectionPool.interface.d.ts +191 -0
  119. package/dist/types/interfaces/Interceptor.interface.d.ts +220 -0
  120. package/dist/types/interfaces/LoadBalancer.interface.d.ts +153 -0
  121. package/dist/types/interfaces/Protocol.interface.d.ts +117 -0
  122. package/dist/types/interfaces/RetryStrategy.interface.d.ts +160 -0
  123. package/dist/types/interfaces/Serializer.interface.d.ts +176 -0
  124. package/dist/types/interfaces/ServiceDiscovery.interface.d.ts +189 -0
  125. package/dist/types/interfaces/Timeout.interface.d.ts +135 -0
  126. package/dist/types/interfaces/index.d.ts +15 -0
  127. package/dist/types/types/config.d.ts +540 -0
  128. package/dist/types/types/events.d.ts +204 -0
  129. package/dist/types/types/index.d.ts +9 -0
  130. package/dist/types/types/request.d.ts +143 -0
  131. package/dist/types/types/response.d.ts +155 -0
  132. package/dist/types/types/service.d.ts +279 -0
  133. package/dist/types/utils.d.ts +179 -0
  134. package/package.json +88 -0
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Abstract base interceptor implementation
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * Abstract base interceptor implementation
7
+ * Provides common functionality for all interceptor implementations
8
+ */
9
+ export class BaseInterceptor {
10
+ /**
11
+ * Create a new base interceptor instance
12
+ * @param name Interceptor name
13
+ * @param options Interceptor options
14
+ */
15
+ constructor(name, options) {
16
+ /** Whether interceptor is enabled */
17
+ this.enabled = true;
18
+ /** Interceptor statistics */
19
+ this.stats = {
20
+ totalRequests: 0,
21
+ totalResponses: 0,
22
+ totalErrors: 0,
23
+ modifications: 0,
24
+ totalProcessingTime: 0,
25
+ averageProcessingTime: 0,
26
+ };
27
+ /** Configuration */
28
+ this.config = {};
29
+ this.name = name;
30
+ this.version = options?.version || '1.0.0';
31
+ this.description = options?.description;
32
+ this.enabled = options?.enabled ?? true;
33
+ // Set default order if not provided
34
+ this.order = options?.order || {
35
+ request: 0,
36
+ response: 0,
37
+ error: 0
38
+ };
39
+ this.config = { ...options };
40
+ // Call setup instead of initialize to avoid confusion with interface method
41
+ this.setup(options);
42
+ }
43
+ /**
44
+ * Setup interceptor (internal initialization)
45
+ * @param options Initialization options
46
+ */
47
+ setup(options) {
48
+ // Can be overridden by subclasses
49
+ }
50
+ /**
51
+ * Initialize interceptor (called when added to client/protocol)
52
+ * @param options Initialization options
53
+ */
54
+ initialize(options) {
55
+ // Can be overridden by subclasses
56
+ this.config = { ...this.config, ...options };
57
+ }
58
+ /**
59
+ * Cleanup interceptor (called when removed from client/protocol)
60
+ */
61
+ cleanup() {
62
+ // Can be overridden by subclasses
63
+ }
64
+ /**
65
+ * Get interceptor configuration
66
+ */
67
+ getConfig() {
68
+ return { ...this.config };
69
+ }
70
+ /**
71
+ * Update interceptor configuration
72
+ * @param config New configuration
73
+ */
74
+ updateConfig(config) {
75
+ this.config = { ...this.config, ...config };
76
+ }
77
+ /**
78
+ * Health check for the interceptor
79
+ */
80
+ healthCheck() {
81
+ const healthy = this.enabled && !this.stats.lastError;
82
+ return {
83
+ healthy,
84
+ message: healthy ? 'Interceptor is operational' : 'Interceptor has errors',
85
+ details: {
86
+ name: this.name,
87
+ enabled: this.enabled,
88
+ lastError: this.stats.lastError,
89
+ statistics: this.getStats(),
90
+ },
91
+ };
92
+ }
93
+ /**
94
+ * Get interceptor statistics
95
+ */
96
+ getStats() {
97
+ return { ...this.stats };
98
+ }
99
+ /**
100
+ * Reset interceptor statistics
101
+ */
102
+ resetStats() {
103
+ this.stats = {
104
+ totalRequests: 0,
105
+ totalResponses: 0,
106
+ totalErrors: 0,
107
+ modifications: 0,
108
+ totalProcessingTime: 0,
109
+ averageProcessingTime: 0,
110
+ };
111
+ this.lastProcessingTime = undefined;
112
+ }
113
+ /**
114
+ * Execute request interception with timing and error handling
115
+ * @param request Original request
116
+ * @param context Request context
117
+ * @returns Modified request or original if not modified
118
+ */
119
+ async executeOnRequest(request, context) {
120
+ if (!this.enabled || !this.onRequest) {
121
+ return request;
122
+ }
123
+ const startTime = Date.now();
124
+ try {
125
+ const result = await this.onRequest(request, context);
126
+ const processingTime = Date.now() - startTime;
127
+ this.updateRequestStats(processingTime, result !== undefined);
128
+ this.lastProcessingTime = processingTime;
129
+ return result !== undefined ? result : request;
130
+ }
131
+ catch (error) {
132
+ this.stats.lastError = error instanceof Error ? error.message : String(error);
133
+ this.stats.totalErrors++;
134
+ throw error;
135
+ }
136
+ }
137
+ /**
138
+ * Execute response interception with timing and error handling
139
+ * @param response Original response
140
+ * @param context Request context
141
+ * @returns Modified response or original if not modified
142
+ */
143
+ async executeOnResponse(response, context) {
144
+ if (!this.enabled || !this.onResponse) {
145
+ return response;
146
+ }
147
+ const startTime = Date.now();
148
+ try {
149
+ const result = await this.onResponse(response, context);
150
+ const processingTime = Date.now() - startTime;
151
+ this.updateResponseStats(processingTime, result !== undefined);
152
+ this.lastProcessingTime = processingTime;
153
+ return result !== undefined ? result : response;
154
+ }
155
+ catch (error) {
156
+ this.stats.lastError = error instanceof Error ? error.message : String(error);
157
+ this.stats.totalErrors++;
158
+ throw error;
159
+ }
160
+ }
161
+ /**
162
+ * Execute error interception with timing and error handling
163
+ * @param error Original error
164
+ * @param context Request context
165
+ * @returns Modified error or response, or original error if not modified
166
+ */
167
+ async executeOnError(error, context) {
168
+ if (!this.enabled || !this.onError) {
169
+ return error;
170
+ }
171
+ const startTime = Date.now();
172
+ try {
173
+ const result = await this.onError(error, context);
174
+ const processingTime = Date.now() - startTime;
175
+ this.updateErrorStats(processingTime, result !== undefined);
176
+ this.lastProcessingTime = processingTime;
177
+ if (result === undefined) {
178
+ return error;
179
+ }
180
+ return result;
181
+ }
182
+ catch (interceptorError) {
183
+ this.stats.lastError = interceptorError instanceof Error ? interceptorError.message : String(interceptorError);
184
+ this.stats.totalErrors++;
185
+ // If interceptor itself fails, return the original error
186
+ return error;
187
+ }
188
+ }
189
+ /**
190
+ * Update request statistics
191
+ * @param processingTime Processing time in milliseconds
192
+ * @param modified Whether request was modified
193
+ */
194
+ updateRequestStats(processingTime, modified) {
195
+ this.stats.totalRequests++;
196
+ this.stats.totalProcessingTime += processingTime;
197
+ this.stats.averageProcessingTime = this.stats.totalProcessingTime /
198
+ (this.stats.totalRequests + this.stats.totalResponses + this.stats.totalErrors);
199
+ if (modified) {
200
+ this.stats.modifications++;
201
+ }
202
+ }
203
+ /**
204
+ * Update response statistics
205
+ * @param processingTime Processing time in milliseconds
206
+ * @param modified Whether response was modified
207
+ */
208
+ updateResponseStats(processingTime, modified) {
209
+ this.stats.totalResponses++;
210
+ this.stats.totalProcessingTime += processingTime;
211
+ this.stats.averageProcessingTime = this.stats.totalProcessingTime /
212
+ (this.stats.totalRequests + this.stats.totalResponses + this.stats.totalErrors);
213
+ if (modified) {
214
+ this.stats.modifications++;
215
+ }
216
+ }
217
+ /**
218
+ * Update error statistics
219
+ * @param processingTime Processing time in milliseconds
220
+ * @param modified Whether error was modified
221
+ */
222
+ updateErrorStats(processingTime, modified) {
223
+ this.stats.totalErrors++;
224
+ this.stats.totalProcessingTime += processingTime;
225
+ this.stats.averageProcessingTime = this.stats.totalProcessingTime /
226
+ (this.stats.totalRequests + this.stats.totalResponses + this.stats.totalErrors);
227
+ if (modified) {
228
+ this.stats.modifications++;
229
+ }
230
+ }
231
+ }
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Abstract base load balancer implementation
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * Abstract base load balancer implementation
7
+ * Provides common functionality for all load balancing strategy implementations
8
+ */
9
+ export class BaseLoadBalancer {
10
+ /**
11
+ * Create a new base load balancer instance
12
+ * @param name Load balancer name
13
+ * @param config Load balancer configuration
14
+ */
15
+ constructor(name, config) {
16
+ /** Total selections made */
17
+ this.totalSelections = 0;
18
+ /** Instance statistics */
19
+ this.instanceStats = new Map();
20
+ /** Load balancer statistics */
21
+ this.stats = {
22
+ totalSelections: 0,
23
+ successfulSelections: 0,
24
+ failedSelections: 0,
25
+ averageSelectionTime: 0,
26
+ instanceStats: {},
27
+ };
28
+ this.name = name;
29
+ this.config = { ...config };
30
+ this.initialize();
31
+ }
32
+ /**
33
+ * Initialize load balancer
34
+ */
35
+ initialize() {
36
+ // Can be overridden by subclasses
37
+ }
38
+ /**
39
+ * Select multiple service instances
40
+ * @param instances Available service instances
41
+ * @param count Number of instances to select
42
+ * @param context Selection context
43
+ * @returns Selected service instances
44
+ */
45
+ selectMultiple(instances, count, context) {
46
+ const selected = [];
47
+ const available = [...instances];
48
+ for (let i = 0; i < count && available.length > 0; i++) {
49
+ const instance = this.select(available, context);
50
+ selected.push(instance);
51
+ // Remove selected instance from available pool
52
+ const index = available.findIndex(inst => inst.id === instance.id);
53
+ if (index !== -1) {
54
+ available.splice(index, 1);
55
+ }
56
+ }
57
+ return selected;
58
+ }
59
+ /**
60
+ * Update instance statistics (for adaptive strategies)
61
+ * @param instance Service instance
62
+ * @param success Whether the request was successful
63
+ * @param responseTime Response time in milliseconds
64
+ */
65
+ updateStats(instance, success, responseTime) {
66
+ const instanceId = instance.id;
67
+ const now = Date.now();
68
+ let stats = this.instanceStats.get(instanceId);
69
+ if (!stats) {
70
+ stats = {
71
+ selections: 0,
72
+ successes: 0,
73
+ failures: 0,
74
+ totalResponseTime: 0,
75
+ averageResponseTime: undefined,
76
+ lastSelected: undefined,
77
+ };
78
+ this.instanceStats.set(instanceId, stats);
79
+ }
80
+ stats.selections++;
81
+ stats.lastSelected = now;
82
+ if (success) {
83
+ stats.successes++;
84
+ if (responseTime !== undefined) {
85
+ stats.totalResponseTime += responseTime;
86
+ stats.averageResponseTime = stats.totalResponseTime / stats.selections;
87
+ }
88
+ }
89
+ else {
90
+ stats.failures++;
91
+ }
92
+ // Update aggregate stats
93
+ this.updateAggregateStats(instanceId, stats);
94
+ }
95
+ /**
96
+ * Update aggregate statistics
97
+ * @param instanceId Instance ID
98
+ * @param instanceStats Instance statistics
99
+ */
100
+ updateAggregateStats(instanceId, instanceStats) {
101
+ this.stats.instanceStats[instanceId] = {
102
+ selections: instanceStats.selections,
103
+ successes: instanceStats.successes,
104
+ failures: instanceStats.failures,
105
+ averageResponseTime: instanceStats.averageResponseTime,
106
+ };
107
+ }
108
+ /**
109
+ * Reset instance statistics
110
+ * @param instanceId Optional instance ID to reset, or reset all
111
+ */
112
+ resetStats(instanceId) {
113
+ if (instanceId) {
114
+ this.instanceStats.delete(instanceId);
115
+ delete this.stats.instanceStats[instanceId];
116
+ }
117
+ else {
118
+ this.instanceStats.clear();
119
+ this.stats.instanceStats = {};
120
+ this.totalSelections = 0;
121
+ this.lastSelectionTime = undefined;
122
+ this.stats.totalSelections = 0;
123
+ this.stats.successfulSelections = 0;
124
+ this.stats.failedSelections = 0;
125
+ this.stats.averageSelectionTime = 0;
126
+ }
127
+ }
128
+ /**
129
+ * Get instance statistics
130
+ * @param instanceId Optional instance ID to get stats for
131
+ */
132
+ getStats(instanceId) {
133
+ if (instanceId) {
134
+ const stats = this.instanceStats.get(instanceId);
135
+ return stats ? { ...stats, instanceId } : {};
136
+ }
137
+ return {
138
+ ...this.stats,
139
+ totalSelections: this.totalSelections,
140
+ lastSelectionTime: this.lastSelectionTime,
141
+ };
142
+ }
143
+ /**
144
+ * Check if an instance is currently eligible for selection
145
+ * @param instance Service instance to check
146
+ */
147
+ isEligible(instance) {
148
+ // Default eligibility: instance must be healthy
149
+ if (instance.status !== 'healthy') {
150
+ return false;
151
+ }
152
+ // Check if instance is within weight limits (if weighted strategy)
153
+ if (this.config.weights) {
154
+ const weight = this.config.weights[instance.id];
155
+ if (weight !== undefined && weight <= 0) {
156
+ return false;
157
+ }
158
+ }
159
+ return true;
160
+ }
161
+ /**
162
+ * Filter instances based on eligibility
163
+ * @param instances Instances to filter
164
+ */
165
+ filterEligible(instances) {
166
+ return instances.filter(instance => this.isEligible(instance));
167
+ }
168
+ /**
169
+ * Sort instances by selection priority
170
+ * @param instances Instances to sort
171
+ */
172
+ sortByPriority(instances) {
173
+ // Default: no sorting, return as-is
174
+ return [...instances];
175
+ }
176
+ /**
177
+ * Get instance weight
178
+ * @param instance Service instance
179
+ * @returns Instance weight
180
+ */
181
+ getInstanceWeight(instance) {
182
+ if (this.config.weights && this.config.weights[instance.id]) {
183
+ return this.config.weights[instance.id];
184
+ }
185
+ if (instance.weight !== undefined) {
186
+ return instance.weight;
187
+ }
188
+ return 1; // Default weight
189
+ }
190
+ /**
191
+ * Record selection
192
+ * @param instance Selected instance
193
+ * @param selectionTime Selection time in milliseconds
194
+ */
195
+ recordSelection(instance, selectionTime) {
196
+ this.totalSelections++;
197
+ this.lastSelectionTime = Date.now();
198
+ this.stats.totalSelections++;
199
+ // Update average selection time
200
+ this.stats.averageSelectionTime = ((this.stats.averageSelectionTime * (this.stats.totalSelections - 1) + selectionTime) /
201
+ this.stats.totalSelections);
202
+ // Update instance stats
203
+ this.updateStats(instance, true);
204
+ }
205
+ /**
206
+ * Create selection result
207
+ * @param instance Selected instance
208
+ * @param instances Available instances
209
+ * @param selectionTime Selection time in milliseconds
210
+ * @returns Selection result
211
+ */
212
+ createSelectionResult(instance, instances, selectionTime) {
213
+ return {
214
+ selectedInstance: instance,
215
+ availableInstances: instances,
216
+ timestamp: Date.now(),
217
+ selectionDuration: selectionTime,
218
+ metadata: {
219
+ strategy: this.name,
220
+ totalSelections: this.totalSelections,
221
+ },
222
+ };
223
+ }
224
+ /**
225
+ * Update load balancer configuration
226
+ * @param config New configuration
227
+ */
228
+ updateConfig(config) {
229
+ this.config = { ...this.config, ...config };
230
+ }
231
+ /**
232
+ * Get load balancer statistics
233
+ */
234
+ getStrategyStats() {
235
+ return { ...this.stats };
236
+ }
237
+ /**
238
+ * Reset load balancer statistics
239
+ */
240
+ resetStrategyStats() {
241
+ this.stats = {
242
+ totalSelections: 0,
243
+ successfulSelections: 0,
244
+ failedSelections: 0,
245
+ averageSelectionTime: 0,
246
+ instanceStats: {},
247
+ };
248
+ }
249
+ /**
250
+ * Health check for the load balancer
251
+ */
252
+ healthCheck() {
253
+ const healthy = true; // Load balancer is always healthy
254
+ return {
255
+ healthy,
256
+ message: 'Load balancer is operational',
257
+ details: {
258
+ name: this.name,
259
+ totalSelections: this.totalSelections,
260
+ instanceCount: this.instanceStats.size,
261
+ config: this.config,
262
+ },
263
+ };
264
+ }
265
+ }