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
91b50b8e
Commit
91b50b8e
authored
Apr 10, 2019
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
context option to disable dialing when opening a new stream
parent
87b9a9c7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
0 deletions
+27
-0
swarm.go
swarm.go
+16
-0
swarm_test.go
swarm_test.go
+11
-0
No files found.
swarm.go
View file @
91b50b8e
...
@@ -37,6 +37,18 @@ var ErrSwarmClosed = errors.New("swarm closed")
...
@@ -37,6 +37,18 @@ var ErrSwarmClosed = errors.New("swarm closed")
// transport is misbehaving.
// transport is misbehaving.
var
ErrAddrFiltered
=
errors
.
New
(
"address filtered"
)
var
ErrAddrFiltered
=
errors
.
New
(
"address filtered"
)
// ErrNoConn is returned when attempting to open a stream to a peer with the NoDial
// option and no usable connection is available.
var
ErrNoConn
=
errors
.
New
(
"no usable connection to peer"
)
// ContextOption is the type of context options understood by the swarm
type
ContextOption
string
// NoDial is a context option that instructs the swarm to not attempt a new
// dial when opening a stream. The value of the key should be a string indicating
// the source of the option.
var
NoDial
=
ContextOption
(
"swarm.NoDial"
)
// Swarm is a connection muxer, allowing connections to other peers to
// Swarm is a connection muxer, allowing connections to other peers to
// be opened and closed, while still using the same Chan for all
// be opened and closed, while still using the same Chan for all
// communication. The Chan sends/receives Messages, which note the
// communication. The Chan sends/receives Messages, which note the
...
@@ -295,6 +307,10 @@ func (s *Swarm) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
...
@@ -295,6 +307,10 @@ func (s *Swarm) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
for
{
for
{
c
:=
s
.
bestConnToPeer
(
p
)
c
:=
s
.
bestConnToPeer
(
p
)
if
c
==
nil
{
if
c
==
nil
{
if
nodial
:=
ctx
.
Value
(
NoDial
);
nodial
!=
nil
{
return
nil
,
ErrNoConn
}
if
dials
>=
DialAttempts
{
if
dials
>=
DialAttempts
{
return
nil
,
errors
.
New
(
"max dial attempts exceeded"
)
return
nil
,
errors
.
New
(
"max dial attempts exceeded"
)
}
}
...
...
swarm_test.go
View file @
91b50b8e
...
@@ -50,6 +50,7 @@ func EchoStreamHandler(stream inet.Stream) {
...
@@ -50,6 +50,7 @@ func EchoStreamHandler(stream inet.Stream) {
return
return
}
}
}
}
}()
}()
}
}
...
@@ -335,3 +336,13 @@ func TestFilterBounds(t *testing.T) {
...
@@ -335,3 +336,13 @@ func TestFilterBounds(t *testing.T) {
t
.
Log
(
"got connect"
)
t
.
Log
(
"got connect"
)
}
}
}
}
func
TestNoDial
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
swarms
:=
makeSwarms
(
ctx
,
t
,
2
)
_
,
err
:=
swarms
[
0
]
.
NewStream
(
context
.
WithValue
(
ctx
,
NoDial
,
"swarm.test"
),
swarms
[
1
]
.
LocalPeer
())
if
err
!=
ErrNoConn
{
t
.
Fatal
(
"should have failed with ErrNoConn"
)
}
}
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