Change exception handling when sending a request.
1) Now RetryAfter is multiplied by 1000, because request contains value in second instead milliseconds. (see https://core.telegram.org/bots/api#responseparameters) 2) Now calls occur in a loop, but a limited number of times. This also ensures that exceptions are caught during a repeated request.
This commit is contained in:
parent
706b0f522a
commit
e742ded371
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@ -770,20 +770,24 @@ namespace TelegramBotBase.Sessions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<T> API<T>(Func<Telegram.Bot.TelegramBotClient, Task<T>> call)
|
public async Task<T> API<T>(Func<Telegram.Bot.TelegramBotClient, Task<T>> call)
|
||||||
{
|
{
|
||||||
try
|
var numberOfTries = 0;
|
||||||
|
while (numberOfTries < DeviceSession.MaxNumberOfRetries)
|
||||||
{
|
{
|
||||||
return await call(this.Client.TelegramClient);
|
try
|
||||||
}
|
|
||||||
catch (ApiRequestException ex)
|
|
||||||
{
|
|
||||||
if (ex.Parameters != null)
|
|
||||||
{
|
{
|
||||||
await Task.Delay(ex.Parameters.RetryAfter);
|
return await call(Client.TelegramClient);
|
||||||
|
}
|
||||||
|
catch (ApiRequestException ex)
|
||||||
|
{
|
||||||
|
if (ex.ErrorCode != 429)
|
||||||
|
throw;
|
||||||
|
|
||||||
return await call(this.Client.TelegramClient);
|
if (ex.Parameters != null)
|
||||||
|
await Task.Delay(ex.Parameters.RetryAfter * 1000);
|
||||||
|
|
||||||
|
numberOfTries++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return default(T);
|
return default(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -794,17 +798,23 @@ namespace TelegramBotBase.Sessions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task API(Func<Telegram.Bot.TelegramBotClient, Task> call)
|
public async Task API(Func<Telegram.Bot.TelegramBotClient, Task> call)
|
||||||
{
|
{
|
||||||
try
|
var numberOfTries = 0;
|
||||||
|
while (numberOfTries < DeviceSession.MaxNumberOfRetries)
|
||||||
{
|
{
|
||||||
await call(this.Client.TelegramClient);
|
try
|
||||||
}
|
|
||||||
catch (ApiRequestException ex)
|
|
||||||
{
|
|
||||||
if (ex.Parameters != null)
|
|
||||||
{
|
{
|
||||||
await Task.Delay(ex.Parameters.RetryAfter);
|
await call(Client.TelegramClient);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (ApiRequestException ex)
|
||||||
|
{
|
||||||
|
if (ex.ErrorCode != 429)
|
||||||
|
throw;
|
||||||
|
|
||||||
await call(this.Client.TelegramClient);
|
if (ex.Parameters != null)
|
||||||
|
await Task.Delay(ex.Parameters.RetryAfter * 1000);
|
||||||
|
|
||||||
|
numberOfTries++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user