Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
00d7b498
Commit
00d7b498
authored
Jan 24, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
routing/dht: adjust routing table on peer conn/disc
parent
4ae01e7a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
1 deletion
+69
-1
routing/dht/dht.go
routing/dht/dht.go
+9
-1
routing/dht/notif.go
routing/dht/notif.go
+33
-0
routing/kbucket/bucket.go
routing/kbucket/bucket.go
+10
-0
routing/kbucket/table.go
routing/kbucket/table.go
+17
-0
No files found.
routing/dht/dht.go
View file @
00d7b498
...
@@ -65,9 +65,17 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
...
@@ -65,9 +65,17 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
dht
.
datastore
=
dstore
dht
.
datastore
=
dstore
dht
.
self
=
h
.
ID
()
dht
.
self
=
h
.
ID
()
dht
.
peerstore
=
h
.
Peerstore
()
dht
.
peerstore
=
h
.
Peerstore
()
dht
.
ContextGroup
=
ctxgroup
.
WithContext
(
ctx
)
dht
.
host
=
h
dht
.
host
=
h
// register for network notifs.
dht
.
host
.
Network
()
.
Notify
((
*
netNotifiee
)(
dht
))
dht
.
ContextGroup
=
ctxgroup
.
WithContextAndTeardown
(
ctx
,
func
()
error
{
// remove ourselves from network notifs.
dht
.
host
.
Network
()
.
StopNotify
((
*
netNotifiee
)(
dht
))
return
nil
})
// sanity check. this should **never** happen
// sanity check. this should **never** happen
if
len
(
dht
.
peerstore
.
Addresses
(
dht
.
self
))
<
1
{
if
len
(
dht
.
peerstore
.
Addresses
(
dht
.
self
))
<
1
{
panic
(
"attempt to initialize dht without addresses for self"
)
panic
(
"attempt to initialize dht without addresses for self"
)
...
...
routing/dht/notif.go
0 → 100644
View file @
00d7b498
package
dht
import
(
inet
"github.com/jbenet/go-ipfs/p2p/net"
)
// netNotifiee defines methods to be used with the IpfsDHT
type
netNotifiee
IpfsDHT
func
(
nn
*
netNotifiee
)
DHT
()
*
IpfsDHT
{
return
(
*
IpfsDHT
)(
nn
)
}
func
(
nn
*
netNotifiee
)
Connected
(
n
inet
.
Network
,
v
inet
.
Conn
)
{
dht
:=
nn
.
DHT
()
select
{
case
<-
dht
.
Closing
()
:
return
}
dht
.
Update
(
dht
.
Context
(),
v
.
RemotePeer
())
}
func
(
nn
*
netNotifiee
)
Disconnected
(
n
inet
.
Network
,
v
inet
.
Conn
)
{
dht
:=
nn
.
DHT
()
select
{
case
<-
dht
.
Closing
()
:
return
}
dht
.
routingTable
.
Remove
(
v
.
RemotePeer
())
}
func
(
nn
*
netNotifiee
)
OpenedStream
(
n
inet
.
Network
,
v
inet
.
Stream
)
{}
func
(
nn
*
netNotifiee
)
ClosedStream
(
n
inet
.
Network
,
v
inet
.
Stream
)
{}
routing/kbucket/bucket.go
View file @
00d7b498
...
@@ -30,6 +30,16 @@ func (b *Bucket) find(id peer.ID) *list.Element {
...
@@ -30,6 +30,16 @@ func (b *Bucket) find(id peer.ID) *list.Element {
return
nil
return
nil
}
}
func
(
b
*
Bucket
)
remove
(
id
peer
.
ID
)
{
b
.
lk
.
RLock
()
defer
b
.
lk
.
RUnlock
()
for
e
:=
b
.
list
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
if
e
.
Value
.
(
peer
.
ID
)
==
id
{
b
.
list
.
Remove
(
e
)
}
}
}
func
(
b
*
Bucket
)
moveToFront
(
e
*
list
.
Element
)
{
func
(
b
*
Bucket
)
moveToFront
(
e
*
list
.
Element
)
{
b
.
lk
.
Lock
()
b
.
lk
.
Lock
()
b
.
list
.
MoveToFront
(
e
)
b
.
list
.
MoveToFront
(
e
)
...
...
routing/kbucket/table.go
View file @
00d7b498
...
@@ -87,6 +87,23 @@ func (rt *RoutingTable) Update(p peer.ID) peer.ID {
...
@@ -87,6 +87,23 @@ func (rt *RoutingTable) Update(p peer.ID) peer.ID {
return
""
return
""
}
}
// Remove deletes a peer from the routing table. This is to be used
// when we are sure a node has disconnected completely.
func
(
rt
*
RoutingTable
)
Remove
(
p
peer
.
ID
)
{
rt
.
tabLock
.
Lock
()
defer
rt
.
tabLock
.
Unlock
()
peerID
:=
ConvertPeerID
(
p
)
cpl
:=
commonPrefixLen
(
peerID
,
rt
.
local
)
bucketID
:=
cpl
if
bucketID
>=
len
(
rt
.
Buckets
)
{
bucketID
=
len
(
rt
.
Buckets
)
-
1
}
bucket
:=
rt
.
Buckets
[
bucketID
]
bucket
.
remove
(
p
)
}
func
(
rt
*
RoutingTable
)
nextBucket
()
peer
.
ID
{
func
(
rt
*
RoutingTable
)
nextBucket
()
peer
.
ID
{
bucket
:=
rt
.
Buckets
[
len
(
rt
.
Buckets
)
-
1
]
bucket
:=
rt
.
Buckets
[
len
(
rt
.
Buckets
)
-
1
]
newBucket
:=
bucket
.
Split
(
len
(
rt
.
Buckets
)
-
1
,
rt
.
local
)
newBucket
:=
bucket
.
Split
(
len
(
rt
.
Buckets
)
-
1
,
rt
.
local
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment