@gorbchain-xyz/chaindecode
Version:
GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.
130 lines (129 loc) • 4.32 kB
TypeScript
/**
* Retry configuration options
*/
export interface RetryOptions {
/** Maximum number of retry attempts */
maxAttempts?: number;
/** Initial delay between retries in milliseconds */
initialDelay?: number;
/** Maximum delay between retries in milliseconds */
maxDelay?: number;
/** Backoff multiplier for exponential backoff */
backoffMultiplier?: number;
/** Add jitter to prevent thundering herd */
jitter?: boolean;
/** Custom retry condition function */
retryCondition?: (error: Error) => boolean;
/** Callback called before each retry */
onRetry?: (error: Error, attempt: number) => void;
}
/**
* Default retry options
*/
export declare const DEFAULT_RETRY_OPTIONS: Required<RetryOptions>;
/**
* Circuit breaker states
*/
export declare enum CircuitBreakerState {
CLOSED = "closed",
OPEN = "open",
HALF_OPEN = "half-open"
}
/**
* Circuit breaker configuration
*/
export interface CircuitBreakerOptions {
/** Number of failures before opening circuit */
failureThreshold?: number;
/** Time in milliseconds to wait before attempting to close circuit */
resetTimeout?: number;
/** Number of successful calls needed to close circuit from half-open state */
successThreshold?: number;
/** Time window in milliseconds for monitoring failures */
monitoringWindow?: number;
}
/**
* Default circuit breaker options
*/
export declare const DEFAULT_CIRCUIT_BREAKER_OPTIONS: Required<CircuitBreakerOptions>;
/**
* Circuit breaker implementation
*/
export declare class CircuitBreaker {
private state;
private failureCount;
private successCount;
private lastFailureTime;
private readonly options;
constructor(options?: CircuitBreakerOptions);
/**
* Execute a function with circuit breaker protection
*/
execute<T>(fn: () => Promise<T>): Promise<T>;
/**
* Get current circuit breaker state
*/
getState(): CircuitBreakerState;
/**
* Reset circuit breaker to closed state
*/
reset(): void;
private onSuccess;
private onFailure;
}
/**
* Retry function with exponential backoff
*/
export declare function retry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
/**
* Retry with circuit breaker
*/
export declare function retryWithCircuitBreaker<T>(fn: () => Promise<T>, circuitBreaker: CircuitBreaker, retryOptions?: RetryOptions): Promise<T>;
/**
* Batch retry - retry multiple operations with shared circuit breaker
*/
export declare function batchRetry<T>(operations: (() => Promise<T>)[], options?: {
retryOptions?: RetryOptions;
circuitBreakerOptions?: CircuitBreakerOptions;
maxConcurrency?: number;
}): Promise<T[]>;
/**
* Retry decorator for class methods
*/
export declare function Retryable(options?: RetryOptions): <T extends (...args: any[]) => Promise<any>>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
/**
* Circuit breaker decorator for class methods
*/
export declare function WithCircuitBreaker(options?: CircuitBreakerOptions): <T extends (...args: any[]) => Promise<any>>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
/**
* Retry manager for coordinating multiple retry operations
*/
export declare class RetryManager {
private readonly circuitBreakers;
private readonly defaultRetryOptions;
private readonly defaultCircuitBreakerOptions;
constructor(defaultRetryOptions?: RetryOptions, defaultCircuitBreakerOptions?: CircuitBreakerOptions);
/**
* Get or create circuit breaker for a specific key
*/
getCircuitBreaker(key: string, options?: CircuitBreakerOptions): CircuitBreaker;
/**
* Execute operation with retry and circuit breaker
*/
execute<T>(key: string, fn: () => Promise<T>, options?: {
retryOptions?: RetryOptions;
circuitBreakerOptions?: CircuitBreakerOptions;
}): Promise<T>;
/**
* Reset circuit breaker for a specific key
*/
resetCircuitBreaker(key: string): void;
/**
* Reset all circuit breakers
*/
resetAll(): void;
/**
* Get status of all circuit breakers
*/
getStatus(): Record<string, CircuitBreakerState>;
}