Skip to content

Commit 1bc9079

Browse files
committed
2 parents b46908e + fc4b60e commit 1bc9079

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,4 @@ MigrationBackup/
350350
.ionide/
351351

352352

353+
/.idea

Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials)
2323

2424
LWAAuthorizationCredentials = lwaAuthorizationCredentials;
2525
LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder();
26-
RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
26+
// RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
27+
RestClient =
28+
RestClientSingleton.GetRestClient(
29+
LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
2730
}
2831

2932

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Net.Http;
3+
using RestSharp;
4+
using RestSharp.Serializers.NewtonsoftJson;
5+
6+
namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime
7+
{
8+
/// <summary>
9+
/// Get Singleton HttpClient
10+
/// to fixed: RestClient not properly disposed causes memory / socket leaks
11+
/// <see cref="https://github.com/abuzuhri/Amazon-SP-API-CSharp/issues/523"/>
12+
/// </summary>
13+
public static class RestClientSingleton
14+
{
15+
private static readonly HttpClient _httpClient = null;
16+
17+
static RestClientSingleton()
18+
{
19+
_httpClient = new HttpClient(new StandardSocketsHttpHandler
20+
{
21+
PooledConnectionLifetime = TimeSpan.FromMinutes(1)
22+
});
23+
}
24+
25+
/// <summary>
26+
/// Get RestClient By Singleton HttpClient
27+
/// </summary>
28+
/// <param name="baseUrl"></param>
29+
/// <returns></returns>
30+
/// <exception cref="ArgumentException"></exception>
31+
public static RestClient GetRestClient(string baseUrl)
32+
{
33+
if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out var baseUri))
34+
{
35+
throw new ArgumentException($"argument of {baseUrl} is illegal");
36+
}
37+
38+
var restClientOption = new RestClientOptions
39+
{
40+
BaseUrl = baseUri,
41+
};
42+
return new RestClient(_httpClient, restClientOption).UseNewtonsoftJson();
43+
}
44+
}
45+
}

Source/FikaAmazonAPI/FikaAmazonAPI.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.100.52" />
3939
<PackageReference Include="AWSSDK.SQS" Version="3.7.100.52" />
4040
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
41+
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
4142
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
4243
<PackageReference Include="RestSharp" Version="108.0.3" />
4344
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="108.0.3" />
45+
<PackageReference Include="StandardSocketsHttpHandler" Version="2.2.0.4" />
4446
<PackageReference Include="System.Collections" Version="4.3.0" />
4547
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
4648
<PackageReference Include="System.Reflection" Version="4.3.0" />

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public RequestService(AmazonCredential amazonCredential)
5656

5757
private void CreateRequest(string url, RestSharp.Method method)
5858
{
59-
RequestClient = new RestClient(ApiBaseUrl);
60-
RequestClient.UseNewtonsoftJson();
59+
RequestClient = RestClientSingleton.GetRestClient(ApiBaseUrl);
60+
// RequestClient = new RestClient(ApiBaseUrl);
61+
// RequestClient.UseNewtonsoftJson();
6162
Request = new RestRequest(url, method);
6263
}
6364

0 commit comments

Comments
 (0)