From 54e315f00d0b1be6d6e8c775e55e92548f249430 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 9 Oct 2020 09:49:28 +1100 Subject: [PATCH] Don't immediately kick out misbehaving paths --- .../API/Onion Requests/OnionRequestAPI.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift index 29d787331..4147dca75 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift @@ -9,6 +9,9 @@ public enum OnionRequestAPI { // MARK: Settings /// The number of snodes (including the guard snode) in a path. private static let pathSize: UInt = 3 + /// The number of times a path can fail before it's replaced. + private static let pathFailureThreshold: UInt = 2 + private static var pathFailureCount: [Path:UInt] = [:] public static let pathCount: UInt = 2 @@ -375,13 +378,18 @@ public enum OnionRequestAPI { guard case HTTP.Error.httpRequestFailed(let statusCode, let json) = error else { return } let path = paths.first { $0.contains(guardSnode) } func handleUnspecificError() { - dropGuardSnode(guardSnode) guard let path = path else { return } - path.forEach { snode in - SnodeAPI.handleError(withStatusCode: statusCode, json: json, forSnode: snode) // Intentionally don't throw + let pathFailureCount = OnionRequestAPI.pathFailureCount[path] ?? 0 + if pathFailureCount >= pathFailureThreshold { + dropGuardSnode(guardSnode) + path.forEach { snode in + SnodeAPI.handleError(withStatusCode: statusCode, json: json, forSnode: snode) // Intentionally don't throw + } + print("[Test] Unspecific error; dropping: \(path)") + drop(path) + } else { + OnionRequestAPI.pathFailureCount[path] = pathFailureCount + 1 } - print("[Test] Unspecific error; dropping: \(path)") - drop(path) } let prefix = "Next node not found: " if let message = json?["result"] as? String, message.hasPrefix(prefix) {