Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-swarm
Commits
0c51fce0
Commit
0c51fce0
authored
Oct 04, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
switching to separated conn and interface-conn packages
parent
22b1351f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
25 additions
and
25 deletions
+25
-25
limiter.go
limiter.go
+4
-4
limiter_test.go
limiter_test.go
+8
-8
swarm_addr.go
swarm_addr.go
+2
-2
swarm_conn.go
swarm_conn.go
+5
-6
swarm_dial.go
swarm_dial.go
+4
-4
swarm_listen.go
swarm_listen.go
+2
-1
No files found.
limiter.go
View file @
0c51fce0
...
...
@@ -7,11 +7,11 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
type
dialResult
struct
{
Conn
conn
.
Conn
Conn
i
conn
.
Conn
Err
error
}
...
...
@@ -38,14 +38,14 @@ type dialLimiter struct {
fdLimit
int
waitingOnFd
[]
*
dialJob
dialFunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
dialFunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
activePerPeer
map
[
peer
.
ID
]
int
perPeerLimit
int
waitingOnPeerLimit
map
[
peer
.
ID
][]
*
dialJob
}
type
dialfunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
type
dialfunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
func
newDialLimiter
(
df
dialfunc
)
*
dialLimiter
{
return
newDialLimiterWithParams
(
df
,
concurrentFdDials
,
defaultPerPeerRateLimit
)
...
...
limiter_test.go
View file @
0c51fce0
...
...
@@ -10,7 +10,7 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
mafmt
"github.com/whyrusleeping/mafmt"
)
...
...
@@ -55,13 +55,13 @@ func tryDialAddrs(ctx context.Context, l *dialLimiter, p peer.ID, addrs []ma.Mul
}
func
hangDialFunc
(
hang
chan
struct
{})
dialfunc
{
return
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
return
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
if
mafmt
.
UTP
.
Matches
(
a
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
if
tcpPortOver
(
a
,
10
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
<-
hang
...
...
@@ -171,9 +171,9 @@ func TestFDLimiting(t *testing.T) {
func
TestTokenRedistribution
(
t
*
testing
.
T
)
{
hangchs
:=
make
(
map
[
peer
.
ID
]
chan
struct
{})
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
)
(
i
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
10
)
{
return
(
conn
.
Conn
)(
nil
),
nil
return
(
i
conn
.
Conn
)(
nil
),
nil
}
<-
hangchs
[
p
]
...
...
@@ -260,9 +260,9 @@ func TestTokenRedistribution(t *testing.T) {
}
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
)
(
i
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
1000
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
5
+
rand
.
Intn
(
100
)))
...
...
swarm_addr.go
View file @
0c51fce0
...
...
@@ -3,7 +3,7 @@ package swarm
import
(
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
// ListenAddresses returns a list of addresses at which this swarm listens.
...
...
@@ -11,7 +11,7 @@ func (s *Swarm) ListenAddresses() []ma.Multiaddr {
listeners
:=
s
.
swarm
.
Listeners
()
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
listeners
))
for
_
,
l
:=
range
listeners
{
if
l2
,
ok
:=
l
.
NetListener
()
.
(
conn
.
Listener
);
ok
{
if
l2
,
ok
:=
l
.
NetListener
()
.
(
i
conn
.
Listener
);
ok
{
addrs
=
append
(
addrs
,
l2
.
Multiaddr
())
}
}
...
...
swarm_conn.go
View file @
0c51fce0
...
...
@@ -4,13 +4,12 @@ import (
"context"
"fmt"
inet
"github.com/libp2p/go-libp2p-net"
ic
"github.com/ipfs/go-libp2p-crypto"
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
ps
"github.com/jbenet/go-peerstream"
conn
"github.com/libp2p/go-libp2p-conn"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
inet
"github.com/libp2p/go-libp2p-net"
)
// Conn is a simple wrapper around a ps.Conn that also exposes
...
...
@@ -33,12 +32,12 @@ func (c *Conn) StreamConn() *ps.Conn {
return
(
*
ps
.
Conn
)(
c
)
}
func
(
c
*
Conn
)
RawConn
()
conn
.
Conn
{
func
(
c
*
Conn
)
RawConn
()
i
conn
.
Conn
{
// righly panic if these things aren't true. it is an expected
// invariant that these Conns are all of the typewe expect:
// ps.Conn wrapping a conn.Conn
// if we get something else it is programmer error.
return
(
*
ps
.
Conn
)(
c
)
.
NetConn
()
.
(
conn
.
Conn
)
return
(
*
ps
.
Conn
)(
c
)
.
NetConn
()
.
(
i
conn
.
Conn
)
}
func
(
c
*
Conn
)
String
()
string
{
...
...
@@ -94,7 +93,7 @@ func (c *Conn) Close() error {
func
wrapConn
(
psc
*
ps
.
Conn
)
(
*
Conn
,
error
)
{
// grab the underlying connection.
if
_
,
ok
:=
psc
.
NetConn
()
.
(
conn
.
Conn
);
!
ok
{
if
_
,
ok
:=
psc
.
NetConn
()
.
(
i
conn
.
Conn
);
!
ok
{
// this should never happen. if we see it ocurring it means that we added
// a Listener to the ps.Swarm that is NOT one of our net/conn.Listener.
return
nil
,
fmt
.
Errorf
(
"swarm connHandler: invalid conn (not a conn.Conn): %s"
,
psc
)
...
...
swarm_dial.go
View file @
0c51fce0
...
...
@@ -11,7 +11,7 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
// Diagram of dial sync:
...
...
@@ -284,7 +284,7 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
return
swarmC
,
nil
}
func
(
s
*
Swarm
)
dialAddrs
(
ctx
context
.
Context
,
p
peer
.
ID
,
remoteAddrs
<-
chan
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
func
(
s
*
Swarm
)
dialAddrs
(
ctx
context
.
Context
,
p
peer
.
ID
,
remoteAddrs
<-
chan
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
log
.
Debugf
(
"%s swarm dialing %s %s"
,
s
.
local
,
p
,
remoteAddrs
)
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
...
...
@@ -344,7 +344,7 @@ func (s *Swarm) limitedDial(ctx context.Context, p peer.ID, a ma.Multiaddr, resp
})
}
func
(
s
*
Swarm
)
dialAddr
(
ctx
context
.
Context
,
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
func
(
s
*
Swarm
)
dialAddr
(
ctx
context
.
Context
,
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
log
.
Debugf
(
"%s swarm dialing %s %s"
,
s
.
local
,
p
,
addr
)
connC
,
err
:=
s
.
dialer
.
Dial
(
ctx
,
addr
,
p
)
...
...
@@ -376,7 +376,7 @@ var ConnSetupTimeout = time.Minute * 5
// dialConnSetup is the setup logic for a connection from the dial side. it
// needs to add the Conn to the StreamSwarm, then run newConnSetup
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
conn
.
Conn
)
(
*
Conn
,
error
)
{
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
i
conn
.
Conn
)
(
*
Conn
,
error
)
{
deadline
,
ok
:=
ctx
.
Deadline
()
if
!
ok
{
...
...
swarm_listen.go
View file @
0c51fce0
...
...
@@ -8,6 +8,7 @@ import (
ma
"github.com/jbenet/go-multiaddr"
ps
"github.com/jbenet/go-peerstream"
conn
"github.com/libp2p/go-libp2p-conn"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
mconn
"github.com/libp2p/go-libp2p-metrics/conn"
inet
"github.com/libp2p/go-libp2p-net"
transport
"github.com/libp2p/go-libp2p-transport"
...
...
@@ -98,7 +99,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
return
s
.
addConnListener
(
list
)
}
func
(
s
*
Swarm
)
addConnListener
(
list
conn
.
Listener
)
error
{
func
(
s
*
Swarm
)
addConnListener
(
list
i
conn
.
Listener
)
error
{
// AddListener to the peerstream Listener. this will begin accepting connections
// and streams!
sl
,
err
:=
s
.
swarm
.
AddListener
(
list
)
...
...
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