Giter Site home page Giter Site logo

Comments (3)

rbresalier avatar rbresalier commented on August 23, 2024 1

@Tradias : Thanks so much for your answer! Indeed I was going out of scope due to my timer being in the while loop's scope, stupid mistake, I should have known better. After fixing that it does indeed work properly. But still glad I asked this because you gave me even more useful information in your response! Thanks so much for your work here, and I know I will have more questions. I'm assuming opening an issue is the right way to ask a question, so I'll ask my next one this way, unless you tell me otherwise.

from asio-grpc.

Tradias avatar Tradias commented on August 23, 2024

asio-grpc works correctly with steady_timer. Given your code snippet I assume that the object t goes out of scope before its deadline expires causing immediate invocation of the timer_callback. You can either do:

    auto t = std::make_unique<boost::asio::steady_timer>(grpc_context, boost::asio::chrono::seconds(5));
    auto& timer = *t;
    timer.async_wait(
        [t = std::move(t)](const boost::system::error_code& ec)
        {
            timer_callback(ec);
        });

or even better, since you already seem to be inside of a coroutine:

    boost::system::error_code ec;
    co_await timer.async_wait(asio::redirect_error(asio::use_awaitable, ec));

Also note, when you create a steady_timer Asio will create an io_context and run it on a background thread. That is rather inefficient. You can instead create a timer for the GrpcContext like so:

    grpc::Alarm alarm;
    bool timer_ran_to_completion =
        co_await agrpc::wait(alarm, boost::asio::chrono::system_clock::now() + boost::asio::chrono::seconds(5));

or again using callback style:

    auto t = std::make_unique<grpc::Alarm>();
    auto& timer = *t;
    agrpc::wait(timer, boost::asio::chrono::system_clock::now() + boost::asio::chrono::seconds(5),
                asio::bind_executor(grpc_context,
                                    [t = std::move(t)](bool timer_ran_to_completion)
                                    {
                                        timer_callback(timer_ran_to_completion);
                                    }));

from asio-grpc.

Tradias avatar Tradias commented on August 23, 2024

Yes please continue to open issues for questions. I must say I am new to managing an open source project like this and am open to suggestions on how to better interact with the community.

from asio-grpc.

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.