Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
d4656657
提交
d4656657
authored
7月 10, 2015
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1466 from ipfs/export-modules
moved util/ctx to github.com/jbenet/go-context
上级
30509fc5
11937be1
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
103 行增加
和
53 行删除
+103
-53
Godeps.json
Godeps/Godeps.json
+8
-5
fracctx.go
...orkspace/src/github.com/jbenet/go-context/frac/fracctx.go
+62
-0
fracctx_test.go
...ace/src/github.com/jbenet/go-context/frac/fracctx_test.go
+4
-5
ctxio.go
...s/_workspace/src/github.com/jbenet/go-context/io/ctxio.go
+11
-1
ctxio_test.go
...rkspace/src/github.com/jbenet/go-context/io/ctxio_test.go
+3
-3
diag.go
diagnostics/diag.go
+5
-6
dht_net.go
routing/dht/dht_net.go
+8
-9
records.go
routing/dht/records.go
+2
-2
fracctx.go
util/ctx/fracctx.go
+0
-22
没有找到文件。
Godeps/Godeps.json
浏览文件 @
d4656657
...
...
@@ -15,11 +15,6 @@
"Rev"
:
"75cd24fc2f2c2a2088577d12123ddee5f54e0675"
},
{
"ImportPath"
:
"code.google.com/p/goprotobuf/proto"
,
"Comment"
:
"go.r60-152"
,
"Rev"
:
"36be16571e14f67e114bb0af619e5de2c1591679"
},
{
"ImportPath"
:
"github.com/ActiveState/tail"
,
"Rev"
:
"068b72961a6bc5b4a82cf4fc14ccc724c0cfa73a"
},
...
...
@@ -138,6 +133,14 @@
"Rev"
:
"6237cf65f3a6f7111cd8a42be3590df99a66bc7d"
},
{
"ImportPath"
:
"github.com/jbenet/go-context/frac"
,
"Rev"
:
"d14ea06fba99483203c19d92cfcd13ebe73135f4"
},
{
"ImportPath"
:
"github.com/jbenet/go-context/io"
,
"Rev"
:
"d14ea06fba99483203c19d92cfcd13ebe73135f4"
},
{
"ImportPath"
:
"github.com/jbenet/go-datastore"
,
"Rev"
:
"7d6acaf7c0164c335f2ca4100f8fe30a7e2943dd"
},
...
...
Godeps/_workspace/src/github.com/jbenet/go-context/frac/fracctx.go
0 → 100644
浏览文件 @
d4656657
// Package ctxext provides multiple useful context constructors.
package
ctxext
import
(
"time"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
// WithDeadlineFraction returns a Context with a fraction of the
// original context's timeout. This is useful in sequential pipelines
// of work, where one might try options and fall back to others
// depending on the time available, or failure to respond. For example:
//
// // getPicture returns a picture from our encrypted database
// // we have a pipeline of multiple steps. we need to:
// // - get the data from a database
// // - decrypt it
// // - apply many transforms
// //
// // we **know** that each step takes increasingly more time.
// // The transforms are much more expensive than decryption, and
// // decryption is more expensive than the database lookup.
// // If our database takes too long (i.e. >0.2 of available time),
// // there's no use in continuing.
// func getPicture(ctx context.Context, key string) ([]byte, error) {
// // fractional timeout contexts to the rescue!
//
// // try the database with 0.2 of remaining time.
// ctx1, _ := ctxext.WithDeadlineFraction(ctx, 0.2)
// val, err := db.Get(ctx1, key)
// if err != nil {
// return nil, err
// }
//
// // try decryption with 0.3 of remaining time.
// ctx2, _ := ctxext.WithDeadlineFraction(ctx, 0.3)
// if val, err = decryptor.Decrypt(ctx2, val); err != nil {
// return nil, err
// }
//
// // try transforms with all remaining time. hopefully it's enough!
// return transformer.Transform(ctx, val)
// }
//
//
func
WithDeadlineFraction
(
ctx
context
.
Context
,
fraction
float64
)
(
context
.
Context
,
context
.
CancelFunc
)
{
d
,
found
:=
ctx
.
Deadline
()
if
!
found
{
// no deadline
return
context
.
WithCancel
(
ctx
)
}
left
:=
d
.
Sub
(
time
.
Now
())
if
left
<
0
{
// already passed...
return
context
.
WithCancel
(
ctx
)
}
left
=
time
.
Duration
(
float64
(
left
)
*
fraction
)
return
context
.
WithTimeout
(
ctx
,
left
)
}
util/ctx
/fracctx_test.go
→
Godeps/_workspace/src/github.com/jbenet/go-context/frac
/fracctx_test.go
浏览文件 @
d4656657
package
ctx
util
package
ctx
ext
import
(
"os"
"testing"
"time"
travis
"github.com/ipfs/go-ipfs/util/testutil/ci/travis"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
// this test is on the context tool itself, not our stuff. it's for sanity on ours.
func
TestDeadline
(
t
*
testing
.
T
)
{
if
travis
.
IsRunning
()
{
if
os
.
Getenv
(
"TRAVIS"
)
==
"true"
{
t
.
Skip
(
"timeouts don't work reliably on travis"
)
}
...
...
@@ -43,7 +42,7 @@ func TestDeadlineFractionForever(t *testing.T) {
}
func
TestDeadlineFractionHalf
(
t
*
testing
.
T
)
{
if
travis
.
IsRunning
()
{
if
os
.
Getenv
(
"TRAVIS"
)
==
"true"
{
t
.
Skip
(
"timeouts don't work reliably on travis"
)
}
...
...
util/ctx
/ctxio.go
→
Godeps/_workspace/src/github.com/jbenet/go-context/io
/ctxio.go
浏览文件 @
d4656657
package
ctxutil
// Package ctxio provides io.Reader and io.Writer wrappers that
// respect context.Contexts. Use these at the interface between
// your context code and your io.
//
// WARNING: read the code. see how writes and reads will continue
// until you cancel the io. Maybe this package should provide
// versions of io.ReadCloser and io.WriteCloser that automatically
// call .Close when the context expires. But for now -- since in my
// use cases I have long-lived connections with ephemeral io wrappers
// -- this has yet to be a need.
package
ctxio
import
(
"io"
...
...
util/ctx
/ctxio_test.go
→
Godeps/_workspace/src/github.com/jbenet/go-context/io
/ctxio_test.go
浏览文件 @
d4656657
package
ctx
util
package
ctx
io
import
(
"bytes"
...
...
@@ -49,8 +49,8 @@ func TestReader(t *testing.T) {
}
func
TestWriter
(
t
*
testing
.
T
)
{
buf
:=
new
(
bytes
.
Buffer
)
w
:=
NewWriter
(
context
.
Background
(),
buf
)
var
buf
bytes
.
Buffer
w
:=
NewWriter
(
context
.
Background
(),
&
buf
)
// write three
n
,
err
:=
w
.
Write
([]
byte
(
"abc"
))
...
...
diagnostics/diag.go
浏览文件 @
d4656657
...
...
@@ -13,15 +13,14 @@ import (
ggio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
proto
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
ctxio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-context/io"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
pb
"github.com/ipfs/go-ipfs/diagnostics/pb"
host
"github.com/ipfs/go-ipfs/p2p/host"
inet
"github.com/ipfs/go-ipfs/p2p/net"
peer
"github.com/ipfs/go-ipfs/p2p/peer"
protocol
"github.com/ipfs/go-ipfs/p2p/protocol"
util
"github.com/ipfs/go-ipfs/util"
ctxutil
"github.com/ipfs/go-ipfs/util/ctx"
)
var
log
=
util
.
Logger
(
"diagnostics"
)
...
...
@@ -209,8 +208,8 @@ func (d *Diagnostics) getDiagnosticFromPeer(ctx context.Context, p peer.ID, pmes
return
nil
,
err
}
cr
:=
ctx
util
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
util
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cr
:=
ctx
io
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
io
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
r
:=
ggio
.
NewDelimitedReader
(
cr
,
inet
.
MessageSizeMax
)
w
:=
ggio
.
NewDelimitedWriter
(
cw
)
...
...
@@ -267,8 +266,8 @@ func newMessage(diagID string) *pb.Message {
func
(
d
*
Diagnostics
)
HandleMessage
(
ctx
context
.
Context
,
s
inet
.
Stream
)
error
{
cr
:=
ctx
util
.
NewReader
(
ctx
,
s
)
cw
:=
ctx
util
.
NewWriter
(
ctx
,
s
)
cr
:=
ctx
io
.
NewReader
(
ctx
,
s
)
cw
:=
ctx
io
.
NewWriter
(
ctx
,
s
)
r
:=
ggio
.
NewDelimitedReader
(
cr
,
inet
.
MessageSizeMax
)
// maxsize
w
:=
ggio
.
NewDelimitedWriter
(
cw
)
...
...
routing/dht/dht_net.go
浏览文件 @
d4656657
...
...
@@ -4,13 +4,12 @@ import (
"errors"
"time"
ggio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
ctxio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-context/io"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
inet
"github.com/ipfs/go-ipfs/p2p/net"
peer
"github.com/ipfs/go-ipfs/p2p/peer"
pb
"github.com/ipfs/go-ipfs/routing/dht/pb"
ctxutil
"github.com/ipfs/go-ipfs/util/ctx"
ggio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
// handleNewStream implements the inet.StreamHandler
...
...
@@ -22,8 +21,8 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
defer
s
.
Close
()
ctx
:=
dht
.
Context
()
cr
:=
ctx
util
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
util
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cr
:=
ctx
io
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
io
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
r
:=
ggio
.
NewDelimitedReader
(
cr
,
inet
.
MessageSizeMax
)
w
:=
ggio
.
NewDelimitedWriter
(
cw
)
mPeer
:=
s
.
Conn
()
.
RemotePeer
()
...
...
@@ -78,8 +77,8 @@ func (dht *IpfsDHT) sendRequest(ctx context.Context, p peer.ID, pmes *pb.Message
}
defer
s
.
Close
()
cr
:=
ctx
util
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
util
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cr
:=
ctx
io
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
io
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
r
:=
ggio
.
NewDelimitedReader
(
cr
,
inet
.
MessageSizeMax
)
w
:=
ggio
.
NewDelimitedWriter
(
cw
)
...
...
@@ -116,7 +115,7 @@ func (dht *IpfsDHT) sendMessage(ctx context.Context, p peer.ID, pmes *pb.Message
}
defer
s
.
Close
()
cw
:=
ctx
util
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctx
io
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
w
:=
ggio
.
NewDelimitedWriter
(
cw
)
if
err
:=
w
.
WriteMsg
(
pmes
);
err
!=
nil
{
...
...
routing/dht/records.go
浏览文件 @
d4656657
...
...
@@ -3,13 +3,13 @@ package dht
import
(
"fmt"
ctxfrac
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-context/frac"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
ci
"github.com/ipfs/go-ipfs/p2p/crypto"
peer
"github.com/ipfs/go-ipfs/p2p/peer"
routing
"github.com/ipfs/go-ipfs/routing"
pb
"github.com/ipfs/go-ipfs/routing/dht/pb"
record
"github.com/ipfs/go-ipfs/routing/record"
ctxutil
"github.com/ipfs/go-ipfs/util/ctx"
)
func
(
dht
*
IpfsDHT
)
GetPublicKey
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
ci
.
PubKey
,
error
)
{
...
...
@@ -22,7 +22,7 @@ func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, err
}
// ok, try the node itself. if they're overwhelmed or slow we can move on.
ctxT
,
cancelFunc
:=
ctx
util
.
WithDeadlineFraction
(
ctx
,
0.3
)
ctxT
,
cancelFunc
:=
ctx
frac
.
WithDeadlineFraction
(
ctx
,
0.3
)
defer
cancelFunc
()
if
pk
,
err
:=
dht
.
getPublicKeyFromNode
(
ctx
,
p
);
err
==
nil
{
err
:=
dht
.
peerstore
.
AddPubKey
(
p
,
pk
)
...
...
util/ctx/fracctx.go
deleted
100644 → 0
浏览文件 @
30509fc5
package
ctxutil
import
(
"time"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
func
WithDeadlineFraction
(
ctx
context
.
Context
,
fraction
float64
)
(
context
.
Context
,
context
.
CancelFunc
)
{
d
,
found
:=
ctx
.
Deadline
()
if
!
found
{
// no deadline
return
context
.
WithCancel
(
ctx
)
}
left
:=
d
.
Sub
(
time
.
Now
())
if
left
<
0
{
// already passed...
return
context
.
WithCancel
(
ctx
)
}
left
=
time
.
Duration
(
float64
(
left
)
*
fraction
)
return
context
.
WithTimeout
(
ctx
,
left
)
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论