Comments (4)
I wouldn't say that this is a bug, but a specific behaviour desired by some people. I could implement an additional option to do that, but it won't be 100% reliable and have it's own can of worms. E.g. how do you think we should handle cancellation in this case since the original token is cancelled?
I see three options how to commit the last offset:
- Pass two cancellation tokens, one to cancel the core loop and another to cancel offset commit operation
- Use a timeout for cancellation operation, but what if I want to cancel the operation manually and not wait for a timeout I don't have any API to control?
- Add another method to the interfaces to all of the pollers that one can call to commit the latest offset using previously saved offset
To me the first two are horrible since they introduce not obvious lifetimes and cancellation mechanisms and only the third one seems the most sane. In the end, there is no easy solution that is ideal and satisfy everyone.
from telegram.bot.
@tuscen, I suggest the following solution:
Firstly, we could introduce a check for the cancellation token within each iteration of the foreach-loop. And if a cancellation is requested, break out of the loop, ensuring that any remaining updates are not handled.
Secondly, upon cancellation, send a GetUpdatesRequest
with the offset of the last successfully handled update. This ensures that the offset is appropriately committed even in the event of a graceful shutdown.
Suggested changes in code:
foreach (var update in updates)
{
try
{
await updateHandler.HandleUpdateAsync(
botClient: _botClient,
update: update,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// ignored
}
if (cancellationToken.IsCancellationRequested)
{
// Commit the last succeful offset.
var request = new GetUpdatesRequest
{
Limit = 1,
Offset = messageOffset,
};
await _botClient.MakeRequestAsync(request);
// Don't handle the rest of updates.
break;
}
messageOffset = update.Id + 1;
}
from telegram.bot.
@Palindromer That's what I wrote earlier - your suggested code does not handle cancellation at all
from telegram.bot.
@tuscen Could you please explain more why do you think cancellation is not adequately handled by the suggested code?
from telegram.bot.
Related Issues (20)
- Bot API 6.8 (August 18, 2023)
- Missing documentation InputOnlineFile => InputFileUrl HOT 3
- Bot API 6.9 (September 22, 2023)
- Could we have InputMediaBase inherit from IAlbumInputMedia? HOT 3
- Erro não autorizado HOT 2
- Replace Newtonsoft.Json with System.Text.Json with proper source generation HOT 10
- Release 19.0.1 HOT 3
- Bot API 7.0 (December 29, 2023)
- Calling MakeRequestAsync with Stream content more then once results in error HOT 2
- Looks like this source generator is completely non incremental HOT 3
- telegram.bot is missing NuGet package README file
- Telegram.Bot.dll PUA classification with ESET problem HOT 5
- Send v20.0.0-alpha.3 to nuget plz HOT 1
- Project is not compatible with net8.0 (.NETCoreApp,Version=v8.0) HOT 7
- System.Text.Json deserialization issue HOT 5
- Temporary alternative NuGet feed due to AV software false positives
- GetStickerSetAsync() fails: Required property 'is_animated' not found in JSON HOT 2
- Required property 'is_animated' not found in JSON HOT 2
- How to resize replymarkup? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telegram.bot.