Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abuzuhri committed Mar 24, 2023
2 parents b46908e + fc4b60e commit 1bc9079
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,4 @@ MigrationBackup/
.ionide/


/.idea
5 changes: 4 additions & 1 deletion Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
45 changes: 45 additions & 0 deletions Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/RestClientSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Net.Http;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime
{
/// <summary>
/// Get Singleton HttpClient
/// to fixed: RestClient not properly disposed causes memory / socket leaks
/// <see cref="https://github.com/abuzuhri/Amazon-SP-API-CSharp/issues/523"/>
/// </summary>
public static class RestClientSingleton
{
private static readonly HttpClient _httpClient = null;

static RestClientSingleton()
{
_httpClient = new HttpClient(new StandardSocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(1)
});
}

/// <summary>
/// Get RestClient By Singleton HttpClient
/// </summary>
/// <param name="baseUrl"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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();
}
}
}
2 changes: 2 additions & 0 deletions Source/FikaAmazonAPI/FikaAmazonAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.100.52" />
<PackageReference Include="AWSSDK.SQS" Version="3.7.100.52" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="108.0.3" />
<PackageReference Include="StandardSocketsHttpHandler" Version="2.2.0.4" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Reflection" Version="4.3.0" />
Expand Down
5 changes: 3 additions & 2 deletions Source/FikaAmazonAPI/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 1bc9079

Please sign in to comment.