From 9722f10a4323d92b9c6b954f48125ee90bb65d8a Mon Sep 17 00:00:00 2001 From: Nicolaas Burgers Date: Mon, 9 Sep 2019 18:21:30 +1000 Subject: [PATCH 1/3] Minimum set of changes that could enable Grasshopper files these to be opened: those which have embedded SpeckleApiClients pointing to a server which is currently unresponsive. --- ApiClient/SpeckleApiClientApiCalls.cs | 25 +++++++++++++++++++------ ApiClient/SpeckleApiClientExtension.cs | 4 ++++ ApiClient/SpeckleApiClientHead.cs | 13 +++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ApiClient/SpeckleApiClientApiCalls.cs b/ApiClient/SpeckleApiClientApiCalls.cs index 38d4692..5813010 100644 --- a/ApiClient/SpeckleApiClientApiCalls.cs +++ b/ApiClient/SpeckleApiClientApiCalls.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -324,7 +325,7 @@ public async System.Threading.Tasks.Task UserGetAsync( System.Thre urlBuilder_.Append( BaseUrl != null ? BaseUrl.TrimEnd( '/' ) : "" ).Append( "/accounts" ); var client_ = GetHttpClient(); - try + try { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) { @@ -394,7 +395,11 @@ public async System.Threading.Tasks.Task UserGetAsync( System.Thre } } } - finally + catch (TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); + } + finally { if ( client_ != null ) client_.Dispose(); @@ -2321,11 +2326,11 @@ public async System.Threading.Tasks.Task StreamGetAsync( string if ( query != null ) urlBuilder_.Append( query ); var client_ = GetHttpClient(); - try + try { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) { - request_.Method = new System.Net.Http.HttpMethod( "GET" ); + request_.Method = new System.Net.Http.HttpMethod( "GET" ); request_.Headers.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue( "application/json" ) ); PrepareRequest( client_, request_, urlBuilder_ ); @@ -2391,7 +2396,11 @@ public async System.Threading.Tasks.Task StreamGetAsync( string } } } - finally + catch (TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); + } + finally { if ( client_ != null ) client_.Dispose(); @@ -2688,6 +2697,10 @@ public async System.Threading.Tasks.Task StreamGetObjectsAsync( return default( ResponseObject ); } + catch(TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle server", (int) HttpStatusCode.RequestTimeout, null, null, null); + } finally { if ( response_ != null ) @@ -3412,7 +3425,7 @@ public async System.Threading.Tasks.Task ObjectGetBulkAsync( str if ( query != null ) urlBuilder_.Append( query ); - var client_ = GetHttpClient(); + var client_ = GetHttpClient(defaultBulkTimeoutMilliseconds); try { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) diff --git a/ApiClient/SpeckleApiClientExtension.cs b/ApiClient/SpeckleApiClientExtension.cs index 387e7cc..06b1115 100644 --- a/ApiClient/SpeckleApiClientExtension.cs +++ b/ApiClient/SpeckleApiClientExtension.cs @@ -55,6 +55,10 @@ public partial class SpeckleApiClient : ISerializable Timer IsReady, WsReconnecter; + //Default timeouts, pending further discussion + private double defaultTimeoutMilliseconds = 3000; + private double defaultBulkTimeoutMilliseconds = 60000; + private Dictionary ObjectCache = new Dictionary(); diff --git a/ApiClient/SpeckleApiClientHead.cs b/ApiClient/SpeckleApiClientHead.cs index 6d51e41..283eb08 100644 --- a/ApiClient/SpeckleApiClientHead.cs +++ b/ApiClient/SpeckleApiClientHead.cs @@ -41,11 +41,16 @@ partial void PrepareRequest( HttpClient client, HttpRequestMessage request, stri request.Content = new GzipContent( request.Content ); } - private HttpClient GetHttpClient( ) + private HttpClient GetHttpClient(double timeoutMillisecondsOverride = 0) { - var handler = new HttpClientHandler(); - handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip; - return new HttpClient( handler, true ); + var handler = new HttpClientHandler + { + AutomaticDecompression = System.Net.DecompressionMethods.GZip + }; + return new HttpClient(handler, true) + { + Timeout = TimeSpan.FromMilliseconds(timeoutMillisecondsOverride == 0 ? defaultTimeoutMilliseconds : timeoutMillisecondsOverride) + }; } } } From 1fad639282f00f9447014522251447b6e1fa5367 Mon Sep 17 00:00:00 2001 From: Nicolaas Burgers Date: Tue, 10 Sep 2019 13:02:40 +1000 Subject: [PATCH 2/3] Replaced tabs with spaces for new code indentations. --- ApiClient/SpeckleApiClientApiCalls.cs | 24 ++++++++++++------------ ApiClient/SpeckleApiClientExtension.cs | 6 +++--- ApiClient/SpeckleApiClientHead.cs | 18 +++++++++--------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ApiClient/SpeckleApiClientApiCalls.cs b/ApiClient/SpeckleApiClientApiCalls.cs index 5813010..e41d6f4 100644 --- a/ApiClient/SpeckleApiClientApiCalls.cs +++ b/ApiClient/SpeckleApiClientApiCalls.cs @@ -395,10 +395,10 @@ public async System.Threading.Tasks.Task UserGetAsync( System.Thre } } } - catch (TaskCanceledException) - { - throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); - } + catch (TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); + } finally { if ( client_ != null ) @@ -2396,10 +2396,10 @@ public async System.Threading.Tasks.Task StreamGetAsync( string } } } - catch (TaskCanceledException) - { - throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); - } + catch (TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle Server", (int)HttpStatusCode.RequestTimeout, null, null, null); + } finally { if ( client_ != null ) @@ -2697,10 +2697,10 @@ public async System.Threading.Tasks.Task StreamGetObjectsAsync( return default( ResponseObject ); } - catch(TaskCanceledException) - { - throw new SpeckleException("Connection timeout with Speckle server", (int) HttpStatusCode.RequestTimeout, null, null, null); - } + catch (TaskCanceledException) + { + throw new SpeckleException("Connection timeout with Speckle server", (int) HttpStatusCode.RequestTimeout, null, null, null); + } finally { if ( response_ != null ) diff --git a/ApiClient/SpeckleApiClientExtension.cs b/ApiClient/SpeckleApiClientExtension.cs index 06b1115..26b9fa0 100644 --- a/ApiClient/SpeckleApiClientExtension.cs +++ b/ApiClient/SpeckleApiClientExtension.cs @@ -55,9 +55,9 @@ public partial class SpeckleApiClient : ISerializable Timer IsReady, WsReconnecter; - //Default timeouts, pending further discussion - private double defaultTimeoutMilliseconds = 3000; - private double defaultBulkTimeoutMilliseconds = 60000; + //Default timeouts, pending further discussion + private double defaultTimeoutMilliseconds = 3000; + private double defaultBulkTimeoutMilliseconds = 60000; private Dictionary ObjectCache = new Dictionary(); diff --git a/ApiClient/SpeckleApiClientHead.cs b/ApiClient/SpeckleApiClientHead.cs index 283eb08..c3fad27 100644 --- a/ApiClient/SpeckleApiClientHead.cs +++ b/ApiClient/SpeckleApiClientHead.cs @@ -41,16 +41,16 @@ partial void PrepareRequest( HttpClient client, HttpRequestMessage request, stri request.Content = new GzipContent( request.Content ); } - private HttpClient GetHttpClient(double timeoutMillisecondsOverride = 0) + private HttpClient GetHttpClient(double timeoutMillisecondsOverride = 0) { - var handler = new HttpClientHandler - { - AutomaticDecompression = System.Net.DecompressionMethods.GZip - }; - return new HttpClient(handler, true) - { - Timeout = TimeSpan.FromMilliseconds(timeoutMillisecondsOverride == 0 ? defaultTimeoutMilliseconds : timeoutMillisecondsOverride) - }; + var handler = new HttpClientHandler + { + AutomaticDecompression = System.Net.DecompressionMethods.GZip + }; + return new HttpClient(handler, true) + { + Timeout = TimeSpan.FromMilliseconds(timeoutMillisecondsOverride == 0 ? defaultTimeoutMilliseconds : timeoutMillisecondsOverride) + }; } } } From fa869a3857fe46897f5dd666c431db37db7cd7c2 Mon Sep 17 00:00:00 2001 From: Nicolaas Burgers Date: Tue, 10 Sep 2019 17:21:36 +1000 Subject: [PATCH 3/3] Last few space/tab changes. --- ApiClient/SpeckleApiClientApiCalls.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ApiClient/SpeckleApiClientApiCalls.cs b/ApiClient/SpeckleApiClientApiCalls.cs index e41d6f4..80607d2 100644 --- a/ApiClient/SpeckleApiClientApiCalls.cs +++ b/ApiClient/SpeckleApiClientApiCalls.cs @@ -325,7 +325,7 @@ public async System.Threading.Tasks.Task UserGetAsync( System.Thre urlBuilder_.Append( BaseUrl != null ? BaseUrl.TrimEnd( '/' ) : "" ).Append( "/accounts" ); var client_ = GetHttpClient(); - try + try { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) { @@ -2326,11 +2326,11 @@ public async System.Threading.Tasks.Task StreamGetAsync( string if ( query != null ) urlBuilder_.Append( query ); var client_ = GetHttpClient(); - try + try { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) { - request_.Method = new System.Net.Http.HttpMethod( "GET" ); + request_.Method = new System.Net.Http.HttpMethod( "GET" ); request_.Headers.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue( "application/json" ) ); PrepareRequest( client_, request_, urlBuilder_ );