Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
c114b04a
提交
c114b04a
authored
1月 30, 2015
作者:
Brian Tiger Chow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(bitswap): synchronous close
上级
951ff4f7
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
47 行增加
和
16 行删除
+47
-16
core.go
core/core.go
+6
-4
bitswap.go
exchange/bitswap/bitswap.go
+41
-10
bitswap_test.go
exchange/bitswap/bitswap_test.go
+0
-2
没有找到文件。
core/core.go
浏览文件 @
c114b04a
...
...
@@ -263,16 +263,18 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context) error {
func
(
n
*
IpfsNode
)
teardown
()
error
{
// owned objects are closed in this teardown to ensure that they're closed
// regardless of which constructor was used to add them to the node.
var
closers
[]
io
.
Closer
addCloser
:=
func
(
c
io
.
Closer
)
{
closers
:=
[]
io
.
Closer
{
n
.
Blocks
,
n
.
Exchange
,
n
.
Repo
,
}
addCloser
:=
func
(
c
io
.
Closer
)
{
// use when field may be nil
if
c
!=
nil
{
closers
=
append
(
closers
,
c
)
}
}
addCloser
(
n
.
Bootstrapper
)
addCloser
(
n
.
Repo
)
addCloser
(
n
.
Blocks
)
if
dht
,
ok
:=
n
.
Routing
.
(
*
dht
.
IpfsDHT
);
ok
{
addCloser
(
dht
)
}
...
...
exchange/bitswap/bitswap.go
浏览文件 @
c114b04a
...
...
@@ -9,6 +9,7 @@ import (
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
inflect
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/inflect"
process
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
blocks
"github.com/jbenet/go-ipfs/blocks"
blockstore
"github.com/jbenet/go-ipfs/blocks/blockstore"
...
...
@@ -52,28 +53,47 @@ var (
func
New
(
parent
context
.
Context
,
p
peer
.
ID
,
network
bsnet
.
BitSwapNetwork
,
bstore
blockstore
.
Blockstore
,
nice
bool
)
exchange
.
Interface
{
// important to use provided parent context (since it may include important
// loggable data). It's probably not a good idea to allow bitswap to be
// coupled to the concerns of the IPFS daemon in this way.
//
// FIXME(btc) Now that bitswap manages itself using a process, it probably
// shouldn't accept a context anymore. Clients should probably use Close()
// exclusively. We should probably find another way to share logging data
ctx
,
cancelFunc
:=
context
.
WithCancel
(
parent
)
notif
:=
notifications
.
New
()
px
:=
process
.
WithTeardown
(
func
()
error
{
notif
.
Shutdown
()
return
nil
})
go
func
()
{
<-
ctx
.
Done
()
<-
px
.
Closing
()
// process closes first
cancelFunc
()
notif
.
Shutdown
()
}()
go
func
()
{
<-
ctx
.
Done
()
// parent cancelled first
px
.
Close
()
}()
bs
:=
&
bitswap
{
self
:
p
,
blockstore
:
bstore
,
cancelFunc
:
cancelFunc
,
notifications
:
notif
,
engine
:
decision
.
NewEngine
(
ctx
,
bstore
),
engine
:
decision
.
NewEngine
(
ctx
,
bstore
),
// TODO close the engine with Close() method
network
:
network
,
wantlist
:
wantlist
.
NewThreadSafe
(),
batchRequests
:
make
(
chan
[]
u
.
Key
,
sizeBatchRequestChan
),
process
:
px
,
}
network
.
SetDelegate
(
bs
)
go
bs
.
clientWorker
(
ctx
)
go
bs
.
taskWorker
(
ctx
)
px
.
Go
(
func
(
px
process
.
Process
)
{
bs
.
clientWorker
(
ctx
)
})
px
.
Go
(
func
(
px
process
.
Process
)
{
bs
.
taskWorker
(
ctx
)
})
return
bs
}
...
...
@@ -102,8 +122,7 @@ type bitswap struct {
wantlist
*
wantlist
.
ThreadSafe
// cancelFunc signals cancellation to the bitswap event loop
cancelFunc
func
()
process
process
.
Process
}
// GetBlock attempts to retrieve a particular block from peers within the
...
...
@@ -149,6 +168,11 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
// that lasts throughout the lifetime of the server)
func
(
bs
*
bitswap
)
GetBlocks
(
ctx
context
.
Context
,
keys
[]
u
.
Key
)
(
<-
chan
*
blocks
.
Block
,
error
)
{
select
{
case
<-
bs
.
process
.
Closing
()
:
return
nil
,
errors
.
New
(
"bitswap is closed"
)
default
:
}
promise
:=
bs
.
notifications
.
Subscribe
(
ctx
,
keys
...
)
select
{
case
bs
.
batchRequests
<-
keys
:
...
...
@@ -161,6 +185,11 @@ func (bs *bitswap) GetBlocks(ctx context.Context, keys []u.Key) (<-chan *blocks.
// HasBlock announces the existance of a block to this bitswap service. The
// service will potentially notify its peers.
func
(
bs
*
bitswap
)
HasBlock
(
ctx
context
.
Context
,
blk
*
blocks
.
Block
)
error
{
select
{
case
<-
bs
.
process
.
Closing
()
:
return
errors
.
New
(
"bitswap is closed"
)
default
:
}
if
err
:=
bs
.
blockstore
.
Put
(
blk
);
err
!=
nil
{
return
err
}
...
...
@@ -235,6 +264,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, entries []wantli
}
func
(
bs
*
bitswap
)
taskWorker
(
ctx
context
.
Context
)
{
defer
log
.
Info
(
"bitswap task worker shutting down..."
)
for
{
select
{
case
<-
ctx
.
Done
()
:
...
...
@@ -256,6 +286,8 @@ func (bs *bitswap) taskWorker(ctx context.Context) {
// TODO ensure only one active request per key
func
(
bs
*
bitswap
)
clientWorker
(
parent
context
.
Context
)
{
defer
log
.
Info
(
"bitswap client worker shutting down..."
)
ctx
,
cancel
:=
context
.
WithCancel
(
parent
)
broadcastSignal
:=
time
.
After
(
rebroadcastDelay
.
Get
())
...
...
@@ -384,6 +416,5 @@ func (bs *bitswap) send(ctx context.Context, p peer.ID, m bsmsg.BitSwapMessage)
}
func
(
bs
*
bitswap
)
Close
()
error
{
bs
.
cancelFunc
()
return
nil
// to conform to Closer interface
return
bs
.
process
.
Close
()
}
exchange/bitswap/bitswap_test.go
浏览文件 @
c114b04a
...
...
@@ -22,8 +22,6 @@ import (
const
kNetworkDelay
=
0
*
time
.
Millisecond
func
TestClose
(
t
*
testing
.
T
)
{
// TODO
t
.
Skip
(
"TODO Bitswap's Close implementation is a WIP"
)
vnet
:=
tn
.
VirtualNetwork
(
mockrouting
.
NewServer
(),
delay
.
Fixed
(
kNetworkDelay
))
sesgen
:=
NewTestSessionGenerator
(
vnet
)
defer
sesgen
.
Close
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论