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
94417543
Commit
94417543
authored
Aug 18, 2016
by
Jeromy
Browse files
swarm: add deadline for connection setup
parent
4ae3510f
Changes
2
Hide whitespace changes
Inline
Side-by-side
p2p/host/basic/basic_host.go
View file @
94417543
...
...
@@ -2,6 +2,7 @@ package basichost
import
(
"io"
"time"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
...
...
@@ -102,20 +103,29 @@ func (h *BasicHost) newConnHandler(c inet.Conn) {
// newStreamHandler is the remote-opened stream handler for inet.Network
// TODO: this feels a bit wonky
func
(
h
*
BasicHost
)
newStreamHandler
(
s
inet
.
Stream
)
{
before
:=
time
.
Now
()
protoID
,
handle
,
err
:=
h
.
Mux
()
.
Negotiate
(
s
)
took
:=
time
.
Now
()
.
Sub
(
before
)
if
err
!=
nil
{
if
err
==
io
.
EOF
{
log
.
Debugf
(
"protocol EOF: %s"
,
s
.
Conn
()
.
RemotePeer
())
logf
:=
log
.
Debugf
if
took
>
time
.
Second
*
10
{
logf
=
log
.
Warningf
}
logf
(
"protocol EOF: %s (took %s)"
,
s
.
Conn
()
.
RemotePeer
(),
took
)
}
else
{
log
.
Warning
(
"protocol mux failed: %s
"
,
err
)
log
.
Warning
(
"protocol mux failed: %s
(took %s)"
,
err
,
took
)
}
return
}
s
.
SetProtocol
(
protocol
.
ID
(
protoID
))
logStream
:=
mstream
.
WrapStream
(
s
,
h
.
bwc
)
if
h
.
bwc
!=
nil
{
s
=
mstream
.
WrapStream
(
s
,
h
.
bwc
)
}
log
.
Debugf
(
"protocol negotiation took %s"
,
took
)
go
handle
(
protoID
,
logStream
)
go
handle
(
protoID
,
s
)
}
// ID returns the (local) peer.ID associated with this Host
...
...
p2p/net/swarm/swarm_dial.go
View file @
94417543
...
...
@@ -372,10 +372,21 @@ func (s *Swarm) dialAddr(ctx context.Context, p peer.ID, addr ma.Multiaddr) (con
return
connC
,
nil
}
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
)
{
deadline
,
ok
:=
ctx
.
Deadline
()
if
!
ok
{
deadline
=
time
.
Now
()
.
Add
(
ConnSetupTimeout
)
}
if
err
:=
connC
.
SetDeadline
(
deadline
);
err
!=
nil
{
return
nil
,
err
}
psC
,
err
:=
s
.
swarm
.
AddConn
(
connC
)
if
err
!=
nil
{
// connC is closed by caller if we fail.
...
...
@@ -389,5 +400,10 @@ func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error
return
nil
,
err
}
if
err
:=
connC
.
SetDeadline
(
time
.
Time
{});
err
!=
nil
{
log
.
Error
(
"failed to reset connection deadline after setup: "
,
err
)
return
nil
,
err
}
return
swarmC
,
err
}
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