p2p: more locks
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jul 13, 2018 at 16:40 UTC
8849193de0f0d11c792f01b3d309401f488fd5d6
2 files changed
+19
-8
core/commands/p2p.go
+12
-1
@@ -231,6 +231,7 @@ var p2pLsCmd = &cmds.Command{
231
232
output := &P2PLsOutput{}
233
234
+ n.P2P.Listeners.Lock()
235
for _, listener := range n.P2P.Listeners.Listeners {
236
output.Listeners = append(output.Listeners, P2PListenerInfoOutput{
237
Protocol: string(listener.Protocol()),
@@ -238,6 +239,7 @@ var p2pLsCmd = &cmds.Command{
239
TargetAddress: listener.TargetAddress().String(),
240
})
241
}
242
+ n.P2P.Listeners.Unlock()
243
244
res.SetOutput(output)
245
},
@@ -402,6 +404,7 @@ var p2pStreamLsCmd = &cmds.Command{
404
405
output := &P2PStreamsOutput{}
406
407
+ n.P2P.Streams.Lock()
408
for id, s := range n.P2P.Streams.Streams {
409
output.Streams = append(output.Streams, P2PStreamInfoOutput{
410
HandlerID: strconv.FormatUint(id, 10),
@@ -412,6 +415,7 @@ var p2pStreamLsCmd = &cmds.Command{
415
TargetAddress: s.TargetAddr.String(),
416
})
417
}
418
+ n.P2P.Streams.Unlock()
419
420
res.SetOutput(output)
421
},
@@ -476,15 +480,22 @@ var p2pStreamCloseCmd = &cmds.Command{
480
}
481
}
482
483
+ toClose := make([]*p2p.Stream, 0, 1)
484
+ n.P2P.Streams.Lock()
485
for id, stream := range n.P2P.Streams.Streams {
486
if !closeAll && handlerID != id {
487
continue
488
}
483
- stream.Reset()
489
+ toClose = append(toClose, stream)
490
if !closeAll {
491
break
492
}
493
}
494
+ n.P2P.Streams.Unlock()
495
+
496
+ for _, s := range toClose {
497
+ s.Reset()
498
+ }
499
},
500
}
501
p2p/stream.go
+7
-7
@@ -63,16 +63,16 @@ func (s *Stream) startStreaming() {
63
64
// StreamRegistry is a collection of active incoming and outgoing proto app streams.
65
type StreamRegistry struct {
66
- Streams map[uint64]*Stream
67
- lk sync.Mutex
66
+ sync.Mutex
67
69
- nextID uint64
68
+ Streams map[uint64]*Stream
69
+ nextID uint64
70
}
71
72
// Register registers a stream to the registry
73
func (r *StreamRegistry) Register(streamInfo *Stream) {
74
- r.lk.Lock()
75
- defer r.lk.Unlock()
74
+ r.Lock()
75
+ defer r.Unlock()
76
77
streamInfo.id = r.nextID
78
r.Streams[r.nextID] = streamInfo
@@ -81,8 +81,8 @@ func (r *StreamRegistry) Register(streamInfo *Stream) {
81
82
// Deregister deregisters stream from the registry
83
func (r *StreamRegistry) Deregister(streamID uint64) {
84
- r.lk.Lock()
85
- defer r.lk.Unlock()
84
+ r.Lock()
85
+ defer r.Unlock()
86
87
delete(r.Streams, streamID)
88
}