Skip to content

Commit d906e83

Browse files
committed
Retry on ratelimited error.
1 parent e2fba59 commit d906e83

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

WikiClientLibrary/Client/MediaWikiJsonResponseParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ protected virtual void OnApiError(string? errorCode, string? errorMessage,
158158
context.NeedRetry = true;
159159
throw new ServerLagException(errorCode, fullMessage, (double?)errorNode["lag"] ?? 0, (string)errorNode["type"],
160160
(string)errorNode["host"]);
161+
case "ratelimited":
162+
context.NeedRetry = true;
163+
throw new RateLimitedException(errorCode, fullMessage);
161164
case "permissiondenied":
162165
case "readapidenied": // You need read permission to use this module.
163166
case "mustbeloggedin": // You must be logged in to upload this file.

WikiClientLibrary/Exceptions.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public OperationFailedException(string? message, Exception? inner)
8181
public class BadValueException : OperationFailedException
8282
{
8383

84-
public BadValueException(string errorCode, string message)
85-
: base(errorCode, message)
84+
public BadValueException(string errorCode, string? errorMessage)
85+
: base(errorCode, errorMessage)
8686
{
8787
}
8888

@@ -95,8 +95,8 @@ public BadValueException(string errorCode, string message)
9595
public class InvalidActionException : BadValueException
9696
{
9797

98-
public InvalidActionException(string errorCode, string message)
99-
: base(errorCode, message)
98+
public InvalidActionException(string errorCode, string? errorMessage)
99+
: base(errorCode, errorMessage)
100100
{
101101
}
102102

@@ -113,8 +113,8 @@ public InvalidActionException(string errorCode, string message)
113113
public class AccountAssertionFailureException : OperationFailedException
114114
{
115115

116-
public AccountAssertionFailureException(string errorCode, string message)
117-
: base(errorCode, message)
116+
public AccountAssertionFailureException(string errorCode, string? errorMessage)
117+
: base(errorCode, errorMessage)
118118
{
119119
}
120120

@@ -195,8 +195,8 @@ public override string ToString()
195195
public class OperationConflictException : OperationFailedException
196196
{
197197

198-
public OperationConflictException(string errorCode, string message)
199-
: base(errorCode, message)
198+
public OperationConflictException(string errorCode, string? errorMessage)
199+
: base(errorCode, errorMessage)
200200
{
201201
}
202202

@@ -209,8 +209,8 @@ public OperationConflictException(string errorCode, string message)
209209
public class BadTokenException : OperationFailedException
210210
{
211211

212-
public BadTokenException(string errorCode, string message)
213-
: base(errorCode, message)
212+
public BadTokenException(string errorCode, string? errorMessage)
213+
: base(errorCode, errorMessage)
214214
{
215215
}
216216

@@ -265,15 +265,15 @@ public override string ToString()
265265
}
266266

267267
/// <summary>
268-
/// Raises when MediaWiki server replication lag
269-
/// does not meet the required limitation.
270-
/// (<a href="https://www.mediawiki.org/wiki/Manual:Maxlag_parameter">mw:Manual:Maxlag parameter</a>)
268+
/// Raised when MediaWiki server replication lag
269+
/// does not meet the required limitation. (<c>maxlag</c>)
270+
/// See <a href="https://www.mediawiki.org/wiki/Manual:Maxlag_parameter">mw:Manual:Maxlag parameter</a>.
271271
/// </summary>
272272
public class ServerLagException : OperationFailedException
273273
{
274274

275-
public ServerLagException(string errorCode, string message, double lag, string lagType, string laggedHost)
276-
: base(errorCode, message)
275+
public ServerLagException(string errorCode, string? errorMessage, double lag, string lagType, string laggedHost)
276+
: base(errorCode, errorMessage)
277277
{
278278
Lag = lag;
279279
LaggedHost = laggedHost;
@@ -300,21 +300,35 @@ public ServerLagException(string errorCode, string message, double lag, string l
300300

301301
}
302302

303+
/// <summary>
304+
/// Raised by MediaWiki server when the API operation is too frequent. (<c>ratelimited</c>).
305+
/// </summary>
306+
public class RateLimitedException : OperationFailedException
307+
{
308+
309+
public RateLimitedException(string errorCode, string? errorMessage)
310+
: base(errorCode, errorMessage)
311+
{
312+
}
313+
314+
315+
}
316+
303317
/// <summary>
304318
/// Raises when the MediaWiki server is in read-only mode.
305319
/// </summary>
306320
public class MediaWikiReadOnlyException : OperationFailedException
307321
{
308322

309-
internal static string? BuildExceptionMessage(string errorCode, string errorMessage, string? readOnlyReason)
323+
internal static string? BuildExceptionMessage(string errorCode, string? errorMessage, string? readOnlyReason)
310324
{
311325
errorMessage ??= "";
312326
if (!string.IsNullOrEmpty(readOnlyReason) && !errorMessage.Contains(readOnlyReason, StringComparison.Ordinal))
313327
errorMessage += " " + readOnlyReason;
314328
return BuildExceptionMessage(errorCode, errorMessage);
315329
}
316330

317-
public MediaWikiReadOnlyException(string errorCode, string errorMessage, string? readOnlyReason)
331+
public MediaWikiReadOnlyException(string errorCode, string? errorMessage, string? readOnlyReason)
318332
: base(errorCode, errorMessage, BuildExceptionMessage(errorCode, errorMessage, readOnlyReason))
319333
{
320334
ReadOnlyReason = readOnlyReason;

WikiClientLibrary/Infrastructures/Throttler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ async Task<IDisposable> RunWorkAsync(WorkItem? previousWork, WorkItem thisWork,
4545
if (ct.CanBeCanceled)
4646
{
4747
// With cancellation support.
48-
var tcs = new TaskCompletionSource<bool>();
49-
using (ct.Register(o => ((TaskCompletionSource<bool>)o!).SetCanceled(), tcs))
48+
var tcs = new TaskCompletionSource();
49+
using (ct.Register(o => ((TaskCompletionSource)o!).SetCanceled(), tcs))
5050
{
5151
await Task.WhenAny(previousWork.Completion, tcs.Task);
5252
}
@@ -122,7 +122,7 @@ public Task Completion
122122
private class WorkItem : IDisposable
123123
{
124124

125-
private readonly TaskCompletionSource<bool> completionTcs = new TaskCompletionSource<bool>();
125+
private readonly TaskCompletionSource completionTcs = new();
126126

127127
public WorkItem(string name)
128128
{
@@ -138,7 +138,7 @@ public WorkItem(string name)
138138
/// </summary>
139139
public void Dispose()
140140
{
141-
completionTcs.TrySetResult(true);
141+
completionTcs.TrySetResult();
142142
}
143143

144144
public override string ToString()

0 commit comments

Comments
 (0)