perf: Оптимизировал работу с Modeus API
All checks were successful
Create and publish a Docker image / Publish image (push) Successful in 1m30s

Устранил искусственную задержку и принудительный сбор мусора в сервисе сотрудников, повысив его отзывчивость.

Перевел HTTP-запросы на использование `JsonContent.Create` и асинхронную десериализацию из потока, что значительно улучшило эффективность обработки JSON.
This commit is contained in:
2026-01-25 01:41:09 +03:00
parent 50ca622b3e
commit fa8418aedb
2 changed files with 9 additions and 16 deletions

View File

@@ -36,10 +36,6 @@ public class ModeusEmployeeService(ILogger<ModeusEmployeeService> logger, Modeus
_employees = employees; _employees = employees;
logger.LogInformationHere($"Получено {employees.Count} сотрудников из Modeus."); logger.LogInformationHere($"Получено {employees.Count} сотрудников из Modeus.");
} }
await Task.Delay(TimeSpan.FromSeconds(5), _cts.Token);
GC.Collect();
GC.WaitForPendingFinalizers();
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {

View File

@@ -1,6 +1,6 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Net.Http.Json;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using ModeusSchedule.Abstractions; using ModeusSchedule.Abstractions;
using ModeusSchedule.Abstractions.DTO; using ModeusSchedule.Abstractions.DTO;
@@ -40,8 +40,7 @@ public class ModeusHttpClient
{ {
using var request = new HttpRequestMessage(HttpMethod.Post, using var request = new HttpRequestMessage(HttpMethod.Post,
$"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}"); $"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}");
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions), request.Content = JsonContent.Create(msr, options: GlobalConsts.JsonSerializerOptions);
Encoding.UTF8, "application/json");
using var response = await _httpClient.SendAsync(request); using var response = await _httpClient.SendAsync(request);
if (response.StatusCode != System.Net.HttpStatusCode.OK) if (response.StatusCode != System.Net.HttpStatusCode.OK)
{ {
@@ -60,11 +59,13 @@ public class ModeusHttpClient
{ {
_logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, eventId: {eventId}"); _logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, eventId: {eventId}");
} }
List<Attendees>? attendees;
try try
{ {
attendees = Attendees.FromJson(await response.Content.ReadAsStringAsync()); await using var contentStream = await response.Content.ReadAsStreamAsync();
return attendees; var attendees = await JsonSerializer.DeserializeAsync<List<Attendees>>(
contentStream,
GlobalConsts.JsonSerializerOptions);
return attendees ?? [];
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -78,9 +79,7 @@ public class ModeusHttpClient
{ {
using var request = new HttpRequestMessage(HttpMethod.Post, using var request = new HttpRequestMessage(HttpMethod.Post,
$"schedule-calendar-v2/api/people/persons/search"); $"schedule-calendar-v2/api/people/persons/search");
request.Content = new StringContent( request.Content = JsonContent.Create(modeusSearchPersonRequest, options: GlobalConsts.JsonSerializerOptions);
JsonSerializer.Serialize(modeusSearchPersonRequest, GlobalConsts.JsonSerializerOptions),
Encoding.UTF8, "application/json");
var stopwatch = Stopwatch.StartNew(); var stopwatch = Stopwatch.StartNew();
using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var requestMs = stopwatch.ElapsedMilliseconds; var requestMs = stopwatch.ElapsedMilliseconds;
@@ -114,9 +113,7 @@ public class ModeusHttpClient
public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto) public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto)
{ {
using var request = new HttpRequestMessage(HttpMethod.Post, "schedule-calendar-v2/api/campus/rooms/search"); using var request = new HttpRequestMessage(HttpMethod.Post, "schedule-calendar-v2/api/campus/rooms/search");
request.Content = request.Content = JsonContent.Create(requestDto, options: GlobalConsts.JsonSerializerOptions);
new StringContent(JsonSerializer.Serialize(requestDto, GlobalConsts.JsonSerializerOptions),
Encoding.UTF8, "application/json");
using var response = await _httpClient.SendAsync(request); using var response = await _httpClient.SendAsync(request);
if (response.StatusCode != System.Net.HttpStatusCode.OK) if (response.StatusCode != System.Net.HttpStatusCode.OK)
{ {