Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
c22b6aa3
提交
c22b6aa3
authored
8月 06, 2014
作者:
Jeromy
提交者:
Juan Batiz-Benet
8月 07, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixing some race conditions
上级
41c124a2
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
42 行增加
和
4 行删除
+42
-4
peer.go
peer/peer.go
+14
-1
dht.go
routing/dht/dht.go
+1
-0
routing.go
routing/dht/routing.go
+8
-2
table.go
routing/dht/table.go
+19
-1
没有找到文件。
peer/peer.go
浏览文件 @
c22b6aa3
...
...
@@ -3,6 +3,7 @@ package peer
import
(
"encoding/hex"
"time"
"sync"
u
"github.com/jbenet/go-ipfs/util"
ma
"github.com/jbenet/go-multiaddr"
...
...
@@ -31,7 +32,9 @@ type Map map[u.Key]*Peer
type
Peer
struct
{
ID
ID
Addresses
[]
*
ma
.
Multiaddr
Distance
time
.
Duration
distance
time
.
Duration
distLock
sync
.
RWMutex
}
// Key returns the ID as a Key (string) for maps.
...
...
@@ -60,3 +63,13 @@ func (p *Peer) NetAddress(n string) *ma.Multiaddr {
}
return
nil
}
func
(
p
*
Peer
)
GetDistance
()
time
.
Duration
{
return
p
.
distance
}
func
(
p
*
Peer
)
SetDistance
(
dist
time
.
Duration
)
{
p
.
distLock
.
Lock
()
p
.
distance
=
dist
p
.
distLock
.
Unlock
()
}
routing/dht/dht.go
浏览文件 @
c22b6aa3
...
...
@@ -186,6 +186,7 @@ func (dht *IpfsDHT) handleMessages() {
case
err
:=
<-
dht
.
network
.
Chan
.
Errors
:
u
.
DErr
(
"dht err: %s"
,
err
)
panic
(
err
)
case
<-
dht
.
shutdown
:
checkTimeouts
.
Stop
()
return
...
...
routing/dht/routing.go
浏览文件 @
c22b6aa3
...
...
@@ -48,6 +48,8 @@ func (s *IpfsDHT) PutValue(key u.Key, value []byte) error {
}
// GetValue searches for the value corresponding to given Key.
// If the search does not succeed, a multiaddr string of a closer peer is
// returned along with util.ErrSearchIncomplete
func
(
s
*
IpfsDHT
)
GetValue
(
key
u
.
Key
,
timeout
time
.
Duration
)
([]
byte
,
error
)
{
var
p
*
peer
.
Peer
p
=
s
.
routes
.
NearestPeer
(
convertKey
(
key
))
...
...
@@ -77,7 +79,11 @@ func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
if
err
!=
nil
{
return
nil
,
err
}
return
pmes_out
.
GetValue
(),
nil
if
pmes_out
.
GetSuccess
()
{
return
pmes_out
.
GetValue
(),
nil
}
else
{
return
pmes_out
.
GetValue
(),
u
.
ErrSearchIncomplete
}
}
}
...
...
@@ -225,7 +231,7 @@ func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
select
{
case
<-
response_chan
:
roundtrip
:=
time
.
Since
(
before
)
p
.
Distance
=
roundtrip
//TODO: This isnt threadsafe
p
.
SetDistance
(
roundtrip
)
u
.
POut
(
"Ping took %s."
,
roundtrip
.
String
())
return
nil
case
<-
tout
:
...
...
routing/dht/table.go
浏览文件 @
c22b6aa3
...
...
@@ -3,8 +3,10 @@ package dht
import
(
"container/list"
"sort"
"sync"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
)
// RoutingTable defines the routing table.
...
...
@@ -13,6 +15,9 @@ type RoutingTable struct {
// ID of the local peer
local
ID
// Blanket lock, refine later for better performance
tabLock
sync
.
RWMutex
// kBuckets define all the fingers to other nodes.
Buckets
[]
*
Bucket
bucketsize
int
...
...
@@ -29,6 +34,8 @@ func NewRoutingTable(bucketsize int, local_id ID) *RoutingTable {
// Update adds or moves the given peer to the front of its respective bucket
// If a peer gets removed from a bucket, it is returned
func
(
rt
*
RoutingTable
)
Update
(
p
*
peer
.
Peer
)
*
peer
.
Peer
{
rt
.
tabLock
.
Lock
()
defer
rt
.
tabLock
.
Unlock
()
peer_id
:=
convertPeerID
(
p
.
ID
)
cpl
:=
xor
(
peer_id
,
rt
.
local
)
.
commonPrefixLen
()
...
...
@@ -88,7 +95,11 @@ func (p peerSorterArr) Less(a, b int) bool {
//
func
copyPeersFromList
(
target
ID
,
peerArr
peerSorterArr
,
peerList
*
list
.
List
)
peerSorterArr
{
for
e
:=
peerList
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
if
peerList
==
nil
{
return
peerSorterArr
{}
}
e
:=
peerList
.
Front
()
for
;
e
!=
nil
;
{
p
:=
e
.
Value
.
(
*
peer
.
Peer
)
p_id
:=
convertPeerID
(
p
.
ID
)
pd
:=
peerDistance
{
...
...
@@ -96,6 +107,11 @@ func copyPeersFromList(target ID, peerArr peerSorterArr, peerList *list.List) pe
distance
:
xor
(
target
,
p_id
),
}
peerArr
=
append
(
peerArr
,
&
pd
)
if
e
!=
nil
{
u
.
POut
(
"list element was nil."
)
return
peerArr
}
e
=
e
.
Next
()
}
return
peerArr
}
...
...
@@ -112,6 +128,8 @@ func (rt *RoutingTable) NearestPeer(id ID) *peer.Peer {
// Returns a list of the 'count' closest peers to the given ID
func
(
rt
*
RoutingTable
)
NearestPeers
(
id
ID
,
count
int
)
[]
*
peer
.
Peer
{
rt
.
tabLock
.
RLock
()
defer
rt
.
tabLock
.
RUnlock
()
cpl
:=
xor
(
id
,
rt
.
local
)
.
commonPrefixLen
()
// Get bucket at cpl index or last bucket
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论