Coder Social home page Coder Social logo

Comments (4)

tuscen avatar tuscen commented on May 27, 2024

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:

  1. Pass two cancellation tokens, one to cancel the core loop and another to cancel offset commit operation
  2. 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?
  3. 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.

Palindromer avatar Palindromer commented on May 27, 2024

@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.

tuscen avatar tuscen commented on May 27, 2024

@Palindromer That's what I wrote earlier - your suggested code does not handle cancellation at all

from telegram.bot.

Palindromer avatar Palindromer commented on May 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.