From 3230cc48223debda4cd47fdf530cd879a2572b11 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 29 Nov 2021 13:45:42 +0800 Subject: [PATCH] invoices: remove redundant mutex lock --- invoices/invoiceregistry.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 543c41968..a2ce60550 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -101,13 +101,13 @@ func (r *htlcReleaseEvent) Less(other queue.PriorityQueueItem) bool { type InvoiceRegistry struct { sync.RWMutex + nextClientID uint32 // must be used atomically + cdb *channeldb.DB // cfg contains the registry's configuration parameters. cfg *RegistryConfig - clientMtx sync.Mutex - nextClientID uint32 notificationClients map[uint32]*InvoiceSubscription singleNotificationClients map[uint32]*SingleInvoiceSubscription @@ -1514,10 +1514,9 @@ func (i *InvoiceRegistry) SubscribeNotifications( } client.ntfnQueue.Start() - i.clientMtx.Lock() - client.id = i.nextClientID - i.nextClientID++ - i.clientMtx.Unlock() + // Always increment by 1 first, and our client ID will start with 1, + // not 0. + client.id = atomic.AddUint32(&i.nextClientID, 1) // Before we register this new invoice subscription, we'll launch a new // goroutine that will proxy all notifications appended to the end of @@ -1613,10 +1612,9 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice( } client.ntfnQueue.Start() - i.clientMtx.Lock() - client.id = i.nextClientID - i.nextClientID++ - i.clientMtx.Unlock() + // Always increment by 1 first, and our client ID will start with 1, + // not 0. + client.id = atomic.AddUint32(&i.nextClientID, 1) // Before we register this new invoice subscription, we'll launch a new // goroutine that will proxy all notifications appended to the end of