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-tcp-transport
Commits
2d16f912
Commit
2d16f912
authored
Jun 08, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a TCP connect timeout
parent
753b87da
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
1 deletion
+19
-1
tcp.go
tcp.go
+19
-1
No files found.
tcp.go
View file @
2d16f912
...
...
@@ -2,6 +2,7 @@ package tcp
import
(
"context"
"time"
logging
"github.com/ipfs/go-log"
peer
"github.com/libp2p/go-libp2p-peer"
...
...
@@ -13,6 +14,10 @@ import (
mafmt
"github.com/whyrusleeping/mafmt"
)
// DefaultConnectTimeout is the (default) maximum amount of time the TCP
// transport will spend on the initial TCP connect before giving up.
var
DefaultConnectTimeout
=
5
*
time
.
Second
var
log
=
logging
.
Logger
(
"tcp-tpt"
)
// TcpTransport is the TCP transport.
...
...
@@ -24,6 +29,9 @@ type TcpTransport struct {
// Explicitly disable reuseport.
DisableReuseport
bool
// TCP connect timeout
ConnectTimeout
time
.
Duration
reuse
rtpt
.
Transport
}
...
...
@@ -32,7 +40,7 @@ var _ tpt.Transport = &TcpTransport{}
// NewTCPTransport creates a tcp transport object that tracks dialers and listeners
// created. It represents an entire tcp stack (though it might not necessarily be)
func
NewTCPTransport
(
upgrader
*
tptu
.
Upgrader
)
*
TcpTransport
{
return
&
TcpTransport
{
Upgrader
:
upgrader
}
return
&
TcpTransport
{
Upgrader
:
upgrader
,
ConnectTimeout
:
DefaultConnectTimeout
}
}
// CanDial returns true if this transport believes it can dial the given
...
...
@@ -42,6 +50,16 @@ func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
}
func
(
t
*
TcpTransport
)
maDial
(
ctx
context
.
Context
,
raddr
ma
.
Multiaddr
)
(
manet
.
Conn
,
error
)
{
// Apply the deadline iff applicable
if
t
.
ConnectTimeout
>
0
{
deadline
:=
time
.
Now
()
.
Add
(
t
.
ConnectTimeout
)
if
d
,
ok
:=
ctx
.
Deadline
();
!
ok
||
deadline
.
Before
(
d
)
{
var
cancel
func
()
ctx
,
cancel
=
context
.
WithDeadline
(
ctx
,
deadline
)
defer
cancel
()
}
}
if
t
.
UseReuseport
()
{
return
t
.
reuse
.
DialContext
(
ctx
,
raddr
)
}
...
...
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