Refactor ntfnstate mutex. (#85)
This refactors the notification state mutex out of the state itself to the client. This is being done since the code makes a copy of the notification state and accesses that copy immutably, and therefore there is no need for it to have its own mutex.
This commit is contained in:
parent
8c8d4453ad
commit
b89c91b9d6
2 changed files with 7 additions and 11 deletions
|
@ -145,8 +145,9 @@ type Client struct {
|
||||||
requestList *list.List
|
requestList *list.List
|
||||||
|
|
||||||
// Notifications.
|
// Notifications.
|
||||||
ntfnHandlers *NotificationHandlers
|
ntfnHandlers *NotificationHandlers
|
||||||
ntfnState *notificationState
|
ntfnStateLock sync.Mutex
|
||||||
|
ntfnState *notificationState
|
||||||
|
|
||||||
// Networking infrastructure.
|
// Networking infrastructure.
|
||||||
sendChan chan []byte
|
sendChan chan []byte
|
||||||
|
@ -233,8 +234,8 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.ntfnState.Lock()
|
c.ntfnStateLock.Lock()
|
||||||
defer c.ntfnState.Unlock()
|
defer c.ntfnStateLock.Unlock()
|
||||||
|
|
||||||
switch bcmd := cmd.(type) {
|
switch bcmd := cmd.(type) {
|
||||||
case *btcjson.NotifyBlocksCmd:
|
case *btcjson.NotifyBlocksCmd:
|
||||||
|
@ -498,7 +499,9 @@ func (c *Client) reregisterNtfns() error {
|
||||||
// the notification state (while not under the lock of course) which
|
// the notification state (while not under the lock of course) which
|
||||||
// also register it with the remote RPC server, so this prevents double
|
// also register it with the remote RPC server, so this prevents double
|
||||||
// registrations.
|
// registrations.
|
||||||
|
c.ntfnStateLock.Lock()
|
||||||
stateCopy := c.ntfnState.Copy()
|
stateCopy := c.ntfnState.Copy()
|
||||||
|
c.ntfnStateLock.Unlock()
|
||||||
|
|
||||||
// Reregister notifyblocks if needed.
|
// Reregister notifyblocks if needed.
|
||||||
if stateCopy.notifyBlocks {
|
if stateCopy.notifyBlocks {
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcjson"
|
"github.com/btcsuite/btcd/btcjson"
|
||||||
|
@ -32,7 +31,6 @@ var (
|
||||||
// registered notification so the state can be automatically re-established on
|
// registered notification so the state can be automatically re-established on
|
||||||
// reconnect.
|
// reconnect.
|
||||||
type notificationState struct {
|
type notificationState struct {
|
||||||
sync.Mutex
|
|
||||||
notifyBlocks bool
|
notifyBlocks bool
|
||||||
notifyNewTx bool
|
notifyNewTx bool
|
||||||
notifyNewTxVerbose bool
|
notifyNewTxVerbose bool
|
||||||
|
@ -41,12 +39,7 @@ type notificationState struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy returns a deep copy of the receiver.
|
// Copy returns a deep copy of the receiver.
|
||||||
//
|
|
||||||
// This function is safe for concurrent access.
|
|
||||||
func (s *notificationState) Copy() *notificationState {
|
func (s *notificationState) Copy() *notificationState {
|
||||||
s.Lock()
|
|
||||||
defer s.Unlock()
|
|
||||||
|
|
||||||
var stateCopy notificationState
|
var stateCopy notificationState
|
||||||
stateCopy.notifyBlocks = s.notifyBlocks
|
stateCopy.notifyBlocks = s.notifyBlocks
|
||||||
stateCopy.notifyNewTx = s.notifyNewTx
|
stateCopy.notifyNewTx = s.notifyNewTx
|
||||||
|
|
Loading…
Add table
Reference in a new issue