**feat: implement robust error-handling and operation utilities for storage adapters**

- Added `BrainyError` class to classify and handle errors with types like `TIMEOUT`, `NETWORK`, `STORAGE`, `NOT_FOUND`, and `RETRY_EXHAUSTED`. Includes static helper methods for error creation and retry determination.
- Introduced `operationUtils` with utility functions for timeout, retry logic, and exponential backoff. Implements features like `withTimeout`, `withRetry`, and a combined `withTimeoutAndRetry`.
- Updated `S3CompatibleStorage` to leverage new operation utilities for timeout and retry handling, including `StorageOperationExecutors` for clean operation execution.
- Enhanced `storageFactory` to pass `OperationConfig` for configurable timeout and retry behavior.
- Extended `BrainyData` to include timeout and retry policy configuration at initialization.

**Purpose**: Improve storage reliability by introducing configurable and reusable error-handling and operation utilities, reducing code duplication and enhancing maintainability.
This commit is contained in:
David Snelling 2025-07-30 11:35:09 -07:00
parent db67ccd34f
commit e8127c54a5
5 changed files with 516 additions and 54 deletions

View file

@ -135,6 +135,60 @@ export interface BrainyDataConfig {
verbose?: boolean
}
/**
* Timeout configuration for async operations
* Controls how long operations wait before timing out
*/
timeouts?: {
/**
* Timeout for get operations in milliseconds
* Default: 30000 (30 seconds)
*/
get?: number
/**
* Timeout for add operations in milliseconds
* Default: 60000 (60 seconds)
*/
add?: number
/**
* Timeout for delete operations in milliseconds
* Default: 30000 (30 seconds)
*/
delete?: number
}
/**
* Retry policy configuration for failed operations
* Controls how operations are retried on failure
*/
retryPolicy?: {
/**
* Maximum number of retry attempts
* Default: 3
*/
maxRetries?: number
/**
* Initial delay between retries in milliseconds
* Default: 1000 (1 second)
*/
initialDelay?: number
/**
* Maximum delay between retries in milliseconds
* Default: 10000 (10 seconds)
*/
maxDelay?: number
/**
* Multiplier for exponential backoff
* Default: 2
*/
backoffMultiplier?: number
}
/**
* Real-time update configuration
* Controls how the database handles updates when data is added by external processes
@ -181,6 +235,10 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
private _dimensions: number
private loggingConfig: BrainyDataConfig['logging'] = {verbose: true}
// Timeout and retry configuration
private timeoutConfig: BrainyDataConfig['timeouts'] = {}
private retryConfig: BrainyDataConfig['retryPolicy'] = {}
// Real-time update properties
private realtimeUpdateConfig: Required<NonNullable<BrainyDataConfig['realtimeUpdates']>> = {
enabled: false,
@ -268,6 +326,10 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
// Store storage configuration for later use in init()
this.storageConfig = config.storage || {}
// Store timeout and retry configuration
this.timeoutConfig = config.timeouts || {}
this.retryConfig = config.retryPolicy || {}
// Store remote server configuration if provided
if (config.remoteServer) {
this.remoteServerConfig = config.remoteServer