Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a RateLimitInfo property to Query #43

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions MetaBrainz.MusicBrainz/Query.Internals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ private async Task<HttpResponseMessage> PerformRequestAsync(Uri uri, HttpMethod
Debug.Print($"[{DateTime.UtcNow}] => HEADERS: {TextUtils.FormatMultiLine(response.Headers.ToString())}");
Debug.Print($"[{DateTime.UtcNow}] => CONTENT ({response.Content.Headers.ContentType}): " +
$"{response.Content.Headers.ContentLength ?? 0} bytes");
var rateLimitInfo = new RateLimitInfo(response.Headers);
this._rateLimitLock.EnterWriteLock();
try {
this._rateLimitInfo = rateLimitInfo;
}
finally {
this._rateLimitLock.ExitWriteLock();
}
if (!response.IsSuccessStatusCode) {
throw await QueryException.FromResponseAsync(response, cancellationToken).ConfigureAwait(false);
}
Expand Down
27 changes: 26 additions & 1 deletion MetaBrainz.MusicBrainz/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;

using JetBrains.Annotations;

using MetaBrainz.Common;

namespace MetaBrainz.MusicBrainz;

/// <summary>Class providing access to the MusicBrainz API.</summary>
Expand Down Expand Up @@ -77,7 +80,8 @@ public static string DefaultUrlScheme {
/// </summary>
/// <remarks>
/// Note that this is a global delay, affecting all threads. When querying the official MusicBrainz site, setting this below the
/// default of one second may incur penalties (ranging from rate limiting to IP bans).
/// default of one second may incur penalties (ranging from rate limiting to IP bans). When setting this to 0 for maximum
/// throughput, <see cref="RateLimitInfo"/> can be used to avoid making too many requests and trigger these penalties.
/// </remarks>
public static double DelayBetweenRequests { get; set; } = 1.0;

Expand Down Expand Up @@ -209,6 +213,27 @@ public int Port {
}
}

private RateLimitInfo _rateLimitInfo;

private readonly ReaderWriterLockSlim _rateLimitLock = new();

/// <summary>The rate limit information from the last web request issued via this MusicBrainz client.</summary>
/// <remarks>
/// This is mainly useful when setting <see cref="DelayBetweenRequests"/> to 0, in order to manage the request volume to avoid
/// triggering penalties.
/// </remarks>
public RateLimitInfo RateLimitInfo {
get {
this._rateLimitLock.EnterReadLock();
try {
return this._rateLimitInfo;
}
finally {
this._rateLimitLock.ExitReadLock();
}
}
}

private string _server = Query.DefaultServer;

/// <summary>The web site to use for requests.</summary>
Expand Down