Service_client_v2.cpp timer_callback

In the service_client_v2.cpp code (the one that introduces calling a service from a spinning node), we have a timer_callback that will get called every timer period (in the example case 1s). But that function starts by waiting_for_service for 1s, and if the service doesn’t become available during that period, it will either log an error if that was the cause, or log that everything is fine and we need to wait a little longer. But that is in a while loop, so that function will keep looping and giving the service a chance to become available, and logging that everything is ok every second until the service becomes available, at which point it will send the request.

All of that makes good sense to me, except this is a timer callback, so after that first second expired, the timer would queue up another identical function call to start going through the same loop. So, if the service actually takes more than 1s to become available, we will have 2 timer_callback functions in the call_back queue. And if it takes more than 2 seconds, we will have 3 function calls in the queue, etc.

Am I misunderstanding the function here, or wouldn’t it be better to make that wait_for_service to be an “if” instead of a “while”, and rely on the timer to kick off the next iteration of “wait for the service” ?

Hello @jimparker ,

You understood correctly. In this case, the service starts just fine so there is no issue. However, if for whatever reason the service takes longer to start, the scenario you presented would happen. So you could use an if statement in order to check the service status instead. However, keep in mind that you should reorganize the code in order to send the request ONLY when the service is available. I encourage you to modify the code and try it yourself.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.