From 7578349220dd4244f84db81d3bb44c3948e248b4 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Fri, 9 Feb 2024 14:09:18 +0100 Subject: [PATCH] reservation: fix exiting on fetchL402 This commit changes the way the reservation manager works, by not exiting when the fetchL402 call fails. This ensures that a client can still run loop, even if they don't have outbound capacity or the loop server is offline. --- instantout/reservation/manager.go | 34 +++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/instantout/reservation/manager.go b/instantout/reservation/manager.go index 58cec31..120ed1c 100644 --- a/instantout/reservation/manager.go +++ b/instantout/reservation/manager.go @@ -23,6 +23,9 @@ type Manager struct { // activeReservations contains all the active reservationsFSMs. activeReservations map[ID]*FSM + // hasL402 is true if the client has a valid L402. + hasL402 bool + runCtx context.Context sync.Mutex @@ -155,16 +158,39 @@ func (m *Manager) newReservation(ctx context.Context, currentHeight uint32, return reservationFSM, nil } +// fetchL402 fetches the L402 from the server. This method will keep on +// retrying until it gets a valid response. +func (m *Manager) fetchL402(ctx context.Context) { + // Add a 0 timer so that we initially fetch the L402 immediately. + timer := time.NewTimer(0) + for { + select { + case <-ctx.Done(): + return + + case <-timer.C: + err := m.cfg.FetchL402(ctx) + if err != nil { + log.Warnf("Error fetching L402: %v", err) + timer.Reset(time.Second * 10) + continue + } + m.hasL402 = true + return + } + } +} + // RegisterReservationNotifications registers a new reservation notification // stream. func (m *Manager) RegisterReservationNotifications( reservationChan chan *reservationrpc.ServerReservationNotification) error { // In order to create a valid lsat we first are going to call - // the FetchL402 method. - err := m.cfg.FetchL402(m.runCtx) - if err != nil { - return err + // the FetchL402 method. As a client might not have outbound capacity + // yet, we'll retry until we get a valid response. + if !m.hasL402 { + m.fetchL402(m.runCtx) } ctx, cancel := context.WithCancel(m.runCtx)