@@ -12,7 +12,7 @@ type paymentChannelListeners struct {
12
12
listeners []chan query.PaymentChannelInfo
13
13
// prev is the previous payment channel info that was sent to the listeners.
14
14
prev query.PaymentChannelInfo
15
- // listenersLock is used to protect against concurrent access to the listeners slice .
15
+ // listenersLock is used to protect against concurrent access to to sibling struct members .
16
16
listenersLock * sync.Mutex
17
17
}
18
18
@@ -24,6 +24,8 @@ func newPaymentChannelListeners() *paymentChannelListeners {
24
24
// Notify notifies all listeners of a payment channel update.
25
25
// It only notifies listeners if the new info is different from the previous info.
26
26
func (li * paymentChannelListeners ) Notify (info query.PaymentChannelInfo ) {
27
+ li .listenersLock .Lock ()
28
+ defer li .listenersLock .Unlock ()
27
29
if li .prev .Equal (info ) {
28
30
return
29
31
}
@@ -45,10 +47,13 @@ func (li *paymentChannelListeners) createNewListener() <-chan query.PaymentChann
45
47
46
48
// getOrCreateListener returns the first listener, creating one if none exist.
47
49
func (li * paymentChannelListeners ) getOrCreateListener () <- chan query.PaymentChannelInfo {
50
+ li .listenersLock .Lock ()
48
51
if len (li .listeners ) != 0 {
49
- return li .listeners [0 ]
52
+ l := li .listeners [0 ]
53
+ li .listenersLock .Unlock ()
54
+ return l
50
55
}
51
-
56
+ li . listenersLock . Unlock ()
52
57
return li .createNewListener ()
53
58
}
54
59
@@ -69,7 +74,7 @@ type ledgerChannelListeners struct {
69
74
listeners []chan query.LedgerChannelInfo
70
75
// prev is the previous ledger channel info that was sent to the listeners.
71
76
prev query.LedgerChannelInfo
72
- // listenersLock is used to protect against concurrent access to the listeners slice .
77
+ // listenersLock is used to protect against concurrent access to sibling struct members .
73
78
listenersLock sync.Mutex
74
79
}
75
80
@@ -81,6 +86,8 @@ func newLedgerChannelListeners() *ledgerChannelListeners {
81
86
// Notify notifies all listeners of a ledger channel update.
82
87
// It only notifies listeners if the new info is different from the previous info.
83
88
func (li * ledgerChannelListeners ) Notify (info query.LedgerChannelInfo ) {
89
+ li .listenersLock .Lock ()
90
+ defer li .listenersLock .Unlock ()
84
91
if li .prev .Equal (info ) {
85
92
return
86
93
}
@@ -103,10 +110,13 @@ func (li *ledgerChannelListeners) createNewListener() <-chan query.LedgerChannel
103
110
104
111
// getOrCreateListener returns the first listener, creating one if none exist.
105
112
func (li * ledgerChannelListeners ) getOrCreateListener () <- chan query.LedgerChannelInfo {
113
+ li .listenersLock .Lock ()
106
114
if len (li .listeners ) != 0 {
107
- return li .listeners [0 ]
115
+ l := li .listeners [0 ]
116
+ li .listenersLock .Unlock ()
117
+ return l
108
118
}
109
-
119
+ li . listenersLock . Unlock ()
110
120
return li .createNewListener ()
111
121
}
112
122
0 commit comments