Skip to content

Commit

Permalink
fix: Only retry connections on specific codes (#84)
Browse files Browse the repository at this point in the history
* fix: Only retry connections on specific codes

* nit

* remove seconds
  • Loading branch information
cbaker6 authored Mar 17, 2023
1 parent c371d5d commit 8c7d8dc
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
# Parse-Swift Changelog

### main
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.3.2...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.3.3...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 5.3.3
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.3.2...5.3.3), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.3.3/documentation/parseswift)

__Fixes__
* Only retry connections on specific codes ([#84](https://github.com/netreconlab/Parse-Swift/pull/84)), thanks to [Corey Baker](https://github.com/cbaker6).

### 5.3.2
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.3.1...5.3.2), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.3.2/documentation/parseswift)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/netreconlab/Parse-Swift", .upToNextMajor(from: "5.1.1")),
.package(url: "https://github.com/netreconlab/Parse-Swift", .upToNextMajor(from: "5.3.3")),
]
)
```
Expand Down
37 changes: 21 additions & 16 deletions Sources/ParseSwift/Extensions/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ internal extension URLSession {
return
}

// If there is current response data, update the client now.
if allowIntermediateResponses {
let result = await self.makeResult(request: request,
responseData: responseData,
urlResponse: urlResponse,
responseError: nil,
mapper: mapper)
completion(result)
}

var delayInterval = TimeInterval()

// Check for constant delays in header information.
Expand All @@ -206,10 +196,30 @@ internal extension URLSession {
}
}

default:
case 408, 425, 500, 504:
if let interval = Utility.computeDelay(Utility.reconnectInterval(2)) {
delayInterval = interval
}

default:
// Don't retry based on error code.
let result = await self.makeResult(request: request,
responseData: responseData,
urlResponse: urlResponse,
responseError: nil,
mapper: mapper)
completion(result)
return
}

// If there is current response data, update the client now.
if allowIntermediateResponses {
let result = await self.makeResult(request: request,
responseData: responseData,
urlResponse: urlResponse,
responseError: nil,
mapper: mapper)
completion(result)
}

if delayInterval < 1.0 {
Expand All @@ -218,11 +228,6 @@ internal extension URLSession {
let delayIntervalNanoSeconds = UInt64(delayInterval * 1_000_000_000)
try await Task.sleep(nanoseconds: delayIntervalNanoSeconds)

// Update requestId in header for Idempotency
var request = request
if request.allHTTPHeaderFields?["X-Parse-Request-Id"] != nil {
request.allHTTPHeaderFields?["X-Parse-Request-Id"] = API.createUniqueRequestId()
}
await self.dataTask(with: request,
callbackQueue: callbackQueue,
attempts: attempts,
Expand Down
3 changes: 3 additions & 0 deletions Sources/ParseSwift/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ internal func initialize(applicationId: String,
configuration.isMigratingFromObjcSDK = migratingFromObjcSDK
configuration.isTestingSDK = testing
configuration.isTestingLiveQueryDontCloseSocket = testLiveQueryDontCloseSocket
if testing && maxConnectionAttempts == 5 {
configuration.maxConnectionAttempts = 1
}
try await initialize(configuration: configuration)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "5.3.2"
static let version = "5.3.3"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
11 changes: 2 additions & 9 deletions Tests/ParseSwiftTests/APICommandMultipleAttemptsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class APICommandMultipleAttemptsTests: XCTestCase {
clientKey: "clientKey",
primaryKey: "primaryKey",
serverURL: url,
maxConnectionAttempts: 2,
testing: true)
}

Expand Down Expand Up @@ -71,7 +72,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
errorKey: errorValue,
codeKey: codeValue
]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -104,7 +104,7 @@ class APICommandMultipleAttemptsTests: XCTestCase {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
if current == 1 {
expectation1.fulfill()
}
}
Expand All @@ -115,7 +115,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
}

func testErrorHTTPReturns400NoDataFromServer() async throws {
Parse.configuration.maxConnectionAttempts = 2
let originalError = ParseError(code: .otherCause, message: "Could not decode")
MockURLProtocol.mockRequests { _ in
return MockURLResponse(error: originalError) // Status code defaults to 400
Expand Down Expand Up @@ -155,7 +154,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
let headerKey = "x-rate-limit-reset"
let headerValue = "2"
let headerFields = [headerKey: headerValue]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -219,7 +217,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss z"
let headerValue = dateFormatter.string(from: date)
let headerFields = [headerKey: headerValue]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -272,7 +269,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
errorKey: errorValue,
codeKey: codeValue
]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -328,7 +324,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
let headerKey = "retry-after"
let headerValue = "2"
let headerFields = [headerKey: headerValue]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -392,7 +387,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss z"
let headerValue = dateFormatter.string(from: date)
let headerFields = [headerKey: headerValue]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down Expand Up @@ -445,7 +439,6 @@ class APICommandMultipleAttemptsTests: XCTestCase {
errorKey: errorValue,
codeKey: codeValue
]
Parse.configuration.maxConnectionAttempts = 2
let currentAttempts = Result()

MockURLProtocol.mockRequests { _ in
Expand Down
4 changes: 0 additions & 4 deletions Tests/ParseSwiftTests/APICommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ class APICommandTests: XCTestCase {
errorKey: errorValue,
codeKey: codeValue
]
Parse.configuration.maxConnectionAttempts = 1

MockURLProtocol.mockRequests { _ in
do {
Expand Down Expand Up @@ -245,7 +244,6 @@ class APICommandTests: XCTestCase {
}

func testErrorHTTPReturns400NoDataFromServer() async {
Parse.configuration.maxConnectionAttempts = 1
let originalError = ParseError(code: .otherCause, message: "Could not decode")
MockURLProtocol.mockRequests { _ in
return MockURLResponse(error: originalError) // Status code defaults to 400
Expand All @@ -270,7 +268,6 @@ class APICommandTests: XCTestCase {

// This is how errors HTTP errors should typically come in
func testErrorHTTP500JSON() async {
Parse.configuration.maxConnectionAttempts = 1
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -311,7 +308,6 @@ class APICommandTests: XCTestCase {
}

func testErrorHTTPReturns500NoDataFromServer() async {
Parse.configuration.maxConnectionAttempts = 1
let originalError = ParseError(code: .otherCause, message: "Could not decode")
MockURLProtocol.mockRequests { _ in
var response = MockURLResponse(error: originalError)
Expand Down
6 changes: 3 additions & 3 deletions Tests/ParseSwiftTests/ParseHealthCombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ParseHealthCombineTests: XCTestCase {
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200)
return MockURLResponse(data: encoded, statusCode: 503)
}

ParseHealth.checkPublisher()
Expand Down Expand Up @@ -119,7 +119,7 @@ class ParseHealthCombineTests: XCTestCase {
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200)
return MockURLResponse(data: encoded, statusCode: 429)
}

ParseHealth.checkPublisher()
Expand Down Expand Up @@ -153,7 +153,7 @@ class ParseHealthCombineTests: XCTestCase {
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200)
return MockURLResponse(data: encoded, statusCode: 429)
}

ParseHealth.checkPublisher()
Expand Down

0 comments on commit 8c7d8dc

Please sign in to comment.