Skip to content

Add Rate Limiting and Retry Logic for API Calls #134

@Patrick-Ehimen

Description

@Patrick-Ehimen

Summary

The SDK currently lacks retry logic and rate limiting for API calls, which can lead to failures during network issues, temporary service unavailability, or when hitting API rate limits. This makes the SDK less reliable in production environments.
This affects user experience and reliability, especially for:

  • File uploads that may fail due to temporary network issues
  • API key generation and authentication requests
  • Deal status checks
  • Balance and usage queries

Proposed Solution

1. Implement Exponential Backoff Retry Logic

Add a utility function that wraps API calls with configurable retry logic:

interface RetryOptions {
  maxRetries: number
  baseDelay: number
  maxDelay: number
  retryCondition?: (error: any) => boolean
}

async function withRetry<T>(
  operation: () => Promise<T>,
  options: RetryOptions = {
    maxRetries: 3,
    baseDelay: 1000,
    maxDelay: 10000
  }
): Promise<T>

2. Rate Limiting Implementation

Implement a token bucket or sliding window rate limiter to prevent overwhelming the API:

class RateLimiter {
  constructor(
    private requestsPerSecond: number,
    private burstSize: number
  ) {}
  
  async waitForToken(): Promise<void>
}

3. Smart Retry Conditions

Only retry on specific conditions:

  • Network errors (ECONNRESET, ETIMEDOUT, etc.)
  • HTTP 429 (Too Many Requests)
  • HTTP 502/503/504 (Server errors)
  • Do NOT retry on 4xx client errors (except 429)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions