-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfetch.go
146 lines (116 loc) · 2.94 KB
/
fetch.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package namesys
import (
"context"
"errors"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-msgio/protoio"
pb "github.com/libp2p/go-libp2p-pubsub-router/pb"
"github.com/gogo/protobuf/proto"
)
const FetchProtoID = protocol.ID("/libp2p/fetch/0.0.1")
type fetchProtocol struct {
ctx context.Context
host host.Host
}
type getValue func(ctx context.Context, key string) ([]byte, error)
func newFetchProtocol(ctx context.Context, host host.Host, getData getValue) *fetchProtocol {
p := &fetchProtocol{ctx, host}
host.SetStreamHandler(FetchProtoID, func(s network.Stream) {
p.receive(s, getData)
})
return p
}
func (p *fetchProtocol) receive(s network.Stream, getData getValue) {
defer s.Close()
msg := &pb.FetchRequest{}
if err := readMsg(p.ctx, s, msg); err != nil {
log.Infof("error reading request from %s: %s", s.Conn().RemotePeer(), err)
s.Reset()
return
}
response, err := getData(p.ctx, msg.Identifier)
var respProto pb.FetchResponse
if err != nil {
respProto = pb.FetchResponse{Status: pb.FetchResponse_NOT_FOUND}
} else {
respProto = pb.FetchResponse{Data: response}
}
if err := writeMsg(p.ctx, s, &respProto); err != nil {
s.Reset()
return
}
}
func (p *fetchProtocol) Fetch(ctx context.Context, pid peer.ID, key string) ([]byte, error) {
peerCtx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
s, err := p.host.NewStream(peerCtx, pid, FetchProtoID)
if err != nil {
return nil, err
}
defer s.Close()
msg := &pb.FetchRequest{Identifier: key}
if err := writeMsg(ctx, s, msg); err != nil {
_ = s.Reset()
return nil, err
}
if err := s.CloseWrite(); err != nil {
_ = s.Reset()
return nil, err
}
response := &pb.FetchResponse{}
if err := readMsg(ctx, s, response); err != nil {
_ = s.Reset()
return nil, err
}
switch response.Status {
case pb.FetchResponse_OK:
return response.Data, nil
case pb.FetchResponse_NOT_FOUND:
return nil, nil
default:
return nil, errors.New("fetch: received unknown status code")
}
}
func writeMsg(ctx context.Context, s network.Stream, msg proto.Message) error {
done := make(chan error, 1)
go func() {
wc := protoio.NewDelimitedWriter(s)
if err := wc.WriteMsg(msg); err != nil {
done <- err
return
}
done <- nil
}()
var retErr error
select {
case retErr = <-done:
case <-ctx.Done():
retErr = ctx.Err()
}
if retErr != nil {
log.Infof("error writing response to %s: %s", s.Conn().RemotePeer(), retErr)
}
return retErr
}
func readMsg(ctx context.Context, s network.Stream, msg proto.Message) error {
done := make(chan error, 1)
go func() {
r := protoio.NewDelimitedReader(s, 1<<20)
if err := r.ReadMsg(msg); err != nil {
done <- err
return
}
done <- nil
}()
select {
case err := <-done:
return err
case <-ctx.Done():
s.Reset()
return ctx.Err()
}
}