From d901463ffb9fdd92bbfc5b4d4c11b9ade31f9de8 Mon Sep 17 00:00:00 2001 From: yell0wsuit <5692900+yell0wsuit@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:52:55 +0700 Subject: [PATCH] Refactor part 1 Cache httpClient and remove ConstructHttpClient() --- src/Ryujinx/Modules/Updater/Updater.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Ryujinx/Modules/Updater/Updater.cs b/src/Ryujinx/Modules/Updater/Updater.cs index 9f186f2b3..e5eca0a7a 100644 --- a/src/Ryujinx/Modules/Updater/Updater.cs +++ b/src/Ryujinx/Modules/Updater/Updater.cs @@ -95,10 +95,9 @@ namespace Ryujinx.Modules // Get latest version number from GitHub API try { - using HttpClient jsonClient = ConstructHttpClient(); string buildInfoUrl = $"{GitHubApiUrl}/repos/{ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelRepo}/releases/latest"; - string fetchedJson = await jsonClient.GetStringAsync(buildInfoUrl); + string fetchedJson = await httpClient.GetStringAsync(buildInfoUrl); var fetched = JsonHelper.Deserialize(fetchedJson, _serializerContext.GithubReleasesJsonResponse); _buildVer = fetched.Name; @@ -185,12 +184,11 @@ namespace Ryujinx.Modules } // Fetch build size information to learn chunk sizes. - using HttpClient buildSizeClient = ConstructHttpClient(); try { - buildSizeClient.DefaultRequestHeaders.Add("Range", "bytes=0-0"); + httpClient.DefaultRequestHeaders.Add("Range", "bytes=0-0"); - HttpResponseMessage message = await buildSizeClient.GetAsync(new Uri(_buildUrl), HttpCompletionOption.ResponseHeadersRead); + HttpResponseMessage message = await httpClient.GetAsync(new Uri(_buildUrl), HttpCompletionOption.ResponseHeadersRead); _buildSize = message.Content.Headers.ContentRange.Length.Value; } @@ -221,6 +219,15 @@ namespace Ryujinx.Modules }); } + private static readonly HttpClient httpClient = new HttpClient + { + // Required by GitHub to interact with APIs. + DefaultRequestHeaders = + { + { "User-Agent", "Ryujinx-Updater/1.0.0" } + } + }; + private static HttpClient ConstructHttpClient() { HttpClient result = new();