@sailboat-computer/resilience
Version:
Enhanced resilience patterns for sailboat computer v3 with marine-specific adaptations
150 lines (134 loc) • 4.4 kB
text/typescript
/**
* Main exports for the resilience framework
*/
// Export all types
export * from './types';
// Export circuit breaker components
export * from './circuit-breaker/MarineCircuitBreaker';
// Export bulkhead components
export * from './bulkhead/ResourcePool';
export * from './bulkhead/BulkheadManager';
// Export timeout components
export * from './timeout/TimeoutManager';
// Export retry components
export * from './retry/RetryManager';
// Export degradation components
export * from './degradation/DegradationManager';
// Constants are already exported via './types'
// TODO: Export additional components when implemented
/**
* Factory function to create a marine circuit breaker with default configuration
*/
import { MarineCircuitBreaker } from './circuit-breaker/MarineCircuitBreaker';
import { CircuitBreakerConfig } from './types';
import { OperationalContext, MarineFailureType } from './types/marine-constants';
export function createMarineCircuitBreaker(
name: string,
config?: Partial<CircuitBreakerConfig>
): MarineCircuitBreaker {
return new MarineCircuitBreaker(name, config);
}
/**
* Pre-configured circuit breakers for common marine scenarios
*/
export const MarineCircuitBreakers = {
/**
* Circuit breaker for sensor operations
*/
sensor: (name: string) => createMarineCircuitBreaker(`sensor-${name}`, {
failureThreshold: 3,
successThreshold: 2,
timeout: 30000, // 30 seconds
operationalContext: OperationalContext.SAILING,
failureTypes: [MarineFailureType.SENSOR_FAILURE, MarineFailureType.TIMEOUT],
environmentalAdjustments: {
roughSeas: true,
lowPower: false,
limitedConnectivity: false
},
volumeThreshold: 5,
errorPercentageThreshold: 60,
slowCallDurationThreshold: 3000, // 3 seconds
slowCallRateThreshold: 40
}),
/**
* Circuit breaker for network operations
*/
network: (name: string) => createMarineCircuitBreaker(`network-${name}`, {
failureThreshold: 5,
successThreshold: 3,
timeout: 120000, // 2 minutes
operationalContext: OperationalContext.SAILING,
failureTypes: [MarineFailureType.CONNECTIVITY_LOSS, MarineFailureType.TIMEOUT],
environmentalAdjustments: {
roughSeas: false,
lowPower: true,
limitedConnectivity: true
},
volumeThreshold: 10,
errorPercentageThreshold: 70,
slowCallDurationThreshold: 10000, // 10 seconds
slowCallRateThreshold: 60
}),
/**
* Circuit breaker for critical safety operations
*/
safety: (name: string) => createMarineCircuitBreaker(`safety-${name}`, {
failureThreshold: 2,
successThreshold: 1,
timeout: 15000, // 15 seconds
operationalContext: OperationalContext.EMERGENCY,
failureTypes: [
MarineFailureType.SENSOR_FAILURE,
MarineFailureType.POWER_FLUCTUATION,
MarineFailureType.TIMEOUT
],
environmentalAdjustments: {
roughSeas: true,
lowPower: true,
limitedConnectivity: true
},
volumeThreshold: 3,
errorPercentageThreshold: 30,
slowCallDurationThreshold: 2000, // 2 seconds
slowCallRateThreshold: 25
}),
/**
* Circuit breaker for power-sensitive operations
*/
power: (name: string) => createMarineCircuitBreaker(`power-${name}`, {
failureThreshold: 4,
successThreshold: 2,
timeout: 60000, // 1 minute
operationalContext: OperationalContext.SAILING,
failureTypes: [MarineFailureType.POWER_FLUCTUATION, MarineFailureType.RESOURCE_EXHAUSTION],
environmentalAdjustments: {
roughSeas: false,
lowPower: true,
limitedConnectivity: false
},
volumeThreshold: 8,
errorPercentageThreshold: 50,
slowCallDurationThreshold: 5000, // 5 seconds
slowCallRateThreshold: 45
}),
/**
* Circuit breaker for maintenance operations
*/
maintenance: (name: string) => createMarineCircuitBreaker(`maintenance-${name}`, {
failureThreshold: 10,
successThreshold: 5,
timeout: 300000, // 5 minutes
operationalContext: OperationalContext.MAINTENANCE,
failureTypes: [MarineFailureType.TIMEOUT, MarineFailureType.RESOURCE_EXHAUSTION],
environmentalAdjustments: {
roughSeas: false,
lowPower: false,
limitedConnectivity: false
},
volumeThreshold: 20,
errorPercentageThreshold: 80,
slowCallDurationThreshold: 30000, // 30 seconds
slowCallRateThreshold: 70
})
};