Retry authentication if connection timed out
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 30 Sep 2024 05:52:18 +0000 (13:52 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 11 Oct 2024 08:40:47 +0000 (16:40 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift

index 7d439245d66ceb89ebd79132a5d6b4037b8cd570..e1bf70541dfc0eccaccd5d9eee2450062ec69b22 100644 (file)
@@ -19,6 +19,10 @@ import NextcloudKit
 import NextcloudFileProviderKit
 import OSLog
 
+let AuthenticationTimeouts: [UInt64] = [ // Have progressively longer timeouts to not hammer server
+    3_000_000_000, 6_000_000_000, 30_000_000_000, 60_000_000_000, 120_000_000_000, 300_000_000_000
+]
+
 extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInterface {
     /*
      This FileProviderExtension extension contains everything needed to communicate with the client.
@@ -120,7 +124,17 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte
         ncKit.setup(delegate: changeObserver)
 
         Task {
-            switch (await ncKit.tryAuthenticationAttempt()) {
+            var authAttemptState = AuthenticationAttemptResultState.connectionError // default
+            for authTimeout in AuthenticationTimeouts { // Retry if we have a connection issue
+                authAttemptState = await ncKit.tryAuthenticationAttempt()
+                guard authAttemptState == .connectionError else { break }
+                Logger.fileProviderExtension.info(
+                    "\(user, privacy: .public) authentication try timed out. Trying again soon."
+                )
+                try? await Task.sleep(nanoseconds: authTimeout)
+            }
+
+            switch (authAttemptState) {
             case .authenticationError:
                 Logger.fileProviderExtension.info(
                     "\(user, privacy: .public) authentication failed due to bad creds, stopping"