Browse Source

paymod: Add a retry modifier that retries payments on failure

This is likely a bit of overkill for this type of functionality, but it is a
nice first use-case of how functionality can be compartmentalized into
modifiers. If makes swapping retry mechanisms in and out really simple.
paymod-01
Christian Decker 5 years ago
parent
commit
7fe59d530e
  1. 36
      plugins/libplugin-pay.c
  2. 4
      plugins/libplugin-pay.h
  3. 3
      plugins/pay.c

36
plugins/libplugin-pay.c

@ -174,3 +174,39 @@ static inline void dummy_step_cb(struct dummy_data *dd,
REGISTER_PAYMENT_MODIFIER(dummy, struct dummy_data *, dummy_data_init,
dummy_step_cb);
static struct retry_mod_data *retry_data_init(struct payment *p);
static inline void retry_step_cb(struct retry_mod_data *rd,
struct payment *p);
REGISTER_PAYMENT_MODIFIER(retry, struct retry_mod_data *, retry_data_init,
retry_step_cb);
static struct retry_mod_data *
retry_data_init(struct payment *p)
{
struct retry_mod_data *rdata = tal(p, struct retry_mod_data);
struct retry_mod_data *parent_rdata;
if (p->parent != NULL) {
parent_rdata = payment_mod_retry_get_data(p->parent);
rdata->retries = parent_rdata->retries - 1;
} else {
rdata->retries = 10;
}
return rdata;
}
static inline void retry_step_cb(struct retry_mod_data *rd,
struct payment *p)
{
struct payment *subpayment;
struct retry_mod_data *rdata = payment_mod_retry_get_data(p);
if (p->step == PAYMENT_STEP_FAILED && rdata->retries > 0) {
subpayment = payment_new(p, p->cmd, p, p->modifiers);
payment_start(subpayment);
p->step = PAYMENT_STEP_RETRY;
}
payment_continue(p);
}

4
plugins/libplugin-pay.h

@ -145,6 +145,10 @@ struct dummy_data {
unsigned int *dummy_param;
};
struct retry_mod_data {
int retries;
};
/* List of globally available payment modifiers. */
extern struct payment_modifier dummy_pay_mod;
REGISTER_PAYMENT_MODIFIER_HEADER(retry, struct retry_mod_data);

3
plugins/pay.c

@ -1704,8 +1704,9 @@ static void init(struct plugin *p,
}
#if DEVELOPER
struct payment_modifier *paymod_mods[2] = {
struct payment_modifier *paymod_mods[3] = {
&dummy_pay_mod,
&retry_pay_mod,
NULL,
};

Loading…
Cancel
Save