UNPKG

@sailboat-computer/resilience

Version:

Enhanced resilience patterns for sailboat computer v3 with marine-specific adaptations

274 lines 6.77 kB
/** * Retry mechanism with marine-specific adaptations */ import { MarineEnvironmentStatus, ResilienceEvent } from '../types'; import { OperationalContextType, MarineFailureTypeType } from '../types/marine-constants'; /** * Retry configuration */ export interface RetryConfig { /** * Maximum number of retry attempts */ maxRetries: number; /** * Base delay between retries in ms */ baseDelayMs: number; /** * Multiplier for exponential backoff */ backoffMultiplier: number; /** * Maximum delay between retries in ms */ maxDelayMs: number; /** * Whether to add jitter to retry delays */ useJitter: boolean; /** * Failure types that should trigger retry */ retryableFailures: MarineFailureTypeType[]; /** * Marine-specific settings */ marineSettings: { /** * Whether to adapt retry behavior to marine conditions */ adaptToMarineConditions: boolean; /** * Whether to adapt to power status */ powerAware: boolean; /** * Operational contexts where retries are allowed */ allowedContexts: OperationalContextType[]; /** * Whether to limit retries in rough seas */ limitInRoughSeas: boolean; }; } /** * Retry execution context */ export interface RetryExecutionContext { /** * Operation name for logging and metrics */ operationName: string; /** * Current operational context */ operationalContext: OperationalContextType; /** * Marine system type */ marineSystemType: 'navigation' | 'safety' | 'comfort' | 'maintenance'; /** * Operation priority */ priority: 'critical' | 'normal' | 'low'; /** * Custom retry configuration overrides */ customRetryConfig?: Partial<RetryConfig>; } /** * Retry execution result */ export interface RetryExecutionResult<T> { /** * Whether the operation was successful */ success: boolean; /** * Result data if successful */ data?: T; /** * Error if failed */ error?: Error; /** * Number of retry attempts made */ retryCount: number; /** * Total execution time including retries */ totalExecutionTime: number; /** * Delays between retry attempts */ retryDelays: number[]; /** * Whether maximum retries were reached */ maxRetriesReached: boolean; /** * Marine context during execution */ marineContext: { operationalContext: OperationalContextType; environmentalConditions?: Partial<MarineEnvironmentStatus>; }; } /** * Retry metrics */ export interface RetryMetrics { /** * Total operations attempted */ totalOperations: number; /** * Operations that succeeded without retry */ successWithoutRetry: number; /** * Operations that succeeded after retry */ successWithRetry: number; /** * Operations that failed after all retries */ failedAfterRetry: number; /** * Total retry attempts */ totalRetryAttempts: number; /** * Average retries per operation */ averageRetriesPerOperation: number; /** * Maximum retries for a single operation */ maxRetriesForOperation: number; /** * Average delay between retries */ averageRetryDelay: number; /** * Marine-specific metrics */ marineMetrics: { /** * Retries due to environmental conditions */ environmentalRetries: number; /** * Retries due to connectivity issues */ connectivityRetries: number; /** * Retries due to power issues */ powerRetries: number; /** * Retries due to sensor failures */ sensorRetries: number; }; } /** * Retry manager for marine operations */ export declare class RetryManager { private config; private metrics; private eventListeners; private marineEnvironment?; constructor(config?: Partial<RetryConfig>); /** * Execute operation with retry */ execute<T>(operation: () => Promise<T>, context: RetryExecutionContext): Promise<RetryExecutionResult<T>>; /** * Update marine environment for adaptive retry behavior */ updateMarineEnvironment(environment: MarineEnvironmentStatus): void; /** * Get current retry metrics */ getMetrics(): RetryMetrics; /** * Add event listener */ onEvent(listener: (event: ResilienceEvent) => void): void; /** * Remove event listener */ removeEventListener(listener: (event: ResilienceEvent) => void): void; /** * Get effective configuration with overrides applied */ private getEffectiveConfig; /** * Check if retry is allowed in current operational context */ private isRetryAllowed; /** * Check if error is retryable */ private isRetryableError; /** * Calculate retry delay with exponential backoff and jitter */ private calculateRetryDelay; /** * Update metrics */ private updateMetrics; /** * Delay helper function */ private delay; /** * Emit resilience event */ private emitEvent; /** * Initialize metrics */ private initializeMetrics; } /** * Default retry manager instance */ export declare const defaultRetryManager: RetryManager; /** * Convenience function to execute with retry */ export declare function executeWithRetry<T>(operation: () => Promise<T>, context: RetryExecutionContext): Promise<RetryExecutionResult<T>>; /** * Pre-configured retry contexts for marine operations */ export declare const RetryContexts: { /** * Navigation sensor operations */ navigationSensor: (operationName: string, operationalContext?: OperationalContextType) => RetryExecutionContext; /** * Safety system operations */ safetyOperation: (operationName: string, operationalContext?: OperationalContextType) => RetryExecutionContext; /** * Network operations */ networkOperation: (operationName: string, operationalContext?: OperationalContextType) => RetryExecutionContext; /** * Maintenance operations */ maintenanceOperation: (operationName: string) => RetryExecutionContext; /** * Comfort system operations */ comfortOperation: (operationName: string, operationalContext?: OperationalContextType) => RetryExecutionContext; }; //# sourceMappingURL=RetryManager.d.ts.map