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/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 new file mode 100644 index 00000000..9d87ab76 --- /dev/null +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs @@ -0,0 +1,45 @@ +using System; +using System.Net.Http; +using RestSharp; +using RestSharp.Serializers.NewtonsoftJson; + +namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime +{ + /// + /// Get Singleton HttpClient + /// to fixed: RestClient not properly disposed causes memory / socket leaks + /// + /// + public static class RestClientSingleton + { + private static readonly HttpClient _httpClient = null; + + static RestClientSingleton() + { + _httpClient = new HttpClient(new StandardSocketsHttpHandler + { + PooledConnectionLifetime = TimeSpan.FromMinutes(1) + }); + } + + /// + /// 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"); + } + + var restClientOption = new RestClientOptions + { + BaseUrl = baseUri, + }; + return new RestClient(_httpClient, restClientOption).UseNewtonsoftJson(); + } + } +} \ No newline at end of file diff --git a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj index 9594c2d9..1cc73c41 100644 --- a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj +++ b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj @@ -38,9 +38,11 @@ + + 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); }