Skip to content

Commit

Permalink
enqueue restarts in a serial queue
Browse files Browse the repository at this point in the history
  • Loading branch information
codifilo committed Oct 28, 2022
1 parent 5f688ec commit e8a8b91
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Sources/SwiftGraphQL/HTTP+WebSockets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class GraphQLSocket<S: GraphQLEnabledSocket> {
case notRunning, started, running
}

private let restartQueue = DispatchQueue(label: "GraphQLSocketRestartQueue")
private var socket: S?
private var initParams: S.InitParamaters
private var autoConnect: Bool
Expand Down Expand Up @@ -169,12 +170,12 @@ public class GraphQLSocket<S: GraphQLEnabledSocket> {
}
if state == .notRunning {
// Try again while we're restarting
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 3.0) { [weak self] in
restartQueue.asyncAfter(deadline: .now() + 3.0) { [weak self] in
self?.detachedPingQueue(interval: interval, errorHandler: errorHandler, pingHandler: pingHandler, token: token)
}
return
}
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + interval) { [weak self] in
restartQueue.asyncAfter(deadline: .now() + interval) { [weak self] in
guard let self = self else {
return
}
Expand Down Expand Up @@ -300,7 +301,7 @@ public class GraphQLSocket<S: GraphQLEnabledSocket> {
public func restart(errorHandler: @escaping (SubscribeError) -> Void) {
self.state = .notRunning
let params = lastConnectionParams
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 3.0) { [weak self] in
restartQueue.asyncAfter(deadline: .now() + 3.0) { [weak self] in
self?.start(connectionParams: params, errorHandler: errorHandler)
}
}
Expand Down

0 comments on commit e8a8b91

Please sign in to comment.