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

Proposed retry implementation based on Poly #25

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 22 additions & 13 deletions src/X.Extensions.Logging.Telegram.Base/TelegramLogWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Polly;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;

Expand All @@ -11,7 +11,7 @@ namespace X.Extensions.Logging.Telegram.Base;
public interface ILogWriter
{
Task Write(string message);

Task Write(string message, CancellationToken cancellationToken);
}

Expand All @@ -38,16 +38,25 @@ public async Task Write(string message)

public async Task Write(string message, CancellationToken cancellationToken)
{
var result = await _client.SendTextMessageAsync(
chatId: _chatId,
text: message,
parseMode: ParseMode.Html,
cancellationToken: cancellationToken);

#if DEBUG

Trace.WriteLine(result.MessageId);

#endif
const int retryCount = 5;

var retryPolicy = Policy
Bardin08 marked this conversation as resolved.
Show resolved Hide resolved
.Handle<Exception>()
.WaitAndRetryAsync(
retryCount: retryCount,
sleepDurationProvider: attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
onRetry: (exception, timeSpan, context) =>
{
// ignored
});

await retryPolicy.ExecuteAsync(async () =>
{
var result = await _client.SendTextMessageAsync(
chatId: _chatId,
text: message,
parseMode: ParseMode.Html,
cancellationToken: cancellationToken);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" PrivateAssets="All"/>
<PackageReference Include="Polly" Version="8.4.1" />
<PackageReference Include="Telegram.Bot" Version="19.0.0"/>
</ItemGroup>

Expand Down