Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
4de5eaad
提交
4de5eaad
authored
10月 16, 2015
作者:
Juan Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1837 from ipfs/dial-smarter
refactor dialing to not panic, and to be smart about ordering
上级
d6297c74
663a0309
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
48 行增加
和
55 行删除
+48
-55
swarm_dial.go
p2p/net/swarm/swarm_dial.go
+48
-55
没有找到文件。
p2p/net/swarm/swarm_dial.go
浏览文件 @
4de5eaad
...
@@ -16,9 +16,6 @@ import (
...
@@ -16,9 +16,6 @@ import (
ma
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
ma
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
manet
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
process
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
processctx
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
ratelimit
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/ratelimit"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
)
...
@@ -371,87 +368,83 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
...
@@ -371,87 +368,83 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
defer
cancel
()
// cancel work when we exit func
defer
cancel
()
// cancel work when we exit func
foundConn
:=
make
(
chan
struct
{})
conns
:=
make
(
chan
conn
.
Conn
)
conns
:=
make
(
chan
conn
.
Conn
,
len
(
remoteAddrs
))
errs
:=
make
(
chan
error
,
len
(
remoteAddrs
))
errs
:=
make
(
chan
error
,
len
(
remoteAddrs
))
// dialSingleAddr is used in the rate-limited async thing below.
// dialSingleAddr is used in the rate-limited async thing below.
dialSingleAddr
:=
func
(
addr
ma
.
Multiaddr
)
{
dialSingleAddr
:=
func
(
addr
ma
.
Multiaddr
)
{
// rebind chans in scope so we can nil them out easily
connsout
:=
conns
errsout
:=
errs
connC
,
err
:=
s
.
dialAddr
(
ctx
,
d
,
p
,
addr
)
connC
,
err
:=
s
.
dialAddr
(
ctx
,
d
,
p
,
addr
)
if
err
!=
nil
{
connsout
=
nil
}
else
if
connC
==
nil
{
// NOTE: this really should never happen
log
.
Errorf
(
"failed to dial %s %s and got no error!"
,
p
,
addr
)
err
=
fmt
.
Errorf
(
"failed to dial %s %s"
,
p
,
addr
)
connsout
=
nil
}
else
{
errsout
=
nil
}
// check parent still wants our results
// check parent still wants our results
select
{
select
{
case
<-
foundConn
:
case
<-
ctx
.
Done
()
:
if
connC
!=
nil
{
if
connC
!=
nil
{
connC
.
Close
()
connC
.
Close
()
}
}
return
case
errsout
<-
err
:
default
:
case
connsout
<-
connC
:
}
if
err
!=
nil
{
errs
<-
err
}
else
if
connC
==
nil
{
errs
<-
fmt
.
Errorf
(
"failed to dial %s %s"
,
p
,
addr
)
}
else
{
conns
<-
connC
}
}
}
}
// this whole thing is in a goroutine so we can use foundConn
// this whole thing is in a goroutine so we can use foundConn
// to end early.
// to end early.
go
func
()
{
go
func
()
{
// rate limiting just in case. at most 10 addrs at once.
limiter
:=
make
(
chan
struct
{},
8
)
limiter
:=
ratelimit
.
NewRateLimiter
(
process
.
Background
(),
8
)
// permute addrs so we try different sets first each time.
limiter
.
Go
(
func
(
worker
process
.
Process
)
{
for
_
,
i
:=
range
rand
.
Perm
(
len
(
remoteAddrs
))
{
// permute addrs so we try different sets first each time.
for
_
,
i
:=
range
rand
.
Perm
(
len
(
remoteAddrs
))
{
addr
:=
remoteAddrs
[
i
]
select
{
// returns whatever ratelimiting is acceptable for workerAddr.
case
<-
foundConn
:
// if one of them succeeded already
// may not rate limit at all.
break
rl
:=
s
.
addrDialRateLimit
(
addr
)
case
<-
worker
.
Closing
()
:
// our context was cancelled
select
{
break
case
<-
ctx
.
Done
()
:
// our context was cancelled
default
:
return
}
case
rl
<-
struct
{}{}
:
// take the token, move on
workerAddr
:=
remoteAddrs
[
i
]
// shadow variable to avoid race
}
// we have to do the waiting concurrently because there are addrs
// that SHOULD NOT be rate limited (utp), nor blocked by other
// rate limited addrs (tcp).
//
// (and we need to call `limiter.Go`, instead of `go` as required
// by goproc/limiter semantics. note: limiter.Go is not LimitedGo.)
limiter
.
Go
(
func
(
p
process
.
Process
)
{
// returns whatever ratelimiting is acceptable for workerAddr.
// may not rate limit at all.
rl
:=
s
.
addrDialRateLimit
(
workerAddr
)
rl
<-
struct
{}{}
limiter
.
LimitedGo
(
func
(
worker
process
.
Process
)
{
dialSingleAddr
(
workerAddr
)
})
<-
rl
})
select
{
case
<-
ctx
.
Done
()
:
// our context was cancelled
return
case
limiter
<-
struct
{}{}
:
// take the token, move on
}
}
})
processctx
.
CloseAfterContext
(
limiter
,
ctx
)
go
func
(
rlc
<-
chan
struct
{},
a
ma
.
Multiaddr
)
{
dialSingleAddr
(
a
)
<-
limiter
<-
rlc
}(
rl
,
addr
)
}
}()
}()
// wair fo
t
the results.
// wair fo
r
the results.
exitErr
:=
fmt
.
Errorf
(
"failed to dial %s"
,
p
)
exitErr
:=
fmt
.
Errorf
(
"failed to dial %s"
,
p
)
for
i
:=
0
;
i
<
len
(
remoteAddrs
);
i
++
{
for
range
remoteAddrs
{
select
{
select
{
case
exitErr
=
<-
errs
:
//
case
exitErr
=
<-
errs
:
//
log
.
Debug
(
"dial error: "
,
exitErr
)
log
.
Debug
(
"dial error: "
,
exitErr
)
case
connC
:=
<-
conns
:
case
connC
:=
<-
conns
:
// take the first + return asap
// take the first + return asap
close
(
foundConn
)
return
connC
,
nil
return
connC
,
nil
case
<-
ctx
.
Done
()
:
// break out and return error
break
}
}
}
}
return
nil
,
exitErr
return
nil
,
exitErr
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论