Skip to content

Commit 5ffbbe0

Browse files
dev_chenjiawenJ-W-Chan
authored andcommitted
add RestClientSingleton
1 parent c10d717 commit 5ffbbe0

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
using System;
22
using System.Net.Http;
33
using RestSharp;
4+
using RestSharp.Serializers.NewtonsoftJson;
45

56
namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime
67
{
7-
public class RestClientSingleton
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
814
{
9-
private static readonly HttpClient _httpClient = new HttpClient(new StandardSocketsHttpHandler
10-
{
11-
PooledConnectionLifetime = TimeSpan.FromMinutes(15)
12-
});
15+
private static readonly HttpClient _httpClient = null;
1316

1417
static RestClientSingleton()
1518
{
19+
_httpClient = new HttpClient(new StandardSocketsHttpHandler
20+
{
21+
PooledConnectionLifetime = TimeSpan.FromMinutes(1)
22+
});
1623
}
1724

18-
private RestClientSingleton()
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)
1932
{
20-
}
33+
if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out var baseUri))
34+
{
35+
throw new ArgumentException($"argument of {baseUrl} is illegal");
36+
}
2137

22-
public static RestClient RestClient => new RestClient(_httpClient);
38+
var restClientOption = new RestClientOptions
39+
{
40+
BaseUrl = baseUri,
41+
};
42+
return new RestClient(_httpClient, restClientOption).UseNewtonsoftJson();
43+
}
2344
}
2445
}

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)