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
16248280
Commit
16248280
authored
Apr 01, 2021
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make DialRequest and DialResponse private
parent
27f6c394
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
39 deletions
+37
-39
dial_sync.go
dial_sync.go
+7
-7
dial_sync_test.go
dial_sync_test.go
+9
-11
swarm_dial.go
swarm_dial.go
+21
-21
No files found.
dial_sync.go
View file @
16248280
...
...
@@ -12,8 +12,8 @@ import (
// TODO: change this text when we fix the bug
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
)
error
// Dial
Worer
Func is
used by DialSync to spawn a new dial worker
type
DialWorkerFunc
func
(
context
.
Context
,
peer
.
ID
,
<-
chan
d
ialRequest
)
error
// 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
}
...
...
@@ -64,16 +64,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
()
}
...
...
@@ -94,7 +94,7 @@ func (ds *DialSync) getActiveDial(p peer.ID) (*activeDial, error) {
id
:
p
,
ctx
:
adctx
,
cancel
:
cancel
,
reqch
:
make
(
chan
D
ialRequest
),
reqch
:
make
(
chan
d
ialRequest
),
ds
:
ds
,
}
...
...
dial_sync_test.go
View file @
16248280
package
swarm
_test
package
swarm
import
(
"context"
...
...
@@ -7,8 +7,6 @@ import (
"testing"
"time"
.
"github.com/libp2p/go-libp2p-swarm"
"github.com/libp2p/go-libp2p-core/peer"
)
...
...
@@ -16,7 +14,7 @@ func getMockDialFunc() (DialWorkerFunc, 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
,
reqch
<-
chan
D
ialRequest
)
error
{
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
error
{
dfcalls
<-
struct
{}{}
go
func
()
{
defer
cancel
()
...
...
@@ -29,9 +27,9 @@ func getMockDialFunc() (DialWorkerFunc, func(), context.Context, <-chan struct{}
select
{
case
<-
ch
:
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
new
(
Conn
)}
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
new
(
Conn
)}
case
<-
ctx
.
Done
()
:
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
ctx
.
Err
()}
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
ctx
.
Err
()}
return
}
case
<-
ctx
.
Done
()
:
...
...
@@ -189,7 +187,7 @@ func TestDialSyncAllCancel(t *testing.T) {
func
TestFailFirst
(
t
*
testing
.
T
)
{
var
count
int
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
D
ialRequest
)
error
{
f
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
error
{
go
func
()
{
for
{
select
{
...
...
@@ -199,9 +197,9 @@ func TestFailFirst(t *testing.T) {
}
if
count
>
0
{
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
new
(
Conn
)}
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
new
(
Conn
)}
}
else
{
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
fmt
.
Errorf
(
"gophers ate the modem"
)}
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
fmt
.
Errorf
(
"gophers ate the modem"
)}
}
count
++
...
...
@@ -236,7 +234,7 @@ func TestFailFirst(t *testing.T) {
}
func
TestStressActiveDial
(
t
*
testing
.
T
)
{
ds
:=
NewDialSync
(
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
D
ialRequest
)
error
{
ds
:=
NewDialSync
(
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
error
{
go
func
()
{
for
{
select
{
...
...
@@ -245,7 +243,7 @@ func TestStressActiveDial(t *testing.T) {
return
}
req
.
R
esch
<-
D
ialResponse
{}
req
.
r
esch
<-
d
ialResponse
{}
case
<-
ctx
.
Done
()
:
return
}
...
...
swarm_dial.go
View file @
16248280
...
...
@@ -285,18 +285,18 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) {
// 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
}
// 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
)
error
{
func
(
s
*
Swarm
)
dialWorker
(
ctx
context
.
Context
,
p
peer
.
ID
,
reqch
<-
chan
d
ialRequest
)
error
{
if
p
==
s
.
local
{
return
ErrDialToSelf
}
...
...
@@ -305,11 +305,11 @@ func (s *Swarm) dialWorker(ctx context.Context, p peer.ID, reqch <-chan DialRequ
return
nil
}
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
}
...
...
@@ -344,11 +344,11 @@ func (s *Swarm) dialWorkerLoop(ctx context.Context, p peer.ID, reqch <-chan Dial
// all addrs have erred, dispatch dial error
// but first do a last one check in case an acceptable connection has landed from
// a simultaneous dial that started later and added new acceptable addrs
c
:=
s
.
bestAcceptableConnToPeer
(
pr
.
req
.
C
tx
,
p
)
c
:=
s
.
bestAcceptableConnToPeer
(
pr
.
req
.
c
tx
,
p
)
if
c
!=
nil
{
pr
.
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
c
}
pr
.
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
c
}
}
else
{
pr
.
req
.
R
esch
<-
D
ialResponse
{
E
rr
:
pr
.
err
}
pr
.
req
.
r
esch
<-
d
ialResponse
{
e
rr
:
pr
.
err
}
}
delete
(
requests
,
reqno
)
}
...
...
@@ -390,15 +390,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
}
...
...
@@ -430,7 +430,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
}
...
...
@@ -447,7 +447,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
}
...
...
@@ -457,14 +457,14 @@ loop:
for
_
,
ad
:=
range
tojoin
{
if
!
ad
.
dialed
{
ad
.
ctx
=
s
.
mergeDialContexts
(
ad
.
ctx
,
req
.
C
tx
)
ad
.
ctx
=
s
.
mergeDialContexts
(
ad
.
ctx
,
req
.
c
tx
)
}
ad
.
requests
=
append
(
ad
.
requests
,
reqno
)
}
if
len
(
todial
)
>
0
{
for
_
,
a
:=
range
todial
{
pending
[
a
]
=
&
addrDial
{
addr
:
a
,
ctx
:
req
.
C
tx
,
requests
:
[]
int
{
reqno
}}
pending
[
a
]
=
&
addrDial
{
addr
:
a
,
ctx
:
req
.
c
tx
,
requests
:
[]
int
{
reqno
}}
}
nextDial
=
append
(
nextDial
,
todial
...
)
...
...
@@ -550,7 +550,7 @@ loop:
continue
}
pr
.
req
.
R
esch
<-
D
ialResponse
{
C
onn
:
conn
}
pr
.
req
.
r
esch
<-
d
ialResponse
{
c
onn
:
conn
}
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