From fa8418aedbb5d4c97003575a732c8f7c6e5e3730 Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Sun, 25 Jan 2026 01:41:09 +0300 Subject: [PATCH] =?UTF-8?q?perf:=20=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8?= =?UTF-8?q?=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D1=83=20=D1=81=20Modeus=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Устранил искусственную задержку и принудительный сбор мусора в сервисе сотрудников, повысив его отзывчивость. Перевел HTTP-запросы на использование `JsonContent.Create` и асинхронную десериализацию из потока, что значительно улучшило эффективность обработки JSON. --- .../Services/ModeusEmployeeService.cs | 4 ---- SfeduSchedule/Services/ModeusHttpClient.cs | 21 ++++++++----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/SfeduSchedule/Services/ModeusEmployeeService.cs b/SfeduSchedule/Services/ModeusEmployeeService.cs index 0505847..cc8888f 100644 --- a/SfeduSchedule/Services/ModeusEmployeeService.cs +++ b/SfeduSchedule/Services/ModeusEmployeeService.cs @@ -36,10 +36,6 @@ public class ModeusEmployeeService(ILogger logger, Modeus _employees = employees; logger.LogInformationHere($"Получено {employees.Count} сотрудников из Modeus."); } - - await Task.Delay(TimeSpan.FromSeconds(5), _cts.Token); - GC.Collect(); - GC.WaitForPendingFinalizers(); } catch (OperationCanceledException) { diff --git a/SfeduSchedule/Services/ModeusHttpClient.cs b/SfeduSchedule/Services/ModeusHttpClient.cs index 557ec0e..6860b3f 100644 --- a/SfeduSchedule/Services/ModeusHttpClient.cs +++ b/SfeduSchedule/Services/ModeusHttpClient.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -using System.Text; using System.Text.Json; +using System.Net.Http.Json; using Microsoft.Net.Http.Headers; using ModeusSchedule.Abstractions; using ModeusSchedule.Abstractions.DTO; @@ -40,8 +40,7 @@ public class ModeusHttpClient { using var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}"); - request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions), - Encoding.UTF8, "application/json"); + request.Content = JsonContent.Create(msr, options: GlobalConsts.JsonSerializerOptions); using var response = await _httpClient.SendAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { @@ -60,11 +59,13 @@ public class ModeusHttpClient { _logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, eventId: {eventId}"); } - List? attendees; try { - attendees = Attendees.FromJson(await response.Content.ReadAsStringAsync()); - return attendees; + await using var contentStream = await response.Content.ReadAsStreamAsync(); + var attendees = await JsonSerializer.DeserializeAsync>( + contentStream, + GlobalConsts.JsonSerializerOptions); + return attendees ?? []; } catch (Exception ex) { @@ -78,9 +79,7 @@ public class ModeusHttpClient { using var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/people/persons/search"); - request.Content = new StringContent( - JsonSerializer.Serialize(modeusSearchPersonRequest, GlobalConsts.JsonSerializerOptions), - Encoding.UTF8, "application/json"); + request.Content = JsonContent.Create(modeusSearchPersonRequest, options: GlobalConsts.JsonSerializerOptions); var stopwatch = Stopwatch.StartNew(); using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); var requestMs = stopwatch.ElapsedMilliseconds; @@ -114,9 +113,7 @@ public class ModeusHttpClient public async Task SearchRoomsAsync(RoomSearchRequest requestDto) { using var request = new HttpRequestMessage(HttpMethod.Post, "schedule-calendar-v2/api/campus/rooms/search"); - request.Content = - new StringContent(JsonSerializer.Serialize(requestDto, GlobalConsts.JsonSerializerOptions), - Encoding.UTF8, "application/json"); + request.Content = JsonContent.Create(requestDto, options: GlobalConsts.JsonSerializerOptions); using var response = await _httpClient.SendAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) {