invoices: add client to map before delivering backlog notif

Prior to this change, if SubscribeSingleInvoice or
SubscribeNotifications was called, it was possible that a state
change would never be delivered to the client. The sequence of
events would be:
- delivery of backlog events with invoice in the Open state
- invoice goes to the Accepted state, no client to notify
- client added to map

This is fixed by adding the client to the map first. However, with
this change alone it then becomes possible for notifications to be
delivered out of order. This is addressed in a following commit.
This commit is contained in:
eugene 2022-05-04 17:38:07 -04:00
parent c2adb03e38
commit 93f87acb59
No known key found for this signature in database
GPG Key ID: 118759E83439A9B1

View File

@ -1543,6 +1543,10 @@ func (i *InvoiceRegistry) SubscribeNotifications(
}
}()
i.Lock()
i.notificationClients[client.id] = client
i.Unlock()
// Query the database to see if based on the provided addIndex and
// settledIndex we need to deliver any backlog notifications.
err := i.deliverBacklogEvents(client)
@ -1552,12 +1556,6 @@ func (i *InvoiceRegistry) SubscribeNotifications(
log.Infof("New invoice subscription client: id=%v", client.id)
i.Lock()
// With the backlog notifications delivered (if any), we'll add this to
// our active subscriptions.
i.notificationClients[client.id] = client
i.Unlock()
return client, nil
}
@ -1617,6 +1615,10 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice(
}
}()
i.Lock()
i.singleNotificationClients[client.id] = client
i.Unlock()
err := i.deliverSingleBacklogEvents(client)
if err != nil {
return nil, err
@ -1625,10 +1627,6 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice(
log.Infof("New single invoice subscription client: id=%v, ref=%v",
client.id, client.invoiceRef)
i.Lock()
i.singleNotificationClients[client.id] = client
i.Unlock()
return client, nil
}