Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
71aefe1a
Commit
71aefe1a
authored
May 30, 2016
by
Jeromy Johnson
Browse files
Merge pull request #58 from ipfs/fix/ping-leak
don't leak goroutines when pings complete
parents
97fbccff
f483c471
Changes
1
Hide whitespace changes
Inline
Side-by-side
p2p/protocol/ping/ping.go
View file @
71aefe1a
...
...
@@ -20,6 +20,8 @@ const PingSize = 32
const
ID
=
"/ipfs/ping/1.0.0"
const
pingTimeout
=
time
.
Second
*
60
type
PingService
struct
{
Host
host
.
Host
}
...
...
@@ -31,8 +33,23 @@ func NewPingService(h host.Host) *PingService {
}
func
(
p
*
PingService
)
PingHandler
(
s
inet
.
Stream
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
buf
:=
make
([]
byte
,
PingSize
)
timer
:=
time
.
NewTimer
(
pingTimeout
)
defer
timer
.
Stop
()
go
func
()
{
select
{
case
<-
timer
.
C
:
case
<-
ctx
.
Done
()
:
}
s
.
Close
()
}()
for
{
_
,
err
:=
io
.
ReadFull
(
s
,
buf
)
if
err
!=
nil
{
...
...
@@ -45,6 +62,8 @@ func (p *PingService) PingHandler(s inet.Stream) {
log
.
Debug
(
err
)
return
}
timer
.
Reset
(
pingTimeout
)
}
}
...
...
@@ -57,6 +76,7 @@ func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duratio
out
:=
make
(
chan
time
.
Duration
)
go
func
()
{
defer
close
(
out
)
defer
s
.
Close
()
for
{
select
{
case
<-
ctx
.
Done
()
:
...
...
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