|
|
@ -77,6 +77,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
]
|
|
|
|
]
|
|
|
|
SNLog("Populating snode pool using: \(target).")
|
|
|
|
SNLog("Populating snode pool using: \(target).")
|
|
|
|
let (promise, seal) = Promise<Snode>.pending()
|
|
|
|
let (promise, seal) = Promise<Snode>.pending()
|
|
|
|
|
|
|
|
Threading.workQueue.async {
|
|
|
|
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
|
|
|
|
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
|
|
|
|
HTTP.execute(.post, url, parameters: parameters, useSSLURLSession: true).map2 { json -> Snode in
|
|
|
|
HTTP.execute(.post, url, parameters: parameters, useSSLURLSession: true).map2 { json -> Snode in
|
|
|
|
guard let intermediate = json["result"] as? JSON, let rawSnodes = intermediate["service_node_states"] as? [JSON] else { throw Error.randomSnodePoolUpdatingFailed }
|
|
|
|
guard let intermediate = json["result"] as? JSON, let rawSnodes = intermediate["service_node_states"] as? [JSON] else { throw Error.randomSnodePoolUpdatingFailed }
|
|
|
@ -105,6 +106,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
SNLog("Failed to contact seed node at: \(target).")
|
|
|
|
SNLog("Failed to contact seed node at: \(target).")
|
|
|
|
seal.reject(error)
|
|
|
|
seal.reject(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return promise
|
|
|
|
return promise
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Promise<Snode> { seal in
|
|
|
|
return Promise<Snode> { seal in
|
|
|
@ -115,6 +117,9 @@ public final class SnodeAPI : NSObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static func dropSnodeFromSnodePool(_ snode: Snode) {
|
|
|
|
internal static func dropSnodeFromSnodePool(_ snode: Snode) {
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
dispatchPrecondition(condition: .onQueue(Threading.workQueue))
|
|
|
|
|
|
|
|
#endif
|
|
|
|
var snodePool = SnodeAPI.snodePool
|
|
|
|
var snodePool = SnodeAPI.snodePool
|
|
|
|
snodePool.remove(snode)
|
|
|
|
snodePool.remove(snode)
|
|
|
|
SnodeAPI.snodePool = snodePool
|
|
|
|
SnodeAPI.snodePool = snodePool
|
|
|
@ -132,6 +137,9 @@ public final class SnodeAPI : NSObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static func dropSnodeFromSwarmIfNeeded(_ snode: Snode, publicKey: String) {
|
|
|
|
public static func dropSnodeFromSwarmIfNeeded(_ snode: Snode, publicKey: String) {
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
dispatchPrecondition(condition: .onQueue(Threading.workQueue))
|
|
|
|
|
|
|
|
#endif
|
|
|
|
let swarm = SnodeAPI.swarmCache[publicKey]
|
|
|
|
let swarm = SnodeAPI.swarmCache[publicKey]
|
|
|
|
if var swarm = swarm, let index = swarm.firstIndex(of: snode) {
|
|
|
|
if var swarm = swarm, let index = swarm.firstIndex(of: snode) {
|
|
|
|
swarm.remove(at: index)
|
|
|
|
swarm.remove(at: index)
|
|
|
@ -282,6 +290,9 @@ public final class SnodeAPI : NSObject {
|
|
|
|
/// - Note: Should only be invoked from `Threading.workQueue` to avoid race conditions.
|
|
|
|
/// - Note: Should only be invoked from `Threading.workQueue` to avoid race conditions.
|
|
|
|
@discardableResult
|
|
|
|
@discardableResult
|
|
|
|
internal static func handleError(withStatusCode statusCode: UInt, json: JSON?, forSnode snode: Snode, associatedWith publicKey: String? = nil) -> Error? {
|
|
|
|
internal static func handleError(withStatusCode statusCode: UInt, json: JSON?, forSnode snode: Snode, associatedWith publicKey: String? = nil) -> Error? {
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
dispatchPrecondition(condition: .onQueue(Threading.workQueue))
|
|
|
|
|
|
|
|
#endif
|
|
|
|
func handleBadSnode() {
|
|
|
|
func handleBadSnode() {
|
|
|
|
let oldFailureCount = SnodeAPI.snodeFailureCount[snode] ?? 0
|
|
|
|
let oldFailureCount = SnodeAPI.snodeFailureCount[snode] ?? 0
|
|
|
|
let newFailureCount = oldFailureCount + 1
|
|
|
|
let newFailureCount = oldFailureCount + 1
|
|
|
|