Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanSchoenfeld committed Oct 31, 2024
1 parent da3d9da commit b45ab0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Src/Notion.Client/RestClient/IRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand Down
26 changes: 10 additions & 16 deletions Src/Notion.Client/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class RestClient : IRestClient
ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }
};

private HttpClient _httpClient;

public RestClient(ClientOptions options)
{
_options = MergeOptions(options);
Expand All @@ -36,7 +34,7 @@ public async Task<T> GetAsync<T>(
JsonSerializerSettings serializerSettings = null,
CancellationToken cancellationToken = default)
{
var response = await SendAsync(uri, HttpMethod.Get, queryParams, headers,
using var response = await SendAsync(uri, HttpMethod.Get, queryParams, headers,
cancellationToken: cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -56,7 +54,7 @@ void AttachContent(HttpRequestMessage httpRequest)
Encoding.UTF8, "application/json");
}

var response = await SendAsync(uri, HttpMethod.Post, queryParams, headers, AttachContent,
using var response = await SendAsync(uri, HttpMethod.Post, queryParams, headers, AttachContent,
cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -76,7 +74,7 @@ void AttachContent(HttpRequestMessage httpRequest)
httpRequest.Content = new StringContent(serializedBody, Encoding.UTF8, "application/json");
}

var response = await SendAsync(uri, new HttpMethod("PATCH"), queryParams, headers, AttachContent,
using var response = await SendAsync(uri, new HttpMethod("PATCH"), queryParams, headers, AttachContent,
cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -88,7 +86,7 @@ public async Task DeleteAsync(
IDictionary<string, string> headers = null,
CancellationToken cancellationToken = default)
{
await SendAsync(uri, HttpMethod.Delete, queryParams, headers, null, cancellationToken);
using var _ = await SendAsync(uri, HttpMethod.Delete, queryParams, headers, null, cancellationToken);
}

private static ClientOptions MergeOptions(ClientOptions options)
Expand Down Expand Up @@ -141,7 +139,7 @@ private async Task<HttpResponseMessage> SendAsync(
Action<HttpRequestMessage> attachContent = null,
CancellationToken cancellationToken = default)
{
EnsureHttpClient();
using var client = GetHttpClient();

requestUri = AddQueryString(requestUri, queryParams);

Expand All @@ -156,7 +154,7 @@ private async Task<HttpResponseMessage> SendAsync(

attachContent?.Invoke(httpRequest);

var response = await _httpClient.SendAsync(httpRequest, cancellationToken);
var response = await client.SendAsync(httpRequest, cancellationToken);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -174,17 +172,13 @@ private static void AddHeaders(HttpRequestMessage request, IDictionary<string, s
}
}

private void EnsureHttpClient()
private HttpClient GetHttpClient()
{
if (_httpClient != null)
{
return;
}

var pipeline = new LoggingHandler { InnerHandler = new HttpClientHandler() };

_httpClient = new HttpClient(pipeline);
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
var httpClient = new HttpClient(pipeline);
httpClient.BaseAddress = new Uri(_options.BaseUrl);
return httpClient;
}

private static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, string>> queryParams)
Expand Down

0 comments on commit b45ab0e

Please sign in to comment.