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
d961d98f
Commit
d961d98f
authored
Mar 15, 2017
by
Hector Sanjuan
Browse files
protocol/ping: make golint happier
License: MIT Signed-off-by:
Hector Sanjuan
<
hector@protocol.ai
>
parent
613ed7ed
Changes
1
Hide whitespace changes
Inline
Side-by-side
p2p/protocol/ping/ping.go
View file @
d961d98f
// Package ping provides a ping service for libp2p hosts. It allows to measure
// round-trip latencies by sending data to a destination which echoes it
// back to the source.
package
ping
import
(
...
...
@@ -16,22 +19,29 @@ import (
var
log
=
logging
.
Logger
(
"ping"
)
// PingSize determines the size of the data written to the inet.Stream.
const
PingSize
=
32
// ID is the protocol ID for the PingService
const
ID
=
"/ipfs/ping/1.0.0"
const
pingTimeout
=
time
.
Second
*
60
// PingService enables sending and responding to Ping requests.
type
PingService
struct
{
Host
host
.
Host
}
// NewPingService creates a PinService on the given
// host by enabling it to perform and respond to pings.
func
NewPingService
(
h
host
.
Host
)
*
PingService
{
ps
:=
&
PingService
{
h
}
h
.
SetStreamHandler
(
ID
,
ps
.
PingHandler
)
return
ps
}
// PingHandler is a Stream handler which reads data from a
// stream and echoes it back.
func
(
p
*
PingService
)
PingHandler
(
s
inet
.
Stream
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
...
...
@@ -67,8 +77,11 @@ func (p *PingService) PingHandler(s inet.Stream) {
}
}
func
(
ps
*
PingService
)
Ping
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
<-
chan
time
.
Duration
,
error
)
{
s
,
err
:=
ps
.
Host
.
NewStream
(
ctx
,
p
,
ID
)
// Ping triggers pings to a given peer. It provides a from which latencies
// for each ping can be read. Pings happen continuosly until the given context
// is cancelled.
func
(
p
*
PingService
)
Ping
(
ctx
context
.
Context
,
pid
peer
.
ID
)
(
<-
chan
time
.
Duration
,
error
)
{
s
,
err
:=
p
.
Host
.
NewStream
(
ctx
,
pid
,
ID
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -88,7 +101,7 @@ func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duratio
return
}
p
s
.
Host
.
Peerstore
()
.
RecordLatency
(
p
,
t
)
p
.
Host
.
Peerstore
()
.
RecordLatency
(
p
id
,
t
)
select
{
case
out
<-
t
:
case
<-
ctx
.
Done
()
:
...
...
@@ -118,7 +131,7 @@ func ping(s inet.Stream) (time.Duration, error) {
}
if
!
bytes
.
Equal
(
buf
,
rbuf
)
{
return
0
,
errors
.
New
(
"ping packet was incorrect
!
"
)
return
0
,
errors
.
New
(
"ping packet was incorrect"
)
}
return
time
.
Since
(
before
),
nil
...
...
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