Make MusicBrainzAlbumProvider thread safe
This commit is contained in:
parent
c7b3d4a90c
commit
04cdc89a5c
|
@ -46,6 +46,7 @@ namespace MediaBrowser.Providers.Music
|
|||
|
||||
private readonly string _musicBrainzBaseUrl;
|
||||
|
||||
private SemaphoreSlim _apiRequestLock = new SemaphoreSlim(1, 1);
|
||||
private Stopwatch _stopWatchMusicBrainz = new Stopwatch();
|
||||
|
||||
public MusicBrainzAlbumProvider(
|
||||
|
@ -742,19 +743,14 @@ namespace MediaBrowser.Providers.Music
|
|||
/// </summary>
|
||||
internal async Task<HttpResponseMessage> GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
using var options = new HttpRequestMessage(HttpMethod.Get, _musicBrainzBaseUrl.TrimEnd('/') + url);
|
||||
|
||||
// MusicBrainz request a contact email address is supplied, as comment, in user agent field:
|
||||
// https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting#User-Agent
|
||||
options.Headers.UserAgent.ParseAdd(string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{0} ( {1} )",
|
||||
_appHost.ApplicationUserAgent,
|
||||
_appHost.ApplicationUserAgentAddress));
|
||||
|
||||
HttpResponseMessage response;
|
||||
var attempts = 0u;
|
||||
var requestUrl = _musicBrainzBaseUrl.TrimEnd('/') + url;
|
||||
|
||||
await _apiRequestLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
attempts++;
|
||||
|
@ -771,16 +767,31 @@ namespace MediaBrowser.Providers.Music
|
|||
_logger.LogDebug("GetMusicBrainzResponse: Time since previous request: {0} ms", _stopWatchMusicBrainz.ElapsedMilliseconds);
|
||||
_stopWatchMusicBrainz.Restart();
|
||||
|
||||
response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options).ConfigureAwait(false);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, _musicBrainzBaseUrl.TrimEnd('/') + url);
|
||||
|
||||
// MusicBrainz request a contact email address is supplied, as comment, in user agent field:
|
||||
// https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting#User-Agent
|
||||
request.Headers.UserAgent.ParseAdd(string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{0} ( {1} )",
|
||||
_appHost.ApplicationUserAgent,
|
||||
_appHost.ApplicationUserAgentAddress));
|
||||
|
||||
response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(request).ConfigureAwait(false);
|
||||
|
||||
// We retry a finite number of times, and only whilst MB is indicating 503 (throttling)
|
||||
}
|
||||
while (attempts < MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_apiRequestLock.Release();
|
||||
}
|
||||
|
||||
// Log error if unable to query MB database due to throttling
|
||||
if (attempts == MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable)
|
||||
{
|
||||
_logger.LogError("GetMusicBrainzResponse: 503 Service Unavailable (throttled) response received {0} times whilst requesting {1}", attempts, options.RequestUri);
|
||||
_logger.LogError("GetMusicBrainzResponse: 503 Service Unavailable (throttled) response received {0} times whilst requesting {1}", attempts, requestUrl);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
|
Loading…
Reference in New Issue
Block a user