Skip to content

Commit

Permalink
p2p: fix codeclimate warnings
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed May 26, 2018
1 parent 08ae673 commit 768d2a0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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."),
Expand All @@ -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
}

Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions p2p/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
)

// Listener listens for connections and proxies them to a target
type Listener interface {
Protocol() string
ListenAddress() string
Expand All @@ -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 {
Expand All @@ -36,7 +37,7 @@ func (r *ListenerRegistry) Lock(l Listener) error {
return nil
}

func (r *ListenerRegistry) Unlock() {
func (r *ListenerRegistry) unlock() {
r.lk.Unlock()
}

Expand Down
4 changes: 2 additions & 2 deletions p2p/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions p2p/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Stream holds information on active incoming and outgoing p2p streams.
type Stream struct {
Id uint64
id uint64

Protocol string

Expand All @@ -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
}

Expand All @@ -61,23 +61,23 @@ type StreamRegistry struct {
Streams map[uint64]*Stream
lk *sync.Mutex

nextId uint64
nextID uint64
}

// Register registers a stream to the registry
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)
}

0 comments on commit 768d2a0

Please sign in to comment.