immobiliare/RealHTTP

HTTPAltValidator not correctly triggered with multiple concurrent calls

malcommac opened this issue · 1 comments

Bug Report

This bug affect both the 0.9.x (pre-releases) and the new 1.0.0.
The bug avoid HTTPAltValidator to be triggered when multiple concurrent calls are calling the same validator. This happens because numberOfAltRequestExecuted is incremented even when no alt-request is triggered so it reach very fast the limit (maxAltRequestsToExecute).

This check should be moved right before returning the alt call.

open func validate(response: HTTPRawResponse, forRequest request: HTTPRequestProtocol) -> HTTPResponseValidatorResult {
        if let statusCode = response.error?.statusCode, triggerHTTPCodes.contains(statusCode) {
            return .passed
        }
        
        // If error is one of the errors in `triggerHTTPCodes`
        
        // If we reached the maximum number of alternate calls to execute we want to cancel any other attempt.
        if let maxAltRequestsToExecute = maxAltRequestsToExecute,
              numberOfAltRequestExecuted > maxAltRequestsToExecute {
            let error = HTTPError(.maxRetryAttemptsReached)
            return .failWithError(error)
        }
        
        guard let altOperation = requestProvider(request, response) else {
            return .passed // if no retry operation is provided we'll skip and mark the validation as passed
        }

        numberOfAltRequestExecuted += 1
        return .retryAfter(altOperation)
    }
Q A
BC Break yes
Version 0.9.x and 1.0.x

fixed in 0.9.18 - pre-release