Skip to content

Commit 58e76b7

Browse files
committed
routing: add docs and monior refactor decideNextStep
Add verbose docs and refactor the method to exit early when `allow` is true.
1 parent ca10707 commit 58e76b7

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

routing/payment_lifecycle.go

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ const (
121121
)
122122

123123
// decideNextStep is used to determine the next step in the payment lifecycle.
124+
// It first checks whether the current state of the payment allows more HTLC
125+
// attempts to be made. If allowed, it will return so the lifecycle can continue
126+
// making new attempts. Otherwise, it checks whether we need to wait for the
127+
// results of already sent attempts. If needed, it will block until one of the
128+
// results is sent back. then process its result here. When there's no need to
129+
// wait for results, the method will exit with `stepExit` such that the payment
130+
// lifecycle loop will terminate.
124131
func (p *paymentLifecycle) decideNextStep(
125132
payment DBMPPayment) (stateStep, error) {
126133

@@ -130,53 +137,53 @@ func (p *paymentLifecycle) decideNextStep(
130137
return stepExit, err
131138
}
132139

133-
if !allow {
134-
// Check whether we need to wait for results.
135-
wait, err := payment.NeedWaitAttempts()
136-
if err != nil {
137-
return stepExit, err
138-
}
139-
140-
// If we are not allowed to make new HTLC attempts and there's
141-
// no need to wait, the lifecycle is done and we can exit.
142-
if !wait {
143-
return stepExit, nil
144-
}
140+
// Exit early we need to make more attempts.
141+
if allow {
142+
return stepProceed, nil
143+
}
145144

146-
log.Tracef("Waiting for attempt results for payment %v",
147-
p.identifier)
145+
// We cannot make more attempts, we now check whether we need to wait
146+
// for results.
147+
wait, err := payment.NeedWaitAttempts()
148+
if err != nil {
149+
return stepExit, err
150+
}
148151

149-
// Otherwise we wait for the result for one HTLC attempt then
150-
// continue the lifecycle.
151-
select {
152-
case r := <-p.resultCollected:
153-
log.Tracef("Received attempt result for payment %v",
154-
p.identifier)
152+
// If we are not allowed to make new HTLC attempts and there's no need
153+
// to wait, the lifecycle is done and we can exit.
154+
if !wait {
155+
return stepExit, nil
156+
}
155157

156-
// Handle the result here. If there's no error, we will
157-
// return stepSkip and move to the next lifecycle
158-
// iteration, which will refresh the payment and wait
159-
// for the next attempt result, if any.
160-
_, err := p.handleAttemptResult(r.attempt, r.result)
158+
log.Tracef("Waiting for attempt results for payment %v", p.identifier)
161159

162-
// We would only get a DB-related error here, which will
163-
// cause us to abort the payment flow.
164-
if err != nil {
165-
return stepExit, err
166-
}
160+
// Otherwise we wait for the result for one HTLC attempt then continue
161+
// the lifecycle.
162+
select {
163+
case r := <-p.resultCollected:
164+
log.Tracef("Received attempt result for payment %v",
165+
p.identifier)
167166

168-
case <-p.quit:
169-
return stepExit, ErrPaymentLifecycleExiting
167+
// Handle the result here. If there's no error, we will return
168+
// stepSkip and move to the next lifecycle iteration, which will
169+
// refresh the payment and wait for the next attempt result, if
170+
// any.
171+
_, err := p.handleAttemptResult(r.attempt, r.result)
170172

171-
case <-p.router.quit:
172-
return stepExit, ErrRouterShuttingDown
173+
// We would only get a DB-related error here, which will cause
174+
// us to abort the payment flow.
175+
if err != nil {
176+
return stepExit, err
173177
}
174178

175-
return stepSkip, nil
179+
case <-p.quit:
180+
return stepExit, ErrPaymentLifecycleExiting
181+
182+
case <-p.router.quit:
183+
return stepExit, ErrRouterShuttingDown
176184
}
177185

178-
// Otherwise we need to make more attempts.
179-
return stepProceed, nil
186+
return stepSkip, nil
180187
}
181188

182189
// resumePayment resumes the paymentLifecycle from the current state.

0 commit comments

Comments
 (0)