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:
Dave Collins 2016-08-17 13:12:27 -05:00 committed by GitHub
parent 8c8d4453ad
commit b89c91b9d6
2 changed files with 7 additions and 11 deletions

View file

@ -145,8 +145,9 @@ type Client struct {
requestList *list.List
// Notifications.
ntfnHandlers *NotificationHandlers
ntfnState *notificationState
ntfnHandlers *NotificationHandlers
ntfnStateLock sync.Mutex
ntfnState *notificationState
// Networking infrastructure.
sendChan chan []byte
@ -233,8 +234,8 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) {
return
}
c.ntfnState.Lock()
defer c.ntfnState.Unlock()
c.ntfnStateLock.Lock()
defer c.ntfnStateLock.Unlock()
switch bcmd := cmd.(type) {
case *btcjson.NotifyBlocksCmd:
@ -498,7 +499,9 @@ func (c *Client) reregisterNtfns() error {
// the notification state (while not under the lock of course) which
// also register it with the remote RPC server, so this prevents double
// registrations.
c.ntfnStateLock.Lock()
stateCopy := c.ntfnState.Copy()
c.ntfnStateLock.Unlock()
// Reregister notifyblocks if needed.
if stateCopy.notifyBlocks {

View file

@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/btcsuite/btcd/btcjson"
@ -32,7 +31,6 @@ var (
// registered notification so the state can be automatically re-established on
// reconnect.
type notificationState struct {
sync.Mutex
notifyBlocks bool
notifyNewTx bool
notifyNewTxVerbose bool
@ -41,12 +39,7 @@ type notificationState struct {
}
// Copy returns a deep copy of the receiver.
//
// This function is safe for concurrent access.
func (s *notificationState) Copy() *notificationState {
s.Lock()
defer s.Unlock()
var stateCopy notificationState
stateCopy.notifyBlocks = s.notifyBlocks
stateCopy.notifyNewTx = s.notifyNewTx