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
9183b8af
Commit
9183b8af
authored
Nov 10, 2016
by
Jeromy Johnson
Committed by
GitHub
Nov 10, 2016
Browse files
Merge pull request #150 from libp2p/feat/negotiate-timeouts
add negotiate timeout option to basichost
parents
6bccc8c1
4f17a651
Changes
2
Hide whitespace changes
Inline
Side-by-side
p2p/host/basic/basic_host.go
View file @
9183b8af
...
@@ -23,6 +23,8 @@ import (
...
@@ -23,6 +23,8 @@ import (
var
log
=
logging
.
Logger
(
"basichost"
)
var
log
=
logging
.
Logger
(
"basichost"
)
var
NegotiateTimeout
=
time
.
Second
*
60
// Option is a type used to pass in options to the host.
// Option is a type used to pass in options to the host.
type
Option
int
type
Option
int
...
@@ -48,6 +50,8 @@ type BasicHost struct {
...
@@ -48,6 +50,8 @@ type BasicHost struct {
relay
*
relay
.
RelayService
relay
*
relay
.
RelayService
natmgr
*
natManager
natmgr
*
natManager
NegotiateTimeout
time
.
Duration
proc
goprocess
.
Process
proc
goprocess
.
Process
bwc
metrics
.
Reporter
bwc
metrics
.
Reporter
...
@@ -56,8 +60,9 @@ type BasicHost struct {
...
@@ -56,8 +60,9 @@ type BasicHost struct {
// New constructs and sets up a new *BasicHost with given Network
// New constructs and sets up a new *BasicHost with given Network
func
New
(
net
inet
.
Network
,
opts
...
interface
{})
*
BasicHost
{
func
New
(
net
inet
.
Network
,
opts
...
interface
{})
*
BasicHost
{
h
:=
&
BasicHost
{
h
:=
&
BasicHost
{
network
:
net
,
network
:
net
,
mux
:
msmux
.
NewMultistreamMuxer
(),
mux
:
msmux
.
NewMultistreamMuxer
(),
NegotiateTimeout
:
NegotiateTimeout
,
}
}
h
.
proc
=
goprocess
.
WithTeardown
(
func
()
error
{
h
.
proc
=
goprocess
.
WithTeardown
(
func
()
error
{
...
@@ -106,6 +111,15 @@ func (h *BasicHost) newConnHandler(c inet.Conn) {
...
@@ -106,6 +111,15 @@ func (h *BasicHost) newConnHandler(c inet.Conn) {
// TODO: this feels a bit wonky
// TODO: this feels a bit wonky
func
(
h
*
BasicHost
)
newStreamHandler
(
s
inet
.
Stream
)
{
func
(
h
*
BasicHost
)
newStreamHandler
(
s
inet
.
Stream
)
{
before
:=
time
.
Now
()
before
:=
time
.
Now
()
if
h
.
NegotiateTimeout
!=
0
{
if
err
:=
s
.
SetDeadline
(
time
.
Now
()
.
Add
(
h
.
NegotiateTimeout
));
err
!=
nil
{
log
.
Error
(
"setting stream deadline: "
,
err
)
s
.
Close
()
return
}
}
protoID
,
handle
,
err
:=
h
.
Mux
()
.
Negotiate
(
s
)
protoID
,
handle
,
err
:=
h
.
Mux
()
.
Negotiate
(
s
)
took
:=
time
.
Now
()
.
Sub
(
before
)
took
:=
time
.
Now
()
.
Sub
(
before
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -121,6 +135,15 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
...
@@ -121,6 +135,15 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
s
.
Close
()
s
.
Close
()
return
return
}
}
if
h
.
NegotiateTimeout
!=
0
{
if
err
:=
s
.
SetDeadline
(
time
.
Time
{});
err
!=
nil
{
log
.
Error
(
"resetting stream deadline: "
,
err
)
s
.
Close
()
return
}
}
s
.
SetProtocol
(
protocol
.
ID
(
protoID
))
s
.
SetProtocol
(
protocol
.
ID
(
protoID
))
if
h
.
bwc
!=
nil
{
if
h
.
bwc
!=
nil
{
...
...
p2p/net/mock/mock_net.go
View file @
9183b8af
...
@@ -85,6 +85,7 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho
...
@@ -85,6 +85,7 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho
}
}
h
:=
bhost
.
New
(
n
)
h
:=
bhost
.
New
(
n
)
h
.
NegotiateTimeout
=
0
mn
.
proc
.
AddChild
(
n
.
proc
)
mn
.
proc
.
AddChild
(
n
.
proc
)
...
...
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