mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
p2p: fix codeclimate warnings
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
0465079f35
commit
3e0184bdad
@ -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()
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user