workos-ruby
workos-ruby copied to clipboard
Add auto-idempotency keys and retry logic for audit log events
Updating python library sdk to always generating an idempotencyKey if not present when POSTing audit-logs events. Also updated to add retryability to endpoints. I set it up to not be enabled by default
/review
@greptile review plz
Greptile Summary
- Adds automatic UUID generation for idempotency keys when not provided for audit log event creation
- Implements retry logic with exponential backoff for 5xx, 408, and 429 HTTP errors with configurable max retries (defaults: 0 for general requests, 3 for audit logs)
Confidence Score: 5/5
- This PR is safe to merge with no critical issues found
- The implementation is well-designed with proper retry logic, comprehensive test coverage, and follows Ruby best practices. The retry mechanism correctly handles retryable errors with exponential backoff, reuses HTTP connections, and maintains idempotency across retries. All edge cases are tested.
- No files require special attention
Important Files Changed
| Filename | Overview |
|---|---|
| lib/workos/audit_logs.rb | Added auto-generation of idempotency keys using SecureRandom.uuid and configured retry logic to use audit_log_max_retries setting |
| lib/workos/client.rb | Implemented retry logic with exponential backoff for 5xx, 408, and 429 errors, including support for Retry-After headers |
Sequence Diagram
sequenceDiagram
participant User
participant AuditLogs
participant Client
participant API
User->>AuditLogs: create_event(organization, event, idempotency_key)
AuditLogs->>AuditLogs: Generate UUID if idempotency_key is nil
AuditLogs->>Client: execute_request(request, retries: audit_log_max_retries)
Client->>API: POST /audit_logs/events with Idempotency-Key
alt Request succeeds (2xx)
API-->>Client: 200/201 response
Client-->>AuditLogs: response
AuditLogs-->>User: response
else Retryable error (5xx, 408, 429)
API-->>Client: 500/503/408/429 response
Client->>Client: Check attempt < retries
Client->>Client: Calculate backoff delay
Client->>Client: Sleep for delay
Client->>Client: Increment attempt counter
Client->>API: Retry POST with same Idempotency-Key
alt Retry succeeds
API-->>Client: 200/201 response
Client-->>AuditLogs: response
AuditLogs-->>User: response
else Max retries exceeded
API-->>Client: Still failing
Client->>Client: Raise error (APIError/TimeoutError)
Client-->>User: Error
end
else Non-retryable error (4xx)
API-->>Client: 400/401/403/404 response
Client->>Client: Raise error immediately
Client-->>User: Error
end
@greptile re-review plz
@greptile re-review plz
@greptile re-review plz
@greptile re-review plz
@greptile re-review plz