@cryptotaxi247 / kubo / commits / 3e0184bda

p2p: fix codeclimate warnings

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed May 26, 2018 at 16:32 UTC 3e0184bdad2dd4a3bc678701553d5f23bfa48f3c
5 files changed +22 -21
core/commands/p2p.go
+6 -6
@@ -322,9 +322,9 @@ var p2pStreamLsCmd = &cmds.Command{
322
323 output := &P2PStreamsOutput{}
324
325 - for _, s := range n.P2P.Streams.Streams {
325 + for id, s := range n.P2P.Streams.Streams {
326 output.Streams = append(output.Streams, P2PStreamInfoOutput{
327 - HandlerID: strconv.FormatUint(s.Id, 10),
327 + HandlerID: strconv.FormatUint(id, 10),
328
329 Protocol: s.Protocol,
330
@@ -366,7 +366,7 @@ var p2pStreamCloseCmd = &cmds.Command{
366 Tagline: "Close active p2p stream.",
367 },
368 Arguments: []cmdkit.Argument{
369 - cmdkit.StringArg("Id", false, false, "Stream Id"),
369 + cmdkit.StringArg("id", false, false, "Stream identifier"),
370 },
371 Options: []cmdkit.Option{
372 cmdkit.BoolOption("all", "a", "Close all streams."),
@@ -385,7 +385,7 @@ var p2pStreamCloseCmd = &cmds.Command{
385
386 if !closeAll {
387 if len(req.Arguments()) == 0 {
388 - res.SetError(errors.New("no Id specified"), cmdkit.ErrNormal)
388 + res.SetError(errors.New("no id specified"), cmdkit.ErrNormal)
389 return
390 }
391
@@ -396,8 +396,8 @@ var p2pStreamCloseCmd = &cmds.Command{
396 }
397 }
398
399 - for _, stream := range n.P2P.Streams.Streams {
400 - if !closeAll && handlerID != stream.Id {
399 + for id, stream := range n.P2P.Streams.Streams {
400 + if !closeAll && handlerID != id {
401 continue
402 }
403 stream.Reset()
p2p/listener.go
+3 -2
@@ -5,6 +5,7 @@ import (
5 "sync"
6 )
7
8 +// Listener listens for connections and proxies them to a target
9 type Listener interface {
10 Protocol() string
11 ListenAddress() string
@@ -26,7 +27,7 @@ type ListenerRegistry struct {
27 lk *sync.Mutex
28 }
29
29 -func (r *ListenerRegistry) Lock(l Listener) error {
30 +func (r *ListenerRegistry) lock(l Listener) error {
31 r.lk.Lock()
32
33 if _, ok := r.Listeners[getListenerKey(l)]; ok {
@@ -36,7 +37,7 @@ func (r *ListenerRegistry) Lock(l Listener) error {
37 return nil
38 }
39
39 -func (r *ListenerRegistry) Unlock() {
40 +func (r *ListenerRegistry) unlock() {
41 r.lk.Unlock()
42 }
43
p2p/local.go
+2 -2
@@ -39,13 +39,13 @@ func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto string, bi
39 peer: peer,
40 }
41
42 - if err := p2p.Listeners.Lock(listener); err != nil {
42 + if err := p2p.Listeners.lock(listener); err != nil {
43 return nil, err
44 }
45
46 maListener, err := manet.Listen(bindAddr)
47 if err != nil {
48 - p2p.Listeners.Unlock()
48 + p2p.Listeners.unlock()
49 return nil, err
50 }
51
p2p/remote.go
+1 -1
@@ -29,7 +29,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
29 addr: addr,
30 }
31
32 - if err := p2p.Listeners.Lock(listener); err != nil {
32 + if err := p2p.Listeners.lock(listener); err != nil {
33 return nil, err
34 }
35
p2p/stream.go
+10 -10
@@ -11,7 +11,7 @@ import (
11
12 // Stream holds information on active incoming and outgoing p2p streams.
13 type Stream struct {
14 - Id uint64
14 + id uint64
15
16 Protocol string
17
@@ -28,15 +28,15 @@ type Stream struct {
28 func (s *Stream) Close() error {
29 s.Local.Close()
30 s.Remote.Close()
31 - s.Registry.Deregister(s.Id)
31 + s.Registry.Deregister(s.id)
32 return nil
33 }
34
35 -// Rest closes stream endpoints and deregisters it
35 +// Reset closes stream endpoints and deregisters it
36 func (s *Stream) Reset() error {
37 s.Local.Close()
38 s.Remote.Reset()
39 - s.Registry.Deregister(s.Id)
39 + s.Registry.Deregister(s.id)
40 return nil
41 }
42
@@ -61,7 +61,7 @@ type StreamRegistry struct {
61 Streams map[uint64]*Stream
62 lk *sync.Mutex
63
64 - nextId uint64
64 + nextID uint64
65 }
66
67 // Register registers a stream to the registry
@@ -69,15 +69,15 @@ func (r *StreamRegistry) Register(streamInfo *Stream) {
69 r.lk.Lock()
70 defer r.lk.Unlock()
71
72 - streamInfo.Id = r.nextId
73 - r.Streams[r.nextId] = streamInfo
74 - r.nextId++
72 + streamInfo.id = r.nextID
73 + r.Streams[r.nextID] = streamInfo
74 + r.nextID++
75 }
76
77 // Deregister deregisters stream from the registry
78 -func (r *StreamRegistry) Deregister(streamId uint64) {
78 +func (r *StreamRegistry) Deregister(streamID uint64) {
79 r.lk.Lock()
80 defer r.lk.Unlock()
81
82 - delete(r.Streams, streamId)
82 + delete(r.Streams, streamID)
83 }