Retry API call once on error -503 Timeout

This commit is contained in:
Wizou 2022-02-24 16:44:27 +01:00
parent 722a8313b0
commit f282d270ae
4 changed files with 11 additions and 7 deletions

View file

@ -371,10 +371,7 @@ If you are not in a Console app or don't want the logs on screen, you can redire
// • Log to VS Output debugging pane in addition to default Console screen logging:
WTelegram.Helpers.Log += (lvl, str) => System.Diagnostics.Debug.WriteLine(str);
// • Log to file in replacement of default Console screen logging:
WTelegram.Helpers.Log = (lvl, str) => File.AppendAllText("WTelegram.log", str + Environment.NewLine);
// • More efficient example with a static variable and detailed logging to file:
// • Log to file in replacement of default Console screen logging, using this static variable:
static StreamWriter WTelegramLogs = new StreamWriter("WTelegram.log", true, Encoding.UTF8) { AutoFlush = true };
...
WTelegram.Helpers.Log = (lvl, str) => WTelegramLogs.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}");

View file

@ -40,6 +40,8 @@ namespace WTelegram
public int FloodRetryThreshold { get; set; } = 60;
/// <summary>Number of seconds between each keep-alive ping. Increase this if you have a slow connection or you're debugging your code</summary>
public int PingInterval { get; set; } = 60;
/// <summary>Size of chunks when uploading/downloading files. Reduce this if you don't have much memory</summary>
public int FilePartSize { get; set; } = 512 * 1024;
/// <summary>Is this Client instance the main or a secondary DC session</summary>
public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC;
/// <summary>Has this Client established connection been disconnected?</summary>
@ -68,7 +70,6 @@ namespace WTelegram
private Task _connecting;
private CancellationTokenSource _cts;
private int _reactorReconnects = 0;
private const int FilePartSize = 512 * 1024;
private const string ConnectionShutDown = "Could not read payload length : Connection shut down";
private readonly SemaphoreSlim _parallelTransfers = new(10); // max parallel part uploads/downloads
private readonly SHA256 _sha256 = SHA256.Create();
@ -840,6 +841,7 @@ namespace WTelegram
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
lock (_pendingRequests)
_pendingRequests[msgId] = (typeof(X), tcs);
bool got503 = false;
var result = await tcs.Task;
switch (result)
{
@ -873,6 +875,11 @@ namespace WTelegram
goto retry;
}
}
else if (rpcError.error_code == -503 && !got503)
{
got503 = true;
goto retry;
}
else if (rpcError.error_code == 500 && rpcError.error_message == "AUTH_RESTART")
{
_session.UserId = 0; // force a full login authorization flow, next time

View file

@ -120,7 +120,7 @@ namespace WTelegram
private int _nextPosition = 8;
public SessionStore(string pathname)
: base(pathname, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1) // no buffering
: base(pathname, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1) // no in-app buffering
{
if (base.Read(_header, 0, 8) == 8)
{

View file

@ -15527,7 +15527,7 @@ namespace TL
/// <param name="peer">Peer</param>
/// <param name="id">Message ID</param>
/// <param name="reaction">Get only reactions of this type (UTF8 emoji)</param>
/// <param name="offset">Offset (typically taken from the <c>next_offset</c> field of the returned <see cref="MessageReactionsList"/>)</param>
/// <param name="offset">Offset (typically taken from the <c>next_offset</c> field of the returned <see cref="Messages_MessageReactionsList"/>)</param>
/// <param name="limit">Maximum number of results to return, <a href="https://corefork.telegram.org/api/offsets">see pagination</a></param>
public static Task<Messages_MessageReactionsList> Messages_GetMessageReactionsList(this Client client, InputPeer peer, int id, int limit = int.MaxValue, string reaction = null, string offset = null)
=> client.Invoke(new Messages_GetMessageReactionsList