invoiceregistry: extract dispatch to method

This commit is contained in:
Joost Jager 2018-12-19 16:15:09 +01:00
parent 3545685177
commit 78cd07570b
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -139,12 +139,27 @@ func (i *InvoiceRegistry) invoiceEventNotifier() {
// A sub-systems has just modified the invoice state, so we'll // A sub-systems has just modified the invoice state, so we'll
// dispatch notifications to all registered clients. // dispatch notifications to all registered clients.
case event := <-i.invoiceEvents: case event := <-i.invoiceEvents:
i.dispatchToClients(event)
case <-i.quit:
return
}
}
}
// dispatchToClients passes the supplied event to all notification clients that
// subscribed to all invoices. Add and settle indices are used to make sure that
// clients don't receive duplicate or unwanted events.
func (i *InvoiceRegistry) dispatchToClients(event *invoiceEvent) {
invoice := event.invoice
for clientID, client := range i.notificationClients { for clientID, client := range i.notificationClients {
// Before we dispatch this event, we'll check // Before we dispatch this event, we'll check
// to ensure that this client hasn't already // to ensure that this client hasn't already
// received this notification in order to // received this notification in order to
// ensure we don't duplicate any events. // ensure we don't duplicate any events.
invoice := event.invoice
// TODO(joostjager): Refactor switches.
switch { switch {
// If we've already sent this settle event to // If we've already sent this settle event to
// the client, then we can skip this. // the client, then we can skip this.
@ -168,6 +183,7 @@ func (i *InvoiceRegistry) invoiceEventNotifier() {
"add_index=%v, new add event index=%v", "add_index=%v, new add event index=%v",
clientID, client.addIndex, clientID, client.addIndex,
invoice.AddIndex) invoice.AddIndex)
case event.state == channeldb.ContractSettled && case event.state == channeldb.ContractSettled &&
client.settleIndex+1 != invoice.SettleIndex: client.settleIndex+1 != invoice.SettleIndex:
log.Warnf("client=%v for invoice "+ log.Warnf("client=%v for invoice "+
@ -186,25 +202,17 @@ func (i *InvoiceRegistry) invoiceEventNotifier() {
return return
} }
// Each time we send a notification to a // Each time we send a notification to a client, we'll record
// client, we'll record the latest add/settle // the latest add/settle index it has. We'll use this to ensure
// index it has. We'll use this to ensure we // we don't send a notification twice, which can happen if a new
// don't send a notification twice, which can // event is added while we're catching up a new client.
// happen if a new event is added while we're
// catching up a new client.
switch event.state { switch event.state {
case channeldb.ContractSettled: case channeldb.ContractSettled:
client.settleIndex = invoice.SettleIndex client.settleIndex = invoice.SettleIndex
case channeldb.ContractOpen: case channeldb.ContractOpen:
client.addIndex = invoice.AddIndex client.addIndex = invoice.AddIndex
default: default:
log.Errorf("unknown invoice "+ log.Errorf("unknown invoice state: %v", event.state)
"state: %v", event.state)
}
}
case <-i.quit:
return
} }
} }
} }