Commit ec696be0 authored by Łukasz Magiera's avatar Łukasz Magiera

feat: lower timeout for local address dials

parent da01184a
package swarm
import (
mafilter "github.com/libp2p/go-maddr-filter"
mamask "github.com/whyrusleeping/multiaddr-filter"
)
// http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
var lowTimeoutFilters = mafilter.NewFilters()
func init() {
for _, p := range []string{
"/ip4/10.0.0.0/ipcidr/8",
"/ip4/100.64.0.0/ipcidr/10",
"/ip4/169.254.0.0/ipcidr/16",
"/ip4/172.16.0.0/ipcidr/12",
"/ip4/192.0.0.0/ipcidr/24",
"/ip4/192.0.0.0/ipcidr/29",
"/ip4/192.0.0.8/ipcidr/32",
"/ip4/192.0.0.170/ipcidr/32",
"/ip4/192.0.0.171/ipcidr/32",
"/ip4/192.0.2.0/ipcidr/24",
"/ip4/192.168.0.0/ipcidr/16",
"/ip4/198.18.0.0/ipcidr/15",
"/ip4/198.51.100.0/ipcidr/24",
"/ip4/203.0.113.0/ipcidr/24",
"/ip4/240.0.0.0/ipcidr/4",
} {
f, err := mamask.NewMask(p)
if err != nil {
panic("error in lowTimeoutFilters init: " + err.Error())
}
lowTimeoutFilters.AddDialFilter(f)
}
}
......@@ -3,6 +3,7 @@ package swarm
import (
"context"
"sync"
"time"
addrutil "github.com/libp2p/go-addr-util"
peer "github.com/libp2p/go-libp2p-peer"
......@@ -33,6 +34,15 @@ func (dj *dialJob) cancelled() bool {
}
}
func (dj *dialJob) dialTimeout() time.Duration {
timeout := DialTimeout
if lowTimeoutFilters.AddrBlocked(dj.addr) {
timeout = DialTimeoutLocal
}
return timeout
}
type dialLimiter struct {
rllock sync.Mutex
fdConsuming int
......@@ -162,7 +172,10 @@ func (dl *dialLimiter) executeDial(j *dialJob) {
return
}
con, err := dl.dialFunc(j.ctx, j.peer, j.addr)
dctx, cancel := context.WithTimeout(j.ctx, j.dialTimeout())
defer cancel()
con, err := dl.dialFunc(dctx, j.peer, j.addr)
select {
case j.resp <- dialResult{Conn: con, Addr: j.addr, Err: err}:
case <-j.ctx.Done():
......
......@@ -22,9 +22,15 @@ import (
)
// DialTimeout is the maximum duration a Dial is allowed to take.
// This includes the time spent waiting in dial limiter, between dialing the raw
// network connection, protocol selection as well the handshake, if applicable.
var DialTimeout = 60 * time.Second
// DialTimeoutLocal is the maximum duration a Dial to local network address
// is allowed to take.
// This includes the time between dialing the raw network connection,
// protocol selection as well the handshake, if applicable.
var DialTimeout = 60 * time.Second
var DialTimeoutLocal = 5 * time.Second
var log = logging.Logger("swarm2")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment