From 1144b2605c382ea873bd3fbc8aa32d26969431b3 Mon Sep 17 00:00:00 2001 From: poseur Date: Mon, 13 Mar 2023 00:22:37 +0800 Subject: [PATCH 1/4] add RestClientSingleton --- .gitignore | 1 + .../Runtime/RestClientSingleton.cs | 24 +++++++++++++++++++ Source/FikaAmazonAPI/FikaAmazonAPI.csproj | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs diff --git a/.gitignore b/.gitignore index 0c74c2a7..62a1fd09 100644 --- a/.gitignore +++ b/.gitignore @@ -350,3 +350,4 @@ MigrationBackup/ .ionide/ +/.idea diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs new file mode 100644 index 00000000..5c8ca289 --- /dev/null +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs @@ -0,0 +1,24 @@ +using System; +using System.Net.Http; +using RestSharp; + +namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime +{ + public class RestClientSingleton + { + private static readonly HttpClient _httpClient = new HttpClient(new StandardSocketsHttpHandler + { + PooledConnectionLifetime = TimeSpan.FromMinutes(15) + }); + + static RestClientSingleton() + { + } + + private RestClientSingleton() + { + } + + public static RestClient RestClient => new RestClient(_httpClient); + } +} \ No newline at end of file diff --git a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj index 19268e09..01a79cbb 100644 --- a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj +++ b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj @@ -38,9 +38,11 @@ + + From dfdf1ee1e876dc9df3345018940d14a4a9b76d0e Mon Sep 17 00:00:00 2001 From: dev_chenjiawen Date: Mon, 13 Mar 2023 15:16:50 +0800 Subject: [PATCH 2/4] add RestClientSingleton --- .../AmazonSpApiSDK/Runtime/LWAClient.cs | 5 ++- .../Runtime/RestClientSingleton.cs | 37 +++++++++++++++---- .../FikaAmazonAPI/Services/RequestService.cs | 5 ++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs index 5244edf4..e1d11933 100644 --- a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs @@ -23,7 +23,10 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials) LWAAuthorizationCredentials = lwaAuthorizationCredentials; LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder(); - RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); + // RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); + RestClient = + RestClientSingleton.GetRestClient( + LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); } diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs index 5c8ca289..9d87ab76 100644 --- a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs @@ -1,24 +1,45 @@ using System; using System.Net.Http; using RestSharp; +using RestSharp.Serializers.NewtonsoftJson; namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime { - public class RestClientSingleton + /// + /// Get Singleton HttpClient + /// to fixed: RestClient not properly disposed causes memory / socket leaks + /// + /// + public static class RestClientSingleton { - private static readonly HttpClient _httpClient = new HttpClient(new StandardSocketsHttpHandler - { - PooledConnectionLifetime = TimeSpan.FromMinutes(15) - }); + private static readonly HttpClient _httpClient = null; static RestClientSingleton() { + _httpClient = new HttpClient(new StandardSocketsHttpHandler + { + PooledConnectionLifetime = TimeSpan.FromMinutes(1) + }); } - private RestClientSingleton() + /// + /// Get RestClient By Singleton HttpClient + /// + /// + /// + /// + public static RestClient GetRestClient(string baseUrl) { - } + if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out var baseUri)) + { + throw new ArgumentException($"argument of {baseUrl} is illegal"); + } - public static RestClient RestClient => new RestClient(_httpClient); + var restClientOption = new RestClientOptions + { + BaseUrl = baseUri, + }; + return new RestClient(_httpClient, restClientOption).UseNewtonsoftJson(); + } } } \ No newline at end of file diff --git a/Source/FikaAmazonAPI/Services/RequestService.cs b/Source/FikaAmazonAPI/Services/RequestService.cs index 904bb5bf..4188c9d9 100644 --- a/Source/FikaAmazonAPI/Services/RequestService.cs +++ b/Source/FikaAmazonAPI/Services/RequestService.cs @@ -56,8 +56,9 @@ public RequestService(AmazonCredential amazonCredential) private void CreateRequest(string url, RestSharp.Method method) { - RequestClient = new RestClient(ApiBaseUrl); - RequestClient.UseNewtonsoftJson(); + RequestClient = RestClientSingleton.GetRestClient(ApiBaseUrl); + // RequestClient = new RestClient(ApiBaseUrl); + // RequestClient.UseNewtonsoftJson(); Request = new RestRequest(url, method); } From c10d717bd9312def0a5f976959bdc763da25a6a0 Mon Sep 17 00:00:00 2001 From: poseur Date: Mon, 13 Mar 2023 00:22:37 +0800 Subject: [PATCH 3/4] add RestClientSingleton --- .gitignore | 1 + .../Runtime/RestClientSingleton.cs | 24 +++++++++++++++++++ Source/FikaAmazonAPI/FikaAmazonAPI.csproj | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs diff --git a/.gitignore b/.gitignore index 0c74c2a7..62a1fd09 100644 --- a/.gitignore +++ b/.gitignore @@ -350,3 +350,4 @@ MigrationBackup/ .ionide/ +/.idea diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs new file mode 100644 index 00000000..5c8ca289 --- /dev/null +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs @@ -0,0 +1,24 @@ +using System; +using System.Net.Http; +using RestSharp; + +namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime +{ + public class RestClientSingleton + { + private static readonly HttpClient _httpClient = new HttpClient(new StandardSocketsHttpHandler + { + PooledConnectionLifetime = TimeSpan.FromMinutes(15) + }); + + static RestClientSingleton() + { + } + + private RestClientSingleton() + { + } + + public static RestClient RestClient => new RestClient(_httpClient); + } +} \ No newline at end of file diff --git a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj index 9c12df3e..eecab75a 100644 --- a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj +++ b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj @@ -38,9 +38,11 @@ + + From 5ffbbe07639a3b4e3d05b652bc9d6a5ee8141aa3 Mon Sep 17 00:00:00 2001 From: dev_chenjiawen Date: Mon, 13 Mar 2023 15:16:50 +0800 Subject: [PATCH 4/4] add RestClientSingleton --- .../AmazonSpApiSDK/Runtime/LWAClient.cs | 5 ++- .../Runtime/RestClientSingleton.cs | 37 +++++++++++++++---- .../FikaAmazonAPI/Services/RequestService.cs | 5 ++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs index 5244edf4..e1d11933 100644 --- a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs @@ -23,7 +23,10 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials) LWAAuthorizationCredentials = lwaAuthorizationCredentials; LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder(); - RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); + // RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); + RestClient = + RestClientSingleton.GetRestClient( + LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); } diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs index 5c8ca289..9d87ab76 100644 --- a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs @@ -1,24 +1,45 @@ using System; using System.Net.Http; using RestSharp; +using RestSharp.Serializers.NewtonsoftJson; namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime { - public class RestClientSingleton + /// + /// Get Singleton HttpClient + /// to fixed: RestClient not properly disposed causes memory / socket leaks + /// + /// + public static class RestClientSingleton { - private static readonly HttpClient _httpClient = new HttpClient(new StandardSocketsHttpHandler - { - PooledConnectionLifetime = TimeSpan.FromMinutes(15) - }); + private static readonly HttpClient _httpClient = null; static RestClientSingleton() { + _httpClient = new HttpClient(new StandardSocketsHttpHandler + { + PooledConnectionLifetime = TimeSpan.FromMinutes(1) + }); } - private RestClientSingleton() + /// + /// Get RestClient By Singleton HttpClient + /// + /// + /// + /// + public static RestClient GetRestClient(string baseUrl) { - } + if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out var baseUri)) + { + throw new ArgumentException($"argument of {baseUrl} is illegal"); + } - public static RestClient RestClient => new RestClient(_httpClient); + var restClientOption = new RestClientOptions + { + BaseUrl = baseUri, + }; + return new RestClient(_httpClient, restClientOption).UseNewtonsoftJson(); + } } } \ No newline at end of file diff --git a/Source/FikaAmazonAPI/Services/RequestService.cs b/Source/FikaAmazonAPI/Services/RequestService.cs index 904bb5bf..4188c9d9 100644 --- a/Source/FikaAmazonAPI/Services/RequestService.cs +++ b/Source/FikaAmazonAPI/Services/RequestService.cs @@ -56,8 +56,9 @@ public RequestService(AmazonCredential amazonCredential) private void CreateRequest(string url, RestSharp.Method method) { - RequestClient = new RestClient(ApiBaseUrl); - RequestClient.UseNewtonsoftJson(); + RequestClient = RestClientSingleton.GetRestClient(ApiBaseUrl); + // RequestClient = new RestClient(ApiBaseUrl); + // RequestClient.UseNewtonsoftJson(); Request = new RestRequest(url, method); }