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
p2p
go-p2p-kad-dht
Commits
a92f79b9
Unverified
Commit
a92f79b9
authored
Mar 04, 2020
by
Steven Allen
Committed by
GitHub
Mar 04, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #477 from libp2p/feat/cancel-dht-context
Close context correctly
parents
da53c0b6
14422102
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
9 deletions
+37
-9
dht.go
dht.go
+13
-9
dht_test.go
dht_test.go
+24
-0
No files found.
dht.go
View file @
a92f79b9
...
...
@@ -111,12 +111,6 @@ func New(ctx context.Context, h host.Host, options ...opts.Option) (*IpfsDHT, er
// register for network notifs.
dht
.
host
.
Network
()
.
Notify
((
*
netNotifiee
)(
dht
))
dht
.
proc
=
goprocessctx
.
WithContextAndTeardown
(
ctx
,
func
()
error
{
// remove ourselves from network notifs.
dht
.
host
.
Network
()
.
StopNotify
((
*
netNotifiee
)(
dht
))
return
nil
})
dht
.
proc
.
AddChild
(
dht
.
providers
.
Process
())
dht
.
Validator
=
cfg
.
Validator
...
...
@@ -172,8 +166,6 @@ func makeDHT(ctx context.Context, h host.Host, cfg *opts.Options) *IpfsDHT {
peerstore
:
h
.
Peerstore
(),
host
:
h
,
strmap
:
make
(
map
[
peer
.
ID
]
*
messageSender
),
ctx
:
ctx
,
providers
:
providers
.
NewProviderManager
(
ctx
,
h
.
ID
(),
cfg
.
Datastore
),
birth
:
time
.
Now
(),
routingTable
:
rt
,
protocols
:
cfg
.
Protocols
,
...
...
@@ -181,7 +173,19 @@ func makeDHT(ctx context.Context, h host.Host, cfg *opts.Options) *IpfsDHT {
triggerRtRefresh
:
make
(
chan
chan
<-
error
),
}
dht
.
ctx
=
dht
.
newContextWithLocalTags
(
ctx
)
// create a DHT proc with the given teardown
dht
.
proc
=
goprocess
.
WithTeardown
(
func
()
error
{
// remove ourselves from network notifs.
dht
.
host
.
Network
()
.
StopNotify
((
*
netNotifiee
)(
dht
))
return
nil
})
// create a tagged context derived from the original context
ctxTags
:=
dht
.
newContextWithLocalTags
(
ctx
)
// the DHT context should be done when the process is closed
dht
.
ctx
=
goprocessctx
.
WithProcessClosing
(
ctxTags
,
dht
.
proc
)
dht
.
providers
=
providers
.
NewProviderManager
(
dht
.
ctx
,
h
.
ID
(),
cfg
.
Datastore
)
return
dht
}
...
...
dht_test.go
View file @
a92f79b9
...
...
@@ -402,6 +402,30 @@ func TestValueSetInvalid(t *testing.T) {
testSetGet
(
"valid"
,
true
,
"newer"
,
nil
)
}
func
TestContextShutDown
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
dht
:=
setupDHT
(
ctx
,
t
,
false
)
// context is alive
select
{
case
<-
dht
.
Context
()
.
Done
()
:
t
.
Fatal
(
"context should not be done"
)
default
:
}
// shut down dht
require
.
NoError
(
t
,
dht
.
Close
())
// now context should be done
select
{
case
<-
dht
.
Context
()
.
Done
()
:
default
:
t
.
Fatal
(
"context should be done"
)
}
}
func
TestSearchValue
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
...
...
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