diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-08-25 20:30:54 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-08-25 20:30:54 -0500 |
| commit | 339086d048cbb2ce224a6b6eef2ce0e10be42a34 (patch) | |
| tree | c6d202681d8638f495e9afa6ff1603b0343c485a | |
| parent | 75c96b1d5ffef182428fa752507e6fe7a1b5a338 (diff) | |
| download | slskd-339086d048cbb2ce224a6b6eef2ce0e10be42a34.tar.gz | |
instrument search responses
| -rw-r--r-- | src/slskd/Application.cs | 76 |
1 files changed, 47 insertions, 29 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index ed3aec30..3a9cb666 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -876,47 +876,65 @@ namespace slskd /// <returns>A Task resolving a SearchResponse, or null.</returns> private async Task<SearchResponse> SearchResponseResolver(string username, int token, SearchQuery query) { - if (CompiledSearchResponseFilters.Any(filter => filter.IsMatch(query.SearchText))) + try { - return null; - } + Metrics.SearchRequestsReceived.Inc(1); - // sometimes clients send search queries consisting only of exclusions; drop them. - // no other clients send search results for these, even though it is technically possible. - if (!query.Terms.Any()) - { - return null; - } + if (CompiledSearchResponseFilters.Any(filter => filter.IsMatch(query.SearchText))) + { + return null; + } - try - { - var results = await Shares.SearchAsync(query); + // sometimes clients send search queries consisting only of exclusions; drop them. + // no other clients send search results for these, even though it is technically possible. + if (!query.Terms.Any()) + { + return null; + } - if (results.Any()) + try { - // make sure our average speed (as reported by the server) is reasonably up to date - await RefreshUserStatistics(); + var sw = new Stopwatch(); + sw.Start(); - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var results = await Shares.SearchAsync(query); - Log.Information("[{Context}]: Sending {Count} records to {Username} for query '{Query}'", "SEARCH RESULT SENT", results.Count(), username, query.SearchText); + sw.Stop(); + Metrics.SearchResponseLatency.Observe(sw.ElapsedMilliseconds); - return new SearchResponse( - Client.Username, - token, - uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, - freeUploadSlots: forecastedPosition == 0 ? 1 : 0, - queueLength: forecastedPosition, - fileList: results); - } + if (results.Any()) + { + // make sure our average speed (as reported by the server) is reasonably up to date + await RefreshUserStatistics(); + + var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); - // if no results, either return null or an instance of SearchResponse with a fileList of length 0 in either case, no - // response will be sent to the requestor. - return null; + Log.Information("[{Context}]: Sending {Count} records to {Username} for query '{Query}'", "SEARCH RESULT SENT", results.Count(), username, query.SearchText); + + Metrics.SearchResponsesSent.Inc(1); + + return new SearchResponse( + Client.Username, + token, + uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, + freeUploadSlots: forecastedPosition == 0 ? 1 : 0, + queueLength: forecastedPosition, + fileList: results); + } + + // if no results, either return null or an instance of SearchResponse with a fileList of length 0 in either case, no + // response will be sent to the requestor. + return null; + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to resolve search response: {Message}", ex.Message); + throw; + } } catch (Exception ex) { - Log.Warning(ex, "Failed to resolve search response: {Message}", ex.Message); + Console.WriteLine(ex); throw; } } |
