فهرست منبع

Properly handle OperationCanceledException when creating client

I've heard of this being thrown in some cases of obscure timeout.

Relates to: #420
Antony Male 7 سال پیش
والد
کامیت
7b03b58121

+ 2 - 2
src/SyncTrayzor/Syncthing/ApiClient/ISyncthingApi.cs

@@ -26,13 +26,13 @@ namespace SyncTrayzor.Syncthing.ApiClient
         Task ScanAsync(string folder, string sub);
 
         [Get("/rest/system/status")]
-        Task<SystemInfo> FetchSystemInfoAsync();
+        Task<SystemInfo> FetchSystemInfoAsync(CancellationToken cancellationToken);
 
         [Get("/rest/system/connections")]
         Task<Connections> FetchConnectionsAsync(CancellationToken cancellationToken);
 
         [Get("/rest/system/version")]
-        Task<SyncthingVersion> FetchVersionAsync();
+        Task<SyncthingVersion> FetchVersionAsync(CancellationToken cancellationToken);
 
         [Post("/rest/system/restart")]
         Task RestartAsync();

+ 2 - 2
src/SyncTrayzor/Syncthing/ApiClient/ISyncthingApiClient.cs

@@ -11,9 +11,9 @@ namespace SyncTrayzor.Syncthing.ApiClient
         Task<List<Event>> FetchEventsAsync(int since, CancellationToken cancellationToken);
         Task<Config> FetchConfigAsync();
         Task ScanAsync(string folderId, string subPath);
-        Task<SystemInfo> FetchSystemInfoAsync();
+        Task<SystemInfo> FetchSystemInfoAsync(CancellationToken cancellationToken);
         Task<Connections> FetchConnectionsAsync(CancellationToken cancellationToken);
-        Task<SyncthingVersion> FetchVersionAsync();
+        Task<SyncthingVersion> FetchVersionAsync(CancellationToken cancellationToken);
         Task RestartAsync();
         Task<FolderStatus> FetchFolderStatusAsync(string folderId, CancellationToken cancellationToken);
         Task<DebugFacilitiesSettings> FetchDebugFacilitiesAsync();

+ 4 - 4
src/SyncTrayzor/Syncthing/ApiClient/SyncthingApiClient.cs

@@ -61,9 +61,9 @@ namespace SyncTrayzor.Syncthing.ApiClient
             return this.api.ScanAsync(folderId, subPath);
         }
 
-        public async Task<SystemInfo> FetchSystemInfoAsync()
+        public async Task<SystemInfo> FetchSystemInfoAsync(CancellationToken cancellationToken)
         {
-            var systemInfo = await this.api.FetchSystemInfoAsync();
+            var systemInfo = await this.api.FetchSystemInfoAsync(cancellationToken);
             logger.Debug("Fetched system info: {0}", systemInfo);
             return systemInfo;
         }
@@ -74,9 +74,9 @@ namespace SyncTrayzor.Syncthing.ApiClient
             return connections;
         }
 
-        public async Task<SyncthingVersion> FetchVersionAsync()
+        public async Task<SyncthingVersion> FetchVersionAsync(CancellationToken cancellationToken)
         {
-            var version = await this.api.FetchVersionAsync();
+            var version = await this.api.FetchVersionAsync(cancellationToken);
             logger.Debug("Fetched version: {0}", version);
             return version;
         }

+ 5 - 1
src/SyncTrayzor/Syncthing/ApiClient/SyncthingApiClientFactory.cs

@@ -32,11 +32,15 @@ namespace SyncTrayzor.Syncthing.ApiClient
                 try
                 {
                     logger.Debug("Attempting to request API");
-                    await client.FetchVersionAsync();
+                    await client.FetchVersionAsync(cancellationToken);
                     success = true;
                     logger.Debug("Success!");
                     break;
                 }
+                catch (OperationCanceledException e) when (e.CancellationToken != cancellationToken)
+                {
+                    logger.Debug("Failed to connect (cancelled) on attempt {0}", retryCount);
+                }
                 catch (HttpRequestException e)
                 {
                     logger.Debug("Failed to connect on attempt {0}", retryCount);

+ 2 - 2
src/SyncTrayzor/Syncthing/SyncthingManager.cs

@@ -469,8 +469,8 @@ namespace SyncTrayzor.Syncthing
             var apiClient = this.apiClient.Value;
             cancellationToken.ThrowIfCancellationRequested();
 
-            var syncthingVersionTask = apiClient.FetchVersionAsync();
-            var systemInfoTask = apiClient.FetchSystemInfoAsync();
+            var syncthingVersionTask = apiClient.FetchVersionAsync(cancellationToken);
+            var systemInfoTask = apiClient.FetchSystemInfoAsync(cancellationToken);
 
             await Task.WhenAll(syncthingVersionTask, systemInfoTask);