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
2e742fb9
Commit
2e742fb9
authored
Mar 30, 2021
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix dial_sync tests
parent
201a8d14
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
41 deletions
+79
-41
dial_sync.go
dial_sync.go
+6
-6
dial_sync_test.go
dial_sync_test.go
+54
-16
swarm_dial.go
swarm_dial.go
+19
-19
No files found.
dial_sync.go
View file @
2e742fb9
...
...
@@ -13,7 +13,7 @@ import (
var
errDialCanceled
=
errors
.
New
(
"dial was aborted internally, likely due to https://git.io/Je2wW"
)
// DialFunc is the type of function expected by DialSync.
type
DialWorkerFunc
func
(
context
.
Context
,
peer
.
ID
,
<-
chan
d
ialRequest
)
type
DialWorkerFunc
func
(
context
.
Context
,
peer
.
ID
,
<-
chan
D
ialRequest
)
// NewDialSync constructs a new DialSync
func
NewDialSync
(
worker
DialWorkerFunc
)
*
DialSync
{
...
...
@@ -38,7 +38,7 @@ type activeDial struct {
ctx
context
.
Context
cancel
func
()
reqch
chan
d
ialRequest
reqch
chan
D
ialRequest
ds
*
DialSync
}
...
...
@@ -68,16 +68,16 @@ func (ad *activeDial) dial(ctx context.Context, p peer.ID) (*Conn, error) {
dialCtx
=
network
.
WithSimultaneousConnect
(
dialCtx
,
reason
)
}
resch
:=
make
(
chan
d
ialResponse
,
1
)
resch
:=
make
(
chan
D
ialResponse
,
1
)
select
{
case
ad
.
reqch
<-
d
ialRequest
{
c
tx
:
dialCtx
,
r
esch
:
resch
}
:
case
ad
.
reqch
<-
D
ialRequest
{
C
tx
:
dialCtx
,
R
esch
:
resch
}
:
case
<-
ctx
.
Done
()
:
return
nil
,
ctx
.
Err
()
}
select
{
case
res
:=
<-
resch
:
return
res
.
c
onn
,
res
.
e
rr
return
res
.
C
onn
,
res
.
E
rr
case
<-
ctx
.
Done
()
:
return
nil
,
ctx
.
Err
()
}
...
...
@@ -98,7 +98,7 @@ func (ds *DialSync) getActiveDial(p peer.ID) *activeDial {
id
:
p
,
ctx
:
adctx
,
cancel
:
cancel
,
reqch
:
make
(
chan
d
ialRequest
),
reqch
:
make
(
chan
D
ialRequest
),
ds
:
ds
,
}
ds
.
dials
[
p
]
=
actd
...
...
dial_sync_test.go
View file @
2e742fb9
...
...
@@ -12,19 +12,33 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)
func
getMockDialFunc
()
(
DialFunc
,
func
(),
context
.
Context
,
<-
chan
struct
{})
{
func
getMockDialFunc
()
(
Dial
Worker
Func
,
func
(),
context
.
Context
,
<-
chan
struct
{})
{
dfcalls
:=
make
(
chan
struct
{},
512
)
// buffer it large enough that we won't care
dialctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
ch
:=
make
(
chan
struct
{})
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
*
Conn
,
error
)
{
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
DialRequest
)
{
dfcalls
<-
struct
{}{}
defer
cancel
()
select
{
case
<-
ch
:
return
new
(
Conn
),
nil
case
<-
ctx
.
Done
()
:
return
nil
,
ctx
.
Err
()
}
go
func
()
{
defer
cancel
()
for
{
select
{
case
req
,
ok
:=
<-
reqch
:
if
!
ok
{
return
}
select
{
case
<-
ch
:
req
.
Resch
<-
DialResponse
{
Conn
:
new
(
Conn
)}
case
<-
ctx
.
Done
()
:
req
.
Resch
<-
DialResponse
{
Err
:
ctx
.
Err
()}
return
}
case
<-
ctx
.
Done
()
:
return
}
}
}()
}
o
:=
new
(
sync
.
Once
)
...
...
@@ -174,12 +188,25 @@ func TestDialSyncAllCancel(t *testing.T) {
func
TestFailFirst
(
t
*
testing
.
T
)
{
var
count
int
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
*
Conn
,
error
)
{
if
count
>
0
{
return
new
(
Conn
),
nil
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
DialRequest
)
{
for
{
select
{
case
req
,
ok
:=
<-
reqch
:
if
!
ok
{
return
}
if
count
>
0
{
req
.
Resch
<-
DialResponse
{
Conn
:
new
(
Conn
)}
}
else
{
req
.
Resch
<-
DialResponse
{
Err
:
fmt
.
Errorf
(
"gophers ate the modem"
)}
}
count
++
case
<-
ctx
.
Done
()
:
return
}
}
count
++
return
nil
,
fmt
.
Errorf
(
"gophers ate the modem"
)
}
ds
:=
NewDialSync
(
f
)
...
...
@@ -205,8 +232,19 @@ func TestFailFirst(t *testing.T) {
}
func
TestStressActiveDial
(
t
*
testing
.
T
)
{
ds
:=
NewDialSync
(
func
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
*
Conn
,
error
)
{
return
nil
,
nil
ds
:=
NewDialSync
(
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
DialRequest
)
{
for
{
select
{
case
req
,
ok
:=
<-
reqch
:
if
!
ok
{
return
}
req
.
Resch
<-
DialResponse
{}
case
<-
ctx
.
Done
()
:
return
}
}
})
wg
:=
sync
.
WaitGroup
{}
...
...
swarm_dial.go
View file @
2e742fb9
...
...
@@ -290,14 +290,14 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) {
// lo and behold, The Dialer
// TODO explain how all this works
//////////////////////////////////////////////////////////////////////////////////
type
d
ialRequest
struct
{
c
tx
context
.
Context
r
esch
chan
d
ialResponse
type
D
ialRequest
struct
{
C
tx
context
.
Context
R
esch
chan
D
ialResponse
}
type
d
ialResponse
struct
{
c
onn
*
Conn
e
rr
error
type
D
ialResponse
struct
{
C
onn
*
Conn
E
rr
error
}
type
dialComplete
struct
{
...
...
@@ -307,7 +307,7 @@ type dialComplete struct {
}
// dialWorker is an active dial goroutine that synchronizes and executes concurrent dials
func
(
s
*
Swarm
)
dialWorker
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
{
func
(
s
*
Swarm
)
dialWorker
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
D
ialRequest
)
{
if
p
==
s
.
local
{
for
{
select
{
...
...
@@ -316,7 +316,7 @@ func (s *Swarm) dialWorker(ctx context.Context, p peer.ID, reqch <-chan dialRequ
return
}
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
ErrDialToSelf
}
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
ErrDialToSelf
}
}
}
}
...
...
@@ -324,11 +324,11 @@ func (s *Swarm) dialWorker(ctx context.Context, p peer.ID, reqch <-chan dialRequ
s
.
dialWorkerLoop
(
ctx
,
p
,
reqch
)
}
func
(
s
*
Swarm
)
dialWorkerLoop
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
{
func
(
s
*
Swarm
)
dialWorkerLoop
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
D
ialRequest
)
{
defer
s
.
limiter
.
clearAllPeerDials
(
p
)
type
pendRequest
struct
{
req
d
ialRequest
// the original request
req
D
ialRequest
// the original request
err
*
DialError
// dial error accumulator
addrs
map
[
ma
.
Multiaddr
]
struct
{}
// pending addr dials
}
...
...
@@ -368,15 +368,15 @@ loop:
return
}
c
:=
s
.
bestAcceptableConnToPeer
(
req
.
c
tx
,
p
)
c
:=
s
.
bestAcceptableConnToPeer
(
req
.
C
tx
,
p
)
if
c
!=
nil
{
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
c
}
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
c
}
continue
loop
}
addrs
,
err
:=
s
.
addrsForDial
(
req
.
c
tx
,
p
)
addrs
,
err
:=
s
.
addrsForDial
(
req
.
C
tx
,
p
)
if
err
!=
nil
{
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
err
}
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
err
}
continue
loop
}
...
...
@@ -408,7 +408,7 @@ loop:
if
ad
.
conn
!=
nil
{
// dial to this addr was successful, complete the request
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
ad
.
conn
}
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
ad
.
conn
}
continue
loop
}
...
...
@@ -424,7 +424,7 @@ loop:
if
len
(
todial
)
==
0
&&
len
(
tojoin
)
==
0
{
// all request applicable addrs have been dialed, we must have errored
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
pr
.
err
}
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
pr
.
err
}
continue
loop
}
...
...
@@ -438,7 +438,7 @@ loop:
if
len
(
todial
)
>
0
{
for
_
,
a
:=
range
todial
{
pending
[
a
]
=
&
addrDial
{
ctx
:
req
.
c
tx
,
requests
:
[]
int
{
reqno
}}
pending
[
a
]
=
&
addrDial
{
ctx
:
req
.
C
tx
,
requests
:
[]
int
{
reqno
}}
}
nextDial
=
append
(
nextDial
,
todial
...
)
...
...
@@ -492,7 +492,7 @@ loop:
continue
}
pr
.
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
res
.
conn
}
pr
.
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
res
.
conn
}
delete
(
requests
,
reqno
)
}
...
...
@@ -513,7 +513,7 @@ loop:
delete
(
pr
.
addrs
,
res
.
addr
)
if
len
(
pr
.
addrs
)
==
0
{
// all addrs have erred, dispatch dial error
pr
.
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
pr
.
err
}
pr
.
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
pr
.
err
}
delete
(
requests
,
reqno
)
}
}
...
...
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