|
5 | 5 |
|
6 | 6 | import { cpus, totalmem } from 'os'
|
7 | 7 |
|
| 8 | +// Constants for performance configuration |
| 9 | +const PERFORMANCE_DEFAULTS = { |
| 10 | + ALLOCATION_CONCURRENCY: 20, |
| 11 | + DEPLOYMENT_CONCURRENCY: 15, |
| 12 | + NETWORK_QUERY_CONCURRENCY: 10, |
| 13 | + BATCH_SIZE: 10, |
| 14 | + CACHE_TTL: 30_000, // 30 seconds |
| 15 | + CACHE_MAX_SIZE: 2000, |
| 16 | + CACHE_CLEANUP_INTERVAL: 60_000, // 1 minute |
| 17 | + CIRCUIT_BREAKER_FAILURE_THRESHOLD: 5, |
| 18 | + CIRCUIT_BREAKER_RESET_TIMEOUT: 60_000, // 1 minute |
| 19 | + PRIORITY_QUEUE_SIGNAL_THRESHOLD: '1000000000000000000000', // 1000 GRT |
| 20 | + PRIORITY_QUEUE_STAKE_THRESHOLD: '10000000000000000000000', // 10000 GRT |
| 21 | + NETWORK_QUERY_BATCH_SIZE: 50, |
| 22 | + NETWORK_QUERY_TIMEOUT: 30_000, // 30 seconds |
| 23 | + MAX_RETRY_ATTEMPTS: 3, |
| 24 | + RETRY_DELAY: 1000, // 1 second |
| 25 | + RETRY_BACKOFF_MULTIPLIER: 2, |
| 26 | + METRICS_INTERVAL: 60_000, // 1 minute |
| 27 | +} as const |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility function for parsing environment variables |
| 31 | + */ |
| 32 | +function parseEnvInt(key: string, defaultValue: number): number { |
| 33 | + const value = process.env[key] |
| 34 | + return value ? parseInt(value, 10) : defaultValue |
| 35 | +} |
| 36 | + |
| 37 | +function parseEnvFloat(key: string, defaultValue: number): number { |
| 38 | + const value = process.env[key] |
| 39 | + return value ? parseFloat(value) : defaultValue |
| 40 | +} |
| 41 | + |
| 42 | +function parseEnvBoolean(key: string, defaultValue: boolean): boolean { |
| 43 | + const value = process.env[key] |
| 44 | + if (value === undefined) return defaultValue |
| 45 | + return value !== 'false' |
| 46 | +} |
| 47 | + |
| 48 | +function parseEnvString(key: string, defaultValue: string): string { |
| 49 | + return process.env[key] ?? defaultValue |
| 50 | +} |
| 51 | + |
8 | 52 | export interface PerformanceConfig {
|
9 | 53 | // Concurrency settings
|
10 | 54 | allocationConcurrency: number
|
@@ -47,152 +91,121 @@ export interface PerformanceConfig {
|
47 | 91 |
|
48 | 92 | export const DEFAULT_PERFORMANCE_CONFIG: PerformanceConfig = {
|
49 | 93 | // Concurrency settings
|
50 |
| - allocationConcurrency: 20, |
51 |
| - deploymentConcurrency: 15, |
52 |
| - networkQueryConcurrency: 10, |
53 |
| - batchSize: 10, |
| 94 | + allocationConcurrency: PERFORMANCE_DEFAULTS.ALLOCATION_CONCURRENCY, |
| 95 | + deploymentConcurrency: PERFORMANCE_DEFAULTS.DEPLOYMENT_CONCURRENCY, |
| 96 | + networkQueryConcurrency: PERFORMANCE_DEFAULTS.NETWORK_QUERY_CONCURRENCY, |
| 97 | + batchSize: PERFORMANCE_DEFAULTS.BATCH_SIZE, |
54 | 98 |
|
55 | 99 | // Cache settings
|
56 | 100 | enableCache: true,
|
57 |
| - cacheTTL: 30000, // 30 seconds |
58 |
| - cacheMaxSize: 2000, |
59 |
| - cacheCleanupInterval: 60000, // 1 minute |
| 101 | + cacheTTL: PERFORMANCE_DEFAULTS.CACHE_TTL, |
| 102 | + cacheMaxSize: PERFORMANCE_DEFAULTS.CACHE_MAX_SIZE, |
| 103 | + cacheCleanupInterval: PERFORMANCE_DEFAULTS.CACHE_CLEANUP_INTERVAL, |
60 | 104 |
|
61 | 105 | // Circuit breaker settings
|
62 | 106 | enableCircuitBreaker: true,
|
63 |
| - circuitBreakerFailureThreshold: 5, |
64 |
| - circuitBreakerResetTimeout: 60000, // 1 minute |
| 107 | + circuitBreakerFailureThreshold: PERFORMANCE_DEFAULTS.CIRCUIT_BREAKER_FAILURE_THRESHOLD, |
| 108 | + circuitBreakerResetTimeout: PERFORMANCE_DEFAULTS.CIRCUIT_BREAKER_RESET_TIMEOUT, |
65 | 109 | circuitBreakerHalfOpenMaxAttempts: 3,
|
66 | 110 |
|
67 | 111 | // Priority queue settings
|
68 | 112 | enablePriorityQueue: true,
|
69 |
| - priorityQueueSignalThreshold: '1000000000000000000000', // 1000 GRT |
70 |
| - priorityQueueStakeThreshold: '10000000000000000000000', // 10000 GRT |
| 113 | + priorityQueueSignalThreshold: PERFORMANCE_DEFAULTS.PRIORITY_QUEUE_SIGNAL_THRESHOLD, |
| 114 | + priorityQueueStakeThreshold: PERFORMANCE_DEFAULTS.PRIORITY_QUEUE_STAKE_THRESHOLD, |
71 | 115 |
|
72 | 116 | // Network settings
|
73 | 117 | enableParallelNetworkQueries: true,
|
74 |
| - networkQueryBatchSize: 50, |
75 |
| - networkQueryTimeout: 30000, // 30 seconds |
| 118 | + networkQueryBatchSize: PERFORMANCE_DEFAULTS.NETWORK_QUERY_BATCH_SIZE, |
| 119 | + networkQueryTimeout: PERFORMANCE_DEFAULTS.NETWORK_QUERY_TIMEOUT, |
76 | 120 |
|
77 | 121 | // Retry settings
|
78 |
| - maxRetryAttempts: 3, |
79 |
| - retryDelay: 1000, // 1 second |
80 |
| - retryBackoffMultiplier: 2, |
| 122 | + maxRetryAttempts: PERFORMANCE_DEFAULTS.MAX_RETRY_ATTEMPTS, |
| 123 | + retryDelay: PERFORMANCE_DEFAULTS.RETRY_DELAY, |
| 124 | + retryBackoffMultiplier: PERFORMANCE_DEFAULTS.RETRY_BACKOFF_MULTIPLIER, |
81 | 125 |
|
82 | 126 | // Monitoring settings
|
83 | 127 | enableMetrics: true,
|
84 |
| - metricsInterval: 60000, // 1 minute |
| 128 | + metricsInterval: PERFORMANCE_DEFAULTS.METRICS_INTERVAL, |
85 | 129 | enableDetailedLogging: false,
|
86 | 130 | }
|
87 | 131 |
|
88 | 132 | /**
|
89 |
| - * Load performance configuration from environment variables |
| 133 | + * Apply concurrency-related environment variable overrides |
90 | 134 | */
|
91 |
| -export function loadPerformanceConfig(): PerformanceConfig { |
92 |
| - const config = { ...DEFAULT_PERFORMANCE_CONFIG } |
93 |
| - |
94 |
| - // Override with environment variables if present |
95 |
| - if (process.env.ALLOCATION_CONCURRENCY) { |
96 |
| - config.allocationConcurrency = parseInt(process.env.ALLOCATION_CONCURRENCY) |
97 |
| - } |
98 |
| - |
99 |
| - if (process.env.DEPLOYMENT_CONCURRENCY) { |
100 |
| - config.deploymentConcurrency = parseInt(process.env.DEPLOYMENT_CONCURRENCY) |
101 |
| - } |
102 |
| - |
103 |
| - if (process.env.NETWORK_QUERY_CONCURRENCY) { |
104 |
| - config.networkQueryConcurrency = parseInt( |
105 |
| - process.env.NETWORK_QUERY_CONCURRENCY, |
106 |
| - ) |
107 |
| - } |
108 |
| - |
109 |
| - if (process.env.BATCH_SIZE) { |
110 |
| - config.batchSize = parseInt(process.env.BATCH_SIZE) |
111 |
| - } |
112 |
| - |
113 |
| - if (process.env.ENABLE_CACHE !== undefined) { |
114 |
| - config.enableCache = process.env.ENABLE_CACHE !== 'false' |
115 |
| - } |
116 |
| - |
117 |
| - if (process.env.CACHE_TTL) { |
118 |
| - config.cacheTTL = parseInt(process.env.CACHE_TTL) |
119 |
| - } |
120 |
| - |
121 |
| - if (process.env.CACHE_MAX_SIZE) { |
122 |
| - config.cacheMaxSize = parseInt(process.env.CACHE_MAX_SIZE) |
123 |
| - } |
124 |
| - |
125 |
| - if (process.env.ENABLE_CIRCUIT_BREAKER !== undefined) { |
126 |
| - config.enableCircuitBreaker = process.env.ENABLE_CIRCUIT_BREAKER !== 'false' |
127 |
| - } |
128 |
| - |
129 |
| - if (process.env.CIRCUIT_BREAKER_FAILURE_THRESHOLD) { |
130 |
| - config.circuitBreakerFailureThreshold = parseInt( |
131 |
| - process.env.CIRCUIT_BREAKER_FAILURE_THRESHOLD, |
132 |
| - ) |
133 |
| - } |
134 |
| - |
135 |
| - if (process.env.CIRCUIT_BREAKER_RESET_TIMEOUT) { |
136 |
| - config.circuitBreakerResetTimeout = parseInt( |
137 |
| - process.env.CIRCUIT_BREAKER_RESET_TIMEOUT, |
138 |
| - ) |
139 |
| - } |
140 |
| - |
141 |
| - if (process.env.ENABLE_PRIORITY_QUEUE !== undefined) { |
142 |
| - config.enablePriorityQueue = process.env.ENABLE_PRIORITY_QUEUE !== 'false' |
143 |
| - } |
144 |
| - |
145 |
| - if (process.env.PRIORITY_QUEUE_SIGNAL_THRESHOLD) { |
146 |
| - config.priorityQueueSignalThreshold = |
147 |
| - process.env.PRIORITY_QUEUE_SIGNAL_THRESHOLD |
148 |
| - } |
149 |
| - |
150 |
| - if (process.env.PRIORITY_QUEUE_STAKE_THRESHOLD) { |
151 |
| - config.priorityQueueStakeThreshold = |
152 |
| - process.env.PRIORITY_QUEUE_STAKE_THRESHOLD |
153 |
| - } |
154 |
| - |
155 |
| - if (process.env.ENABLE_PARALLEL_NETWORK_QUERIES !== undefined) { |
156 |
| - config.enableParallelNetworkQueries = |
157 |
| - process.env.ENABLE_PARALLEL_NETWORK_QUERIES !== 'false' |
158 |
| - } |
| 135 | +function applyConcurrencySettings(config: PerformanceConfig): void { |
| 136 | + config.allocationConcurrency = parseEnvInt('ALLOCATION_CONCURRENCY', config.allocationConcurrency) |
| 137 | + config.deploymentConcurrency = parseEnvInt('DEPLOYMENT_CONCURRENCY', config.deploymentConcurrency) |
| 138 | + config.networkQueryConcurrency = parseEnvInt('NETWORK_QUERY_CONCURRENCY', config.networkQueryConcurrency) |
| 139 | + config.batchSize = parseEnvInt('BATCH_SIZE', config.batchSize) |
| 140 | +} |
159 | 141 |
|
160 |
| - if (process.env.NETWORK_QUERY_BATCH_SIZE) { |
161 |
| - config.networkQueryBatchSize = parseInt( |
162 |
| - process.env.NETWORK_QUERY_BATCH_SIZE, |
163 |
| - ) |
164 |
| - } |
| 142 | +/** |
| 143 | + * Apply cache-related environment variable overrides |
| 144 | + */ |
| 145 | +function applyCacheSettings(config: PerformanceConfig): void { |
| 146 | + config.enableCache = parseEnvBoolean('ENABLE_CACHE', config.enableCache) |
| 147 | + config.cacheTTL = parseEnvInt('CACHE_TTL', config.cacheTTL) |
| 148 | + config.cacheMaxSize = parseEnvInt('CACHE_MAX_SIZE', config.cacheMaxSize) |
| 149 | +} |
165 | 150 |
|
166 |
| - if (process.env.NETWORK_QUERY_TIMEOUT) { |
167 |
| - config.networkQueryTimeout = parseInt(process.env.NETWORK_QUERY_TIMEOUT) |
168 |
| - } |
| 151 | +/** |
| 152 | + * Apply circuit breaker environment variable overrides |
| 153 | + */ |
| 154 | +function applyCircuitBreakerSettings(config: PerformanceConfig): void { |
| 155 | + config.enableCircuitBreaker = parseEnvBoolean('ENABLE_CIRCUIT_BREAKER', config.enableCircuitBreaker) |
| 156 | + config.circuitBreakerFailureThreshold = parseEnvInt('CIRCUIT_BREAKER_FAILURE_THRESHOLD', config.circuitBreakerFailureThreshold) |
| 157 | + config.circuitBreakerResetTimeout = parseEnvInt('CIRCUIT_BREAKER_RESET_TIMEOUT', config.circuitBreakerResetTimeout) |
| 158 | +} |
169 | 159 |
|
170 |
| - if (process.env.MAX_RETRY_ATTEMPTS) { |
171 |
| - config.maxRetryAttempts = parseInt(process.env.MAX_RETRY_ATTEMPTS) |
172 |
| - } |
| 160 | +/** |
| 161 | + * Apply priority queue environment variable overrides |
| 162 | + */ |
| 163 | +function applyPriorityQueueSettings(config: PerformanceConfig): void { |
| 164 | + config.enablePriorityQueue = parseEnvBoolean('ENABLE_PRIORITY_QUEUE', config.enablePriorityQueue) |
| 165 | + config.priorityQueueSignalThreshold = parseEnvString('PRIORITY_QUEUE_SIGNAL_THRESHOLD', config.priorityQueueSignalThreshold) |
| 166 | + config.priorityQueueStakeThreshold = parseEnvString('PRIORITY_QUEUE_STAKE_THRESHOLD', config.priorityQueueStakeThreshold) |
| 167 | +} |
173 | 168 |
|
174 |
| - if (process.env.RETRY_DELAY) { |
175 |
| - config.retryDelay = parseInt(process.env.RETRY_DELAY) |
176 |
| - } |
| 169 | +/** |
| 170 | + * Apply network-related environment variable overrides |
| 171 | + */ |
| 172 | +function applyNetworkSettings(config: PerformanceConfig): void { |
| 173 | + config.enableParallelNetworkQueries = parseEnvBoolean('ENABLE_PARALLEL_NETWORK_QUERIES', config.enableParallelNetworkQueries) |
| 174 | + config.networkQueryBatchSize = parseEnvInt('NETWORK_QUERY_BATCH_SIZE', config.networkQueryBatchSize) |
| 175 | + config.networkQueryTimeout = parseEnvInt('NETWORK_QUERY_TIMEOUT', config.networkQueryTimeout) |
| 176 | +} |
177 | 177 |
|
178 |
| - if (process.env.RETRY_BACKOFF_MULTIPLIER) { |
179 |
| - config.retryBackoffMultiplier = parseFloat( |
180 |
| - process.env.RETRY_BACKOFF_MULTIPLIER, |
181 |
| - ) |
182 |
| - } |
| 178 | +/** |
| 179 | + * Apply retry-related environment variable overrides |
| 180 | + */ |
| 181 | +function applyRetrySettings(config: PerformanceConfig): void { |
| 182 | + config.maxRetryAttempts = parseEnvInt('MAX_RETRY_ATTEMPTS', config.maxRetryAttempts) |
| 183 | + config.retryDelay = parseEnvInt('RETRY_DELAY', config.retryDelay) |
| 184 | + config.retryBackoffMultiplier = parseEnvFloat('RETRY_BACKOFF_MULTIPLIER', config.retryBackoffMultiplier) |
| 185 | +} |
183 | 186 |
|
184 |
| - if (process.env.ENABLE_METRICS !== undefined) { |
185 |
| - config.enableMetrics = process.env.ENABLE_METRICS !== 'false' |
186 |
| - } |
| 187 | +/** |
| 188 | + * Apply monitoring-related environment variable overrides |
| 189 | + */ |
| 190 | +function applyMonitoringSettings(config: PerformanceConfig): void { |
| 191 | + config.enableMetrics = parseEnvBoolean('ENABLE_METRICS', config.enableMetrics) |
| 192 | + config.metricsInterval = parseEnvInt('METRICS_INTERVAL', config.metricsInterval) |
| 193 | + config.enableDetailedLogging = parseEnvBoolean('ENABLE_DETAILED_LOGGING', config.enableDetailedLogging) |
| 194 | +} |
187 | 195 |
|
188 |
| - if (process.env.METRICS_INTERVAL) { |
189 |
| - config.metricsInterval = parseInt(process.env.METRICS_INTERVAL) |
190 |
| - } |
| 196 | +/** |
| 197 | + * Load performance configuration from environment variables |
| 198 | + */ |
| 199 | +export function loadPerformanceConfig(): PerformanceConfig { |
| 200 | + const config = { ...DEFAULT_PERFORMANCE_CONFIG } |
191 | 201 |
|
192 |
| - if (process.env.ENABLE_DETAILED_LOGGING !== undefined) { |
193 |
| - config.enableDetailedLogging = |
194 |
| - process.env.ENABLE_DETAILED_LOGGING === 'true' |
195 |
| - } |
| 202 | + applyConcurrencySettings(config) |
| 203 | + applyCacheSettings(config) |
| 204 | + applyCircuitBreakerSettings(config) |
| 205 | + applyPriorityQueueSettings(config) |
| 206 | + applyNetworkSettings(config) |
| 207 | + applyRetrySettings(config) |
| 208 | + applyMonitoringSettings(config) |
196 | 209 |
|
197 | 210 | return config
|
198 | 211 | }
|
|
0 commit comments