Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
d8468400
Commit
d8468400
authored
Aug 17, 2016
by
Jeromy Johnson
Committed by
GitHub
Aug 17, 2016
Browse files
Merge pull request #80 from libp2p/lint/fix-some-linting
lint: fixed a bunch of issues reported by gometalinter
parents
3c597252
e90e0170
Changes
7
Hide whitespace changes
Inline
Side-by-side
p2p/net/swarm/limiter_test.go
View file @
d8468400
...
@@ -63,10 +63,10 @@ func hangDialFunc(hang chan struct{}) dialfunc {
...
@@ -63,10 +63,10 @@ func hangDialFunc(hang chan struct{}) dialfunc {
if
tcpPortOver
(
a
,
10
)
{
if
tcpPortOver
(
a
,
10
)
{
return
conn
.
Conn
(
nil
),
nil
return
conn
.
Conn
(
nil
),
nil
}
else
{
<-
hang
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
<-
hang
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
}
}
...
@@ -127,7 +127,7 @@ func TestFDLimiting(t *testing.T) {
...
@@ -127,7 +127,7 @@ func TestFDLimiting(t *testing.T) {
bads
:=
[]
ma
.
Multiaddr
{
addrWithPort
(
t
,
1
),
addrWithPort
(
t
,
2
),
addrWithPort
(
t
,
3
),
addrWithPort
(
t
,
4
)}
bads
:=
[]
ma
.
Multiaddr
{
addrWithPort
(
t
,
1
),
addrWithPort
(
t
,
2
),
addrWithPort
(
t
,
3
),
addrWithPort
(
t
,
4
)}
pids
:=
[]
peer
.
ID
{
"testpeer1"
,
"testpeer2"
,
"testpeer3"
,
"testpeer4"
}
pids
:=
[]
peer
.
ID
{
"testpeer1"
,
"testpeer2"
,
"testpeer3"
,
"testpeer4"
}
good
_tcp
:=
addrWithPort
(
t
,
20
)
good
TCP
:=
addrWithPort
(
t
,
20
)
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
resch
:=
make
(
chan
dialResult
)
resch
:=
make
(
chan
dialResult
)
...
@@ -143,7 +143,7 @@ func TestFDLimiting(t *testing.T) {
...
@@ -143,7 +143,7 @@ func TestFDLimiting(t *testing.T) {
l
.
AddDialJob
(
&
dialJob
{
l
.
AddDialJob
(
&
dialJob
{
ctx
:
ctx
,
ctx
:
ctx
,
peer
:
pid
,
peer
:
pid
,
addr
:
good
_tcp
,
addr
:
good
TCP
,
resp
:
resch
,
resp
:
resch
,
})
})
}
}
...
@@ -175,10 +175,10 @@ func TestTokenRedistribution(t *testing.T) {
...
@@ -175,10 +175,10 @@ func TestTokenRedistribution(t *testing.T) {
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
10
)
{
if
tcpPortOver
(
a
,
10
)
{
return
(
conn
.
Conn
)(
nil
),
nil
return
(
conn
.
Conn
)(
nil
),
nil
}
else
{
<-
hangchs
[
p
]
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
<-
hangchs
[
p
]
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
l
:=
newDialLimiterWithParams
(
df
,
8
,
4
)
l
:=
newDialLimiterWithParams
(
df
,
8
,
4
)
...
@@ -264,10 +264,10 @@ func TestStressLimiter(t *testing.T) {
...
@@ -264,10 +264,10 @@ func TestStressLimiter(t *testing.T) {
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
1000
)
{
if
tcpPortOver
(
a
,
1000
)
{
return
conn
.
Conn
(
nil
),
nil
return
conn
.
Conn
(
nil
),
nil
}
else
{
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
5
+
rand
.
Intn
(
100
)))
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
5
+
rand
.
Intn
(
100
)))
return
nil
,
fmt
.
Errorf
(
"test bad dial"
)
}
}
l
:=
newDialLimiterWithParams
(
df
,
20
,
5
)
l
:=
newDialLimiterWithParams
(
df
,
20
,
5
)
...
...
p2p/net/swarm/swarm.go
View file @
d8468400
//
p
ackage swarm implements a connection muxer with a pair of channels
//
P
ackage swarm implements a connection muxer with a pair of channels
// to synchronize all network communication.
// to synchronize all network communication.
package
swarm
package
swarm
...
@@ -34,6 +34,8 @@ import (
...
@@ -34,6 +34,8 @@ import (
var
log
=
logging
.
Logger
(
"swarm2"
)
var
log
=
logging
.
Logger
(
"swarm2"
)
// PSTransport is the default peerstream transport that will be used by
// any libp2p swarms.
var
PSTransport
pst
.
Transport
var
PSTransport
pst
.
Transport
func
init
()
{
func
init
()
{
...
@@ -143,6 +145,8 @@ func (s *Swarm) teardown() error {
...
@@ -143,6 +145,8 @@ func (s *Swarm) teardown() error {
return
s
.
swarm
.
Close
()
return
s
.
swarm
.
Close
()
}
}
// AddAddrFilter adds a multiaddr filter to the set of filters the swarm will
// use to determine which addresses not to dial to.
func
(
s
*
Swarm
)
AddAddrFilter
(
f
string
)
error
{
func
(
s
*
Swarm
)
AddAddrFilter
(
f
string
)
error
{
m
,
err
:=
mafilter
.
NewMask
(
f
)
m
,
err
:=
mafilter
.
NewMask
(
f
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -165,6 +169,7 @@ func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
...
@@ -165,6 +169,7 @@ func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
return
listenAddrs
,
nil
return
listenAddrs
,
nil
}
}
// Listen sets up listeners for all of the given addresses
func
(
s
*
Swarm
)
Listen
(
addrs
...
ma
.
Multiaddr
)
error
{
func
(
s
*
Swarm
)
Listen
(
addrs
...
ma
.
Multiaddr
)
error
{
addrs
,
err
:=
filterAddrs
(
addrs
)
addrs
,
err
:=
filterAddrs
(
addrs
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -286,6 +291,7 @@ func (s *Swarm) LocalPeer() peer.ID {
...
@@ -286,6 +291,7 @@ func (s *Swarm) LocalPeer() peer.ID {
return
s
.
local
return
s
.
local
}
}
// Backoff returns the dialbackoff object for this swarm.
func
(
s
*
Swarm
)
Backoff
()
*
dialbackoff
{
func
(
s
*
Swarm
)
Backoff
()
*
dialbackoff
{
return
&
s
.
backf
return
&
s
.
backf
}
}
...
...
p2p/net/swarm/swarm_addr_test.go
View file @
d8468400
...
@@ -113,7 +113,7 @@ func TestDialBadAddrs(t *testing.T) {
...
@@ -113,7 +113,7 @@ func TestDialBadAddrs(t *testing.T) {
p
:=
testutil
.
RandPeerIDFatal
(
t
)
p
:=
testutil
.
RandPeerIDFatal
(
t
)
s
.
peers
.
AddAddr
(
p
,
a
,
pstore
.
PermanentAddrTTL
)
s
.
peers
.
AddAddr
(
p
,
a
,
pstore
.
PermanentAddrTTL
)
if
_
,
err
:=
s
.
Dial
(
ctx
,
p
);
err
==
nil
{
if
_
,
err
:=
s
.
Dial
(
ctx
,
p
);
err
==
nil
{
t
.
Error
(
"swarm should not dial: %s"
,
m
)
t
.
Error
f
(
"swarm should not dial: %s"
,
p
)
}
}
}
}
...
...
p2p/net/swarm/swarm_conn.go
View file @
d8468400
...
@@ -13,7 +13,7 @@ import (
...
@@ -13,7 +13,7 @@ import (
context
"golang.org/x/net/context"
context
"golang.org/x/net/context"
)
)
//
a
Conn is a simple wrapper around a ps.Conn that also exposes
// Conn is a simple wrapper around a ps.Conn that also exposes
// some of the methods from the underlying conn.Conn.
// some of the methods from the underlying conn.Conn.
// There's **five** "layers" to each connection:
// There's **five** "layers" to each connection:
// * 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
// * 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
...
@@ -87,6 +87,7 @@ func (c *Conn) NewStream() (inet.Stream, error) {
...
@@ -87,6 +87,7 @@ func (c *Conn) NewStream() (inet.Stream, error) {
return
inet
.
Stream
(
s
),
err
return
inet
.
Stream
(
s
),
err
}
}
// Close closes the underlying stream connection
func
(
c
*
Conn
)
Close
()
error
{
func
(
c
*
Conn
)
Close
()
error
{
return
c
.
StreamConn
()
.
Close
()
return
c
.
StreamConn
()
.
Close
()
}
}
...
...
p2p/net/swarm/swarm_dial.go
View file @
d8468400
...
@@ -26,9 +26,15 @@ import (
...
@@ -26,9 +26,15 @@ import (
// retry dialAttempt x
// retry dialAttempt x
var
(
var
(
// ErrDialBackoff is returned by the backoff code when a given peer has
// been dialed too frequently
ErrDialBackoff
=
errors
.
New
(
"dial backoff"
)
ErrDialBackoff
=
errors
.
New
(
"dial backoff"
)
ErrDialFailed
=
errors
.
New
(
"dial attempt failed"
)
ErrDialToSelf
=
errors
.
New
(
"dial to self attempted"
)
// ErrDialFailed is returned when connecting to a peer has ultimately failed
ErrDialFailed
=
errors
.
New
(
"dial attempt failed"
)
// ErrDialToSelf is returned if we attempt to dial our own peer
ErrDialToSelf
=
errors
.
New
(
"dial to self attempted"
)
)
)
// dialAttempts governs how many times a goroutine will try to dial a given peer.
// dialAttempts governs how many times a goroutine will try to dial a given peer.
...
@@ -45,7 +51,7 @@ const defaultPerPeerRateLimit = 8
...
@@ -45,7 +51,7 @@ const defaultPerPeerRateLimit = 8
// DialTimeout is the amount of time each dial attempt has. We can think about making
// DialTimeout is the amount of time each dial attempt has. We can think about making
// this larger down the road, or putting more granular timeouts (i.e. within each
// this larger down the road, or putting more granular timeouts (i.e. within each
// subcomponent of Dial)
// subcomponent of Dial)
var
DialTimeout
time
.
Duration
=
time
.
Second
*
10
var
DialTimeout
=
time
.
Second
*
10
// dialsync is a small object that helps manage ongoing dials.
// dialsync is a small object that helps manage ongoing dials.
// this way, if we receive many simultaneous dial requests, one
// this way, if we receive many simultaneous dial requests, one
...
@@ -320,13 +326,13 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
...
@@ -320,13 +326,13 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
}
}
ila
,
_
:=
s
.
InterfaceListenAddresses
()
ila
,
_
:=
s
.
InterfaceListenAddresses
()
subtract
_f
ilter
:=
addrutil
.
SubtractFilter
(
append
(
ila
,
s
.
peers
.
Addrs
(
s
.
local
)
...
)
...
)
subtract
F
ilter
:=
addrutil
.
SubtractFilter
(
append
(
ila
,
s
.
peers
.
Addrs
(
s
.
local
)
...
)
...
)
// get live channel of addresses for peer, filtered by the given filters
// get live channel of addresses for peer, filtered by the given filters
/*
/*
remoteAddrChan := s.peers.AddrsChan(ctx, p,
remoteAddrChan := s.peers.AddrsChan(ctx, p,
addrutil.AddrUsableFilter,
addrutil.AddrUsableFilter,
subtract
_f
ilter,
subtract
F
ilter,
s.Filters.AddrBlocked)
s.Filters.AddrBlocked)
*/
*/
...
@@ -339,13 +345,13 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
...
@@ -339,13 +345,13 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
that we previously had (halting a dial when we run out of addrs)
that we previously had (halting a dial when we run out of addrs)
*/
*/
paddrs
:=
s
.
peers
.
Addrs
(
p
)
paddrs
:=
s
.
peers
.
Addrs
(
p
)
good
_a
ddrs
:=
addrutil
.
FilterAddrs
(
paddrs
,
good
A
ddrs
:=
addrutil
.
FilterAddrs
(
paddrs
,
addrutil
.
AddrUsableFunc
,
addrutil
.
AddrUsableFunc
,
subtract
_f
ilter
,
subtract
F
ilter
,
addrutil
.
FilterNeg
(
s
.
Filters
.
AddrBlocked
),
addrutil
.
FilterNeg
(
s
.
Filters
.
AddrBlocked
),
)
)
remoteAddrChan
:=
make
(
chan
ma
.
Multiaddr
,
len
(
good
_a
ddrs
))
remoteAddrChan
:=
make
(
chan
ma
.
Multiaddr
,
len
(
good
A
ddrs
))
for
_
,
a
:=
range
good
_a
ddrs
{
for
_
,
a
:=
range
good
A
ddrs
{
remoteAddrChan
<-
a
remoteAddrChan
<-
a
}
}
close
(
remoteAddrChan
)
close
(
remoteAddrChan
)
...
...
p2p/net/swarm/swarm_net.go
View file @
d8468400
...
@@ -63,7 +63,7 @@ func (n *Network) Peers() []peer.ID {
...
@@ -63,7 +63,7 @@ func (n *Network) Peers() []peer.ID {
return
n
.
Swarm
()
.
Peers
()
return
n
.
Swarm
()
.
Peers
()
}
}
// Peers returns the Peerstore, which tracks known peers
// Peers
tore
returns the Peerstore, which tracks known peers
func
(
n
*
Network
)
Peerstore
()
pstore
.
Peerstore
{
func
(
n
*
Network
)
Peerstore
()
pstore
.
Peerstore
{
return
n
.
Swarm
()
.
peers
return
n
.
Swarm
()
.
peers
}
}
...
@@ -142,7 +142,7 @@ func (n *Network) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error)
...
@@ -142,7 +142,7 @@ func (n *Network) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error)
return
inet
.
Stream
(
s
),
nil
return
inet
.
Stream
(
s
),
nil
}
}
// SetHandler sets the protocol handler on the Network's Muxer.
// Set
Stream
Handler sets the protocol handler on the Network's Muxer.
// This operation is threadsafe.
// This operation is threadsafe.
func
(
n
*
Network
)
SetStreamHandler
(
h
inet
.
StreamHandler
)
{
func
(
n
*
Network
)
SetStreamHandler
(
h
inet
.
StreamHandler
)
{
n
.
Swarm
()
.
SetStreamHandler
(
h
)
n
.
Swarm
()
.
SetStreamHandler
(
h
)
...
...
p2p/net/swarm/swarm_stream.go
View file @
d8468400
...
@@ -6,7 +6,7 @@ import (
...
@@ -6,7 +6,7 @@ import (
ps
"github.com/jbenet/go-peerstream"
ps
"github.com/jbenet/go-peerstream"
)
)
//
a
Stream is a wrapper around a ps.Stream that exposes a way to get
// Stream is a wrapper around a ps.Stream that exposes a way to get
// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
type
Stream
struct
{
type
Stream
struct
{
stream
*
ps
.
Stream
stream
*
ps
.
Stream
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment