From f282d270ae65aea6259027c53bf61fe1e0777eaa Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Thu, 24 Feb 2022 16:44:27 +0100 Subject: [PATCH] Retry API call once on error -503 Timeout --- EXAMPLES.md | 5 +---- src/Client.cs | 9 ++++++++- src/Session.cs | 2 +- src/TL.Schema.cs | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index d29ae3e..ffeab06 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -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}"); diff --git a/src/Client.cs b/src/Client.cs index 7750bc7..4fba0b5 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -40,6 +40,8 @@ namespace WTelegram public int FloodRetryThreshold { get; set; } = 60; /// Number of seconds between each keep-alive ping. Increase this if you have a slow connection or you're debugging your code public int PingInterval { get; set; } = 60; + /// Size of chunks when uploading/downloading files. Reduce this if you don't have much memory + public int FilePartSize { get; set; } = 512 * 1024; /// Is this Client instance the main or a secondary DC session public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC; /// Has this Client established connection been disconnected? @@ -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(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 diff --git a/src/Session.cs b/src/Session.cs index 5284283..f718492 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -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) { diff --git a/src/TL.Schema.cs b/src/TL.Schema.cs index 47ed9f8..f68e5af 100644 --- a/src/TL.Schema.cs +++ b/src/TL.Schema.cs @@ -15527,7 +15527,7 @@ namespace TL /// Peer /// Message ID /// Get only reactions of this type (UTF8 emoji) - /// Offset (typically taken from the next_offset field of the returned ) + /// Offset (typically taken from the next_offset field of the returned ) /// Maximum number of results to return, see pagination public static Task Messages_GetMessageReactionsList(this Client client, InputPeer peer, int id, int limit = int.MaxValue, string reaction = null, string offset = null) => client.Invoke(new Messages_GetMessageReactionsList