Skip to content

Commit

Permalink
try retrying acquireStream directly
Browse files Browse the repository at this point in the history
  • Loading branch information
dayaffe committed Jan 22, 2025
1 parent 9614523 commit 79ef053
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ public class CRTClientEngine: HTTPClient {
do {
let acquireConnectionStart = Date().timeIntervalSinceReferenceDate
logger.info("TEST LOGGER WORKS BEFORE ACQUIRE STREAM")
let stream = try await connectionMgr.acquireStream(requestOptions: requestOptions)
// Retry logic for acquiring the stream
let stream = try await retryWithBackoff(maxRetries: 3, initialDelay: 0.5) {
try await connectionMgr.acquireStream(requestOptions: requestOptions)
}
let acquireConnectionEnd = Date().timeIntervalSinceReferenceDate

// Telemetry
Expand Down Expand Up @@ -465,6 +468,30 @@ public class CRTClientEngine: HTTPClient {
}
}

// Helper function for retry logic
private func retryWithBackoff<T>(
maxRetries: Int,
initialDelay: TimeInterval,
task: @escaping () async throws -> T
) async throws -> T {
var attempt = 0
var delay = initialDelay

while attempt < maxRetries {
do {
return try await task()
} catch {
if attempt >= maxRetries - 1 {
throw error // Exhaust retries
}
await Task.sleep(UInt64(delay * 1_000_000_000)) // Delay before retry

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-14, Xcode_15.2, platform=macOS)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-14, Xcode_15.2, platform=tvOS Simulator,OS=17.2,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=tvOS Simulator,OS=18.1,name=Apple TV 4K (3rd generation)...

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-14, Xcode_15.2, platform=iOS Simulator,OS=17.2,name=iPhone 15)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-14, Xcode_15.2, platform=visionOS Simulator,OS=1.0,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=visionOS Simulator,OS=2.1,name=Apple Vision Pro)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'

Check warning on line 487 in Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

View workflow job for this annotation

GitHub Actions / apple-ci (macos-15, Xcode_16.1, platform=iOS Simulator,OS=18.1,name=iPhone 16)

'sleep' is deprecated: replaced by 'Task.sleep(nanoseconds:)'
delay *= 2 // Exponential backoff
attempt += 1
}
}
throw NSError(domain: "RetryError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Max retries reached"])
}

/// Creates a `HTTPRequestOptions` object that can be used to make a HTTP request
/// - Parameters:
/// - request: The `HTTPRequestBase` object that contains the request information
Expand Down

0 comments on commit 79ef053

Please sign in to comment.