set_retry: add retries remaining parameter to the provided function (#4251)

This commit is contained in:
Dan Jackson 2023-01-17 17:26:32 -08:00 committed by GitHub
parent 3aa5953cd9
commit 029ac75a04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 12 deletions

View file

@ -57,7 +57,7 @@ bool Component::cancel_interval(const std::string &name) { // NOLINT
} }
void Component::set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, void Component::set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
std::function<RetryResult()> &&f, float backoff_increase_factor) { // NOLINT std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor); App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
} }
@ -130,7 +130,7 @@ void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // N
void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
App.scheduler.set_interval(this, "", interval, std::move(f)); App.scheduler.set_interval(this, "", interval, std::move(f));
} }
void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult()> &&f, void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f,
float backoff_increase_factor) { // NOLINT float backoff_increase_factor) { // NOLINT
App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor); App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
} }

View file

@ -193,6 +193,9 @@ class Component {
* increased by multipling by `backoff_increase_factor` each time. If no backoff_increase_factor is * increased by multipling by `backoff_increase_factor` each time. If no backoff_increase_factor is
* supplied (default = 1.0), the wait time will stay constant. * supplied (default = 1.0), the wait time will stay constant.
* *
* The retry function f needs to accept a single argument: the number of attempts remaining. On the
* final retry of f, this value will be 0.
*
* This retry function can also be cancelled by name via cancel_retry(). * This retry function can also be cancelled by name via cancel_retry().
* *
* IMPORTANT: Do not rely on this having correct timing. This is only called from * IMPORTANT: Do not rely on this having correct timing. This is only called from
@ -211,9 +214,9 @@ class Component {
* @see cancel_retry() * @see cancel_retry()
*/ */
void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
std::function<RetryResult()> &&f, float backoff_increase_factor = 1.0f); // NOLINT std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult()> &&f, // NOLINT void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
float backoff_increase_factor = 1.0f); // NOLINT float backoff_increase_factor = 1.0f); // NOLINT
/** Cancel a retry function. /** Cancel a retry function.

View file

@ -74,7 +74,7 @@ bool HOT Scheduler::cancel_interval(Component *component, const std::string &nam
} }
struct RetryArgs { struct RetryArgs {
std::function<RetryResult()> func; std::function<RetryResult(uint8_t)> func;
uint8_t retry_countdown; uint8_t retry_countdown;
uint32_t current_interval; uint32_t current_interval;
Component *component; Component *component;
@ -84,17 +84,18 @@ struct RetryArgs {
}; };
static void retry_handler(const std::shared_ptr<RetryArgs> &args) { static void retry_handler(const std::shared_ptr<RetryArgs> &args) {
RetryResult retry_result = args->func(); RetryResult const retry_result = args->func(--args->retry_countdown);
if (retry_result == RetryResult::DONE || --args->retry_countdown <= 0) if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
return; return;
// second execution of `func` hapens after `initial_wait_time` // second execution of `func` happens after `initial_wait_time`
args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); }); args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
// backoff_increase_factor applied to third & later executions // backoff_increase_factor applied to third & later executions
args->current_interval *= args->backoff_increase_factor; args->current_interval *= args->backoff_increase_factor;
} }
void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
uint8_t max_attempts, std::function<RetryResult()> func, float backoff_increase_factor) { uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
float backoff_increase_factor) {
if (!name.empty()) if (!name.empty())
this->cancel_retry(component, name); this->cancel_retry(component, name);

View file

@ -16,7 +16,7 @@ class Scheduler {
bool cancel_interval(Component *component, const std::string &name); bool cancel_interval(Component *component, const std::string &name);
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
std::function<RetryResult()> func, float backoff_increase_factor = 1.0f); std::function<RetryResult(uint8_t)> func, float backoff_increase_factor = 1.0f);
bool cancel_retry(Component *component, const std::string &name); bool cancel_retry(Component *component, const std::string &name);
optional<uint32_t> next_schedule_in(); optional<uint32_t> next_schedule_in();

View file

@ -462,6 +462,15 @@ binary_sensor:
name: Mi Motion Sensor 2 Light name: Mi Motion Sensor 2 Light
button: button:
name: Mi Motion Sensor 2 Button name: Mi Motion Sensor 2 Button
- platform: gpio
id: gpio_set_retry_test
pin: GPIO9
on_press:
then:
- lambda: |-
App.scheduler.set_retry(id(gpio_set_retry_test), "set_retry_test", 100, 3, [](const uint8_t remaining) {
return remaining ? RetryResult::RETRY : RetryResult::DONE; // just to reference both symbols
}, 5.0f);
esp32_ble_tracker: esp32_ble_tracker:
on_ble_advertise: on_ble_advertise: