1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-25 14:42:21 +01:00

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
5 changed files with 25 additions and 12 deletions

View File

@@ -74,7 +74,7 @@ bool HOT Scheduler::cancel_interval(Component *component, const std::string &nam
}
struct RetryArgs {
std::function<RetryResult()> func;
std::function<RetryResult(uint8_t)> func;
uint8_t retry_countdown;
uint32_t current_interval;
Component *component;
@@ -84,17 +84,18 @@ struct RetryArgs {
};
static void retry_handler(const std::shared_ptr<RetryArgs> &args) {
RetryResult retry_result = args->func();
if (retry_result == RetryResult::DONE || --args->retry_countdown <= 0)
RetryResult const retry_result = args->func(--args->retry_countdown);
if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
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); });
// backoff_increase_factor applied to third & later executions
args->current_interval *= args->backoff_increase_factor;
}
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())
this->cancel_retry(component, name);