From b45ab0ef8a1689899abba14bb8aaccb87b9e5058 Mon Sep 17 00:00:00 2001 From: Herman Schoenfeld Date: Thu, 31 Oct 2024 15:39:10 +1000 Subject: [PATCH] Fix memory leaks --- Src/Notion.Client/RestClient/IRestClient.cs | 3 ++- Src/Notion.Client/RestClient/RestClient.cs | 26 ++++++++------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Src/Notion.Client/RestClient/IRestClient.cs b/Src/Notion.Client/RestClient/IRestClient.cs index b8ffe5b..62d81c6 100644 --- a/Src/Notion.Client/RestClient/IRestClient.cs +++ b/Src/Notion.Client/RestClient/IRestClient.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; diff --git a/Src/Notion.Client/RestClient/RestClient.cs b/Src/Notion.Client/RestClient/RestClient.cs index 59a848f..1874dd7 100644 --- a/Src/Notion.Client/RestClient/RestClient.cs +++ b/Src/Notion.Client/RestClient/RestClient.cs @@ -22,8 +22,6 @@ public class RestClient : IRestClient ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }; - private HttpClient _httpClient; - public RestClient(ClientOptions options) { _options = MergeOptions(options); @@ -36,7 +34,7 @@ public async Task GetAsync( 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(serializerSettings); @@ -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(serializerSettings); @@ -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(serializerSettings); @@ -88,7 +86,7 @@ public async Task DeleteAsync( IDictionary 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) @@ -141,7 +139,7 @@ private async Task SendAsync( Action attachContent = null, CancellationToken cancellationToken = default) { - EnsureHttpClient(); + using var client = GetHttpClient(); requestUri = AddQueryString(requestUri, queryParams); @@ -156,7 +154,7 @@ private async Task SendAsync( attachContent?.Invoke(httpRequest); - var response = await _httpClient.SendAsync(httpRequest, cancellationToken); + var response = await client.SendAsync(httpRequest, cancellationToken); if (!response.IsSuccessStatusCode) { @@ -174,17 +172,13 @@ private static void AddHeaders(HttpRequestMessage request, IDictionary> queryParams)