Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GhenadieVP committed Aug 1, 2024
1 parent d44485e commit a8c7c1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
26 changes: 25 additions & 1 deletion RadixWallet/Clients/HTTPClient/HTTPClient+Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@ extension HTTPClient {

return .init(
executeRequest: { request, acceptedStatusCodes in
let (data, urlResponse) = try await session.data(for: request)
let (data, urlResponse) = try await {
// Retrying only once seems to be enough, but better to be on the safe side and retry more.
var retryAttempts = 5
while retryAttempts > 0 {
do {
return try await session.data(for: request)
} catch {
// Handle the very obscure error when the CFNetwork drops the request after it being sent.
// Note that NSURLErrorNetworkConnectionLost seems to be an opaque error hiding some other
// possible error withing CFNetwork, it does not literally mean that hte network connection
// was actually lost. This error will usually be thrown when the request was made right after
// the app did come to foreground, it happens seldomly, but consistently.
// As a workaround - retry the request if it failed initially.
if let nsError = error as NSError?,
nsError.domain == NSURLErrorDomain,
nsError.code == NSURLErrorNetworkConnectionLost
{
retryAttempts -= 1
continue
}
throw error
}
}
throw RequestRetryAttemptsExceeded()
}()

guard let httpURLResponse = urlResponse as? HTTPURLResponse else {
throw ExpectedHTTPURLResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ public struct ExpectedHTTPURLResponse: Swift.Error {
public init() {}
}

// MARK: - RequestRetryAttemptsExceeded
public struct RequestRetryAttemptsExceeded: Swift.Error {
public init() {}
}

// MARK: - BadHTTPResponseCode
public struct BadHTTPResponseCode: LocalizedError {
public let got: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ extension RadixConnectMobile {
}

func handleRequest(_ request: URL) async throws {
@Dependency(\.continuousClock) var clock
// A slight delay before handling the request.
//
// This is mainly added to fix the following issue:
// In some cases the Wallet will show the "Failed to validate dApp" alert.
//
// The cause for this issue is that during dApp validation, when Dev mode is not enabled,
// network requests are being made, and seldomly, but quite consistent, the OS will terminate
// the request with the quite obscure message - "Network connection lost".
// Likely that this is because the app is not fully in foreground at the moment the request is being made.
// So adding a small delay allows the OS to be ready to handle the request. Still, this assumption is based
// purely on expirementation, and there might be some other more robust fix.
try? await clock.sleep(for: .milliseconds(100))

let result = try await radixConnectMobile.handleDeepLink(url: request.absoluteString)
incomingMessagesSubject.send(
.init(
Expand Down

0 comments on commit a8c7c1a

Please sign in to comment.