From 768d2a0f4e67e5fc777e44935e1a842ed8595a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 26 May 2018 16:32:18 +0200 Subject: [PATCH] p2p: fix codeclimate warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/commands/p2p.go | 12 ++++++------ p2p/listener.go | 5 +++-- p2p/local.go | 4 ++-- p2p/remote.go | 2 +- p2p/stream.go | 20 ++++++++++---------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/core/commands/p2p.go b/core/commands/p2p.go index 0286b6f6d51..a3275950764 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -322,9 +322,9 @@ var p2pStreamLsCmd = &cmds.Command{ output := &P2PStreamsOutput{} - for _, s := range n.P2P.Streams.Streams { + for id, s := range n.P2P.Streams.Streams { output.Streams = append(output.Streams, P2PStreamInfoOutput{ - HandlerID: strconv.FormatUint(s.Id, 10), + HandlerID: strconv.FormatUint(id, 10), Protocol: s.Protocol, @@ -366,7 +366,7 @@ var p2pStreamCloseCmd = &cmds.Command{ Tagline: "Close active p2p stream.", }, Arguments: []cmdkit.Argument{ - cmdkit.StringArg("Id", false, false, "Stream Id"), + cmdkit.StringArg("id", false, false, "Stream identifier"), }, Options: []cmdkit.Option{ cmdkit.BoolOption("all", "a", "Close all streams."), @@ -385,7 +385,7 @@ var p2pStreamCloseCmd = &cmds.Command{ if !closeAll { if len(req.Arguments()) == 0 { - res.SetError(errors.New("no Id specified"), cmdkit.ErrNormal) + res.SetError(errors.New("no id specified"), cmdkit.ErrNormal) return } @@ -396,8 +396,8 @@ var p2pStreamCloseCmd = &cmds.Command{ } } - for _, stream := range n.P2P.Streams.Streams { - if !closeAll && handlerID != stream.Id { + for id, stream := range n.P2P.Streams.Streams { + if !closeAll && handlerID != id { continue } stream.Reset() diff --git a/p2p/listener.go b/p2p/listener.go index 1f6e16a3371..37ac5d8562f 100644 --- a/p2p/listener.go +++ b/p2p/listener.go @@ -5,6 +5,7 @@ import ( "sync" ) +// Listener listens for connections and proxies them to a target type Listener interface { Protocol() string ListenAddress() string @@ -26,7 +27,7 @@ type ListenerRegistry struct { lk *sync.Mutex } -func (r *ListenerRegistry) Lock(l Listener) error { +func (r *ListenerRegistry) lock(l Listener) error { r.lk.Lock() if _, ok := r.Listeners[getListenerKey(l)]; ok { @@ -36,7 +37,7 @@ func (r *ListenerRegistry) Lock(l Listener) error { return nil } -func (r *ListenerRegistry) Unlock() { +func (r *ListenerRegistry) unlock() { r.lk.Unlock() } diff --git a/p2p/local.go b/p2p/local.go index 69bb443f6f1..127bc997343 100644 --- a/p2p/local.go +++ b/p2p/local.go @@ -39,13 +39,13 @@ func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto string, bi peer: peer, } - if err := p2p.Listeners.Lock(listener); err != nil { + if err := p2p.Listeners.lock(listener); err != nil { return nil, err } maListener, err := manet.Listen(bindAddr) if err != nil { - p2p.Listeners.Unlock() + p2p.Listeners.unlock() return nil, err } diff --git a/p2p/remote.go b/p2p/remote.go index 050ac5cd277..3f6be40c38f 100644 --- a/p2p/remote.go +++ b/p2p/remote.go @@ -29,7 +29,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad addr: addr, } - if err := p2p.Listeners.Lock(listener); err != nil { + if err := p2p.Listeners.lock(listener); err != nil { return nil, err } diff --git a/p2p/stream.go b/p2p/stream.go index bc7096d1412..b8e2d9567e1 100644 --- a/p2p/stream.go +++ b/p2p/stream.go @@ -11,7 +11,7 @@ import ( // Stream holds information on active incoming and outgoing p2p streams. type Stream struct { - Id uint64 + id uint64 Protocol string @@ -28,15 +28,15 @@ type Stream struct { func (s *Stream) Close() error { s.Local.Close() s.Remote.Close() - s.Registry.Deregister(s.Id) + s.Registry.Deregister(s.id) return nil } -// Rest closes stream endpoints and deregisters it +// Reset closes stream endpoints and deregisters it func (s *Stream) Reset() error { s.Local.Close() s.Remote.Reset() - s.Registry.Deregister(s.Id) + s.Registry.Deregister(s.id) return nil } @@ -61,7 +61,7 @@ type StreamRegistry struct { Streams map[uint64]*Stream lk *sync.Mutex - nextId uint64 + nextID uint64 } // Register registers a stream to the registry @@ -69,15 +69,15 @@ func (r *StreamRegistry) Register(streamInfo *Stream) { r.lk.Lock() defer r.lk.Unlock() - streamInfo.Id = r.nextId - r.Streams[r.nextId] = streamInfo - r.nextId++ + streamInfo.id = r.nextID + r.Streams[r.nextID] = streamInfo + r.nextID++ } // Deregister deregisters stream from the registry -func (r *StreamRegistry) Deregister(streamId uint64) { +func (r *StreamRegistry) Deregister(streamID uint64) { r.lk.Lock() defer r.lk.Unlock() - delete(r.Streams, streamId) + delete(r.Streams, streamID) }