Skip to content

Commit

Permalink
lsps2: use paymentFailure chan for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Sep 14, 2023
1 parent 894c80c commit 87d3808
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lsps2/intercept_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Interceptor struct {
registrationReady chan *registrationReadyEvent
notRegistered chan string
paymentReady chan string
paymentTimeout chan string
paymentFailure chan *paymentFailureEvent
paymentChanOpened chan *paymentChanOpenedEvent
inflightPayments map[string]*paymentState
Expand Down Expand Up @@ -68,7 +67,6 @@ func NewInterceptHandler(
registrationReady: make(chan *registrationReadyEvent, 1000),
notRegistered: make(chan string, 1000),
paymentReady: make(chan string, 1000),
paymentTimeout: make(chan string, 1000),
paymentFailure: make(chan *paymentFailureEvent, 1000),
paymentChanOpened: make(chan *paymentChanOpenedEvent, 1000),
inflightPayments: make(map[string]*paymentState),
Expand Down Expand Up @@ -148,8 +146,6 @@ func (i *Interceptor) Start(ctx context.Context) {
i.handlePartAwaitingRegistration(ev)
case paymentId := <-i.paymentReady:
i.handlePaymentReady(paymentId)
case paymentId := <-i.paymentTimeout:
i.handlePaymentTimeout(paymentId)
case ev := <-i.paymentFailure:
i.handlePaymentFailure(ev.paymentId, ev.code)
case ev := <-i.paymentChanOpened:
Expand All @@ -159,23 +155,27 @@ func (i *Interceptor) Start(ctx context.Context) {
}

func (i *Interceptor) handleNewPart(part *partState) {
payment, paymentExisted := i.inflightPayments[part.req.PaymentId()]
paymentId := part.req.PaymentId()
payment, paymentExisted := i.inflightPayments[paymentId]
if !paymentExisted {
payment = &paymentState{
id: part.req.PaymentId(),
id: paymentId,
fakeScid: part.req.Scid,
parts: make(map[string]*partState),
timeoutChan: make(chan struct{}),
}
i.inflightPayments[part.req.PaymentId()] = payment
i.inflightPayments[paymentId] = payment

go func() {
select {
case <-time.After(i.config.MppTimeout):
// Handle timeout inside the event loop, to make sure there are
// no race conditions, since this timeout watcher is running in
// a goroutine.
i.paymentTimeout <- part.req.PaymentId()
i.paymentFailure <- &paymentFailureEvent{
paymentId: paymentId,
code: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
}
case <-payment.timeoutChan:
// Stop listening for timeouts when the payment is ready.
}
Expand Down Expand Up @@ -666,10 +666,6 @@ func (i *Interceptor) handlePaymentChanOpened(event *paymentChanOpenedEvent) {
delete(i.inflightPayments, event.paymentId)
}

func (i *Interceptor) handlePaymentTimeout(paymentId string) {
i.handlePaymentFailure(paymentId, shared.FAILURE_TEMPORARY_CHANNEL_FAILURE)
}

func (i *Interceptor) handlePaymentFailure(
paymentId string,
code shared.InterceptFailureCode,
Expand Down

0 comments on commit 87d3808

Please sign in to comment.