-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate retryability for outgoing mutation queue
- Loading branch information
Showing
19 changed files
with
2,018 additions
and
1,461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright 2018-2019 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
extension DispatchSource { | ||
/// Convenience function to encapsulate creation of a one-off DispatchSourceTimer for different versions of Swift | ||
/// | ||
/// - Parameters: | ||
/// - interval: The future DispatchInterval at which to fire the timer | ||
/// - queue: The queue on which the timer should perform its block | ||
/// - block: The block to invoke when the timer is fired | ||
/// - Returns: The unstarted timer | ||
public static func makeOneOffDispatchSourceTimer(interval: DispatchTimeInterval, | ||
queue: DispatchQueue, | ||
block: @escaping () -> Void ) -> DispatchSourceTimer { | ||
let deadline = DispatchTime.now() + interval | ||
return makeOneOffDispatchSourceTimer(deadline: deadline, queue: queue, block: block) | ||
} | ||
|
||
/// Convenience function to encapsulate creation of a one-off DispatchSourceTimer for different versions of Swift | ||
/// - Parameters: | ||
/// - deadline: The time to fire the timer | ||
/// - queue: The queue on which the timer should perform its block | ||
/// - block: The block to invoke when the timer is fired | ||
public static func makeOneOffDispatchSourceTimer(deadline: DispatchTime, | ||
queue: DispatchQueue, | ||
block: @escaping () -> Void ) -> DispatchSourceTimer { | ||
let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: 0), queue: queue) | ||
#if swift(>=4) | ||
timer.schedule(deadline: deadline) | ||
#else | ||
timer.scheduleOneshot(deadline: deadline) | ||
#endif | ||
timer.setEventHandler(handler: block) | ||
return timer | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...taStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/MutationRetryNotifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// Copyright 2018-2019 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import Foundation | ||
import AWSPluginsCore | ||
import Combine | ||
|
||
@available(iOS 13.0, *) | ||
final class MutationRetryNotifier { | ||
private var nextSyncTimer: DispatchSourceTimer? | ||
private var handlerQueue = DispatchQueue.global(qos: .default) | ||
var retryMutationCallback: () -> Void | ||
private var reachabilitySubscription: Subscription? | ||
|
||
init(advice: RequestRetryAdvice, | ||
networkReachabilityPublisher: AnyPublisher<ReachabilityUpdate, DataStoreError>?, | ||
retryMutationCallback: @escaping BasicClosure) { | ||
self.retryMutationCallback = retryMutationCallback | ||
|
||
networkReachabilityPublisher?.subscribe(self) | ||
|
||
let deadline = DispatchTime.now() + advice.retryInterval | ||
scheduleTimer(at: deadline) | ||
} | ||
|
||
deinit { | ||
cancel() | ||
} | ||
|
||
private func scheduleTimer(at deadline: DispatchTime) { | ||
nextSyncTimer = DispatchSource.makeOneOffDispatchSourceTimer(deadline: deadline, queue: handlerQueue) { | ||
self.notifyCallback() | ||
} | ||
nextSyncTimer?.resume() | ||
} | ||
|
||
func cancel() { | ||
reachabilitySubscription?.cancel() | ||
nextSyncTimer?.cancel() | ||
} | ||
|
||
func notifyCallback() { | ||
// Call the cancel routine as the purpose of retry is fulfilled | ||
cancel() | ||
retryMutationCallback() | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MutationRetryNotifier: Subscriber { | ||
func receive(subscription: Subscription) { | ||
log.verbose(#function) | ||
reachabilitySubscription = subscription | ||
subscription.request(.unlimited) | ||
} | ||
|
||
func receive(_ reachabilityUpdate: ReachabilityUpdate) -> Subscribers.Demand { | ||
if reachabilityUpdate.isOnline { | ||
notifyCallback() | ||
return .none | ||
} | ||
return .unlimited | ||
} | ||
|
||
func receive(completion: Subscribers.Completion<DataStoreError>) { | ||
log.verbose(#function) | ||
reachabilitySubscription?.cancel() | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MutationRetryNotifier: DefaultLogger { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.