-
Notifications
You must be signed in to change notification settings - Fork 337
/
pingpong.go
126 lines (107 loc) · 3.03 KB
/
pingpong.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package pingpong exposes the simple ping-pong protocol
// which measures round-trip-time with other peers.
package pingpong
import (
"context"
"fmt"
"io"
"time"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/pingpong/pb"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tracing"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "pinpong"
const (
protocolName = "pingpong"
protocolVersion = "1.0.0"
streamName = "pingpong"
)
type Interface interface {
Ping(ctx context.Context, address swarm.Address, msgs ...string) (rtt time.Duration, err error)
}
type Service struct {
streamer p2p.Streamer
logger log.Logger
tracer *tracing.Tracer
metrics metrics
}
func New(streamer p2p.Streamer, logger log.Logger, tracer *tracing.Tracer) *Service {
return &Service{
streamer: streamer,
logger: logger.WithName(loggerName).Register(),
tracer: tracer,
metrics: newMetrics(),
}
}
func (s *Service) Protocol() p2p.ProtocolSpec {
return p2p.ProtocolSpec{
Name: protocolName,
Version: protocolVersion,
StreamSpecs: []p2p.StreamSpec{
{
Name: streamName,
Handler: s.handler,
},
},
}
}
func (s *Service) Ping(ctx context.Context, address swarm.Address, msgs ...string) (rtt time.Duration, err error) {
span, _, ctx := s.tracer.StartSpanFromContext(ctx, "pingpong-p2p-ping", s.logger)
defer span.Finish()
start := time.Now()
stream, err := s.streamer.NewStream(ctx, address, nil, protocolName, protocolVersion, streamName)
if err != nil {
return 0, fmt.Errorf("new stream: %w", err)
}
defer func() {
go stream.FullClose()
}()
w, r := protobuf.NewWriterAndReader(stream)
var pong pb.Pong
for _, msg := range msgs {
if err := w.WriteMsgWithContext(ctx, &pb.Ping{
Greeting: msg,
}); err != nil {
return 0, fmt.Errorf("write message: %w", err)
}
s.metrics.PingSentCount.Inc()
if err := r.ReadMsgWithContext(ctx, &pong); err != nil {
if err == io.EOF {
break
}
return 0, fmt.Errorf("read message: %w", err)
}
s.metrics.PongReceivedCount.Inc()
}
return time.Since(start), nil
}
func (s *Service) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) error {
w, r := protobuf.NewWriterAndReader(stream)
defer stream.FullClose()
span, _, ctx := s.tracer.StartSpanFromContext(ctx, "pingpong-p2p-handler", s.logger)
defer span.Finish()
var ping pb.Ping
for {
if err := r.ReadMsgWithContext(ctx, &ping); err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("read message: %w", err)
}
s.metrics.PingReceivedCount.Inc()
if err := w.WriteMsgWithContext(ctx, &pb.Pong{
Response: "{" + ping.Greeting + "}",
}); err != nil {
return fmt.Errorf("write message: %w", err)
}
s.metrics.PongSentCount.Inc()
}
return nil
}