ping.go 2.18 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
package ping

import (
	"bytes"
5
	"context"
Jeromy's avatar
Jeromy committed
6
7
8
9
	"errors"
	"io"
	"time"

Jeromy's avatar
Jeromy committed
10
11
	u "github.com/ipfs/go-ipfs-util"
	logging "github.com/ipfs/go-log"
Jeromy's avatar
Jeromy committed
12
	host "github.com/libp2p/go-libp2p-host"
Jeromy's avatar
Jeromy committed
13
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
14
	peer "github.com/libp2p/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
15
16
)

Jeromy's avatar
Jeromy committed
17
var log = logging.Logger("ping")
Jeromy's avatar
Jeromy committed
18
19
20

const PingSize = 32

Jeromy's avatar
Jeromy committed
21
const ID = "/ipfs/ping/1.0.0"
Jeromy's avatar
Jeromy committed
22

23
24
const pingTimeout = time.Second * 60

Jeromy's avatar
Jeromy committed
25
26
27
28
29
30
31
32
33
34
35
36
37
type PingService struct {
	Host host.Host
}

func NewPingService(h host.Host) *PingService {
	ps := &PingService{h}
	h.SetStreamHandler(ID, ps.PingHandler)
	return ps
}

func (p *PingService) PingHandler(s inet.Stream) {
	buf := make([]byte, PingSize)

Steven Allen's avatar
Steven Allen committed
38
39
	errCh := make(chan error, 1)
	defer close(errCh)
40
41
42
43
44
45
	timer := time.NewTimer(pingTimeout)
	defer timer.Stop()

	go func() {
		select {
		case <-timer.C:
Steven Allen's avatar
Steven Allen committed
46
47
48
49
50
51
52
53
54
55
56
57
58
			log.Debug("ping timeout")
			s.Reset()
		case err, ok := <-errCh:
			if ok {
				log.Debug(err)
				if err == io.EOF {
					s.Close()
				} else {
					s.Reset()
				}
			} else {
				log.Error("ping loop failed without error")
			}
59
60
61
		}
	}()

Jeromy's avatar
Jeromy committed
62
63
64
	for {
		_, err := io.ReadFull(s, buf)
		if err != nil {
Steven Allen's avatar
Steven Allen committed
65
			errCh <- err
Jeromy's avatar
Jeromy committed
66
67
68
69
70
			return
		}

		_, err = s.Write(buf)
		if err != nil {
Steven Allen's avatar
Steven Allen committed
71
			errCh <- err
Jeromy's avatar
Jeromy committed
72
73
			return
		}
74
75

		timer.Reset(pingTimeout)
Jeromy's avatar
Jeromy committed
76
77
78
79
	}
}

func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duration, error) {
80
	s, err := ps.Host.NewStream(ctx, p, ID)
Jeromy's avatar
Jeromy committed
81
82
83
84
85
86
87
	if err != nil {
		return nil, err
	}

	out := make(chan time.Duration)
	go func() {
		defer close(out)
88
		defer s.Close()
Jeromy's avatar
Jeromy committed
89
90
91
92
93
94
95
		for {
			select {
			case <-ctx.Done():
				return
			default:
				t, err := ping(s)
				if err != nil {
Steven Allen's avatar
Steven Allen committed
96
					s.Reset()
Jeromy's avatar
Jeromy committed
97
98
99
100
					log.Debugf("ping error: %s", err)
					return
				}

Jeromy's avatar
Jeromy committed
101
				ps.Host.Peerstore().RecordLatency(p, t)
Jeromy's avatar
Jeromy committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
				select {
				case out <- t:
				case <-ctx.Done():
					return
				}
			}
		}
	}()

	return out, nil
}

func ping(s inet.Stream) (time.Duration, error) {
	buf := make([]byte, PingSize)
	u.NewTimeSeededRand().Read(buf)

	before := time.Now()
	_, err := s.Write(buf)
	if err != nil {
		return 0, err
	}

	rbuf := make([]byte, PingSize)
	_, err = io.ReadFull(s, rbuf)
	if err != nil {
		return 0, err
	}

	if !bytes.Equal(buf, rbuf) {
		return 0, errors.New("ping packet was incorrect!")
	}

John Steidley's avatar
John Steidley committed
134
	return time.Since(before), nil
Jeromy's avatar
Jeromy committed
135
}