-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_response.go
110 lines (83 loc) · 1.97 KB
/
request_response.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
package quasar
import (
"bufio"
"errors"
"fmt"
"time"
pb "github.com/Bit-Nation/go-libp2p-quasar/pb"
net "github.com/libp2p/go-libp2p-net"
protoEnc "github.com/multiformats/go-multicodec/protobuf"
uuid "github.com/satori/go.uuid"
)
type response struct {
msg *pb.Message
err error
}
type request struct {
msg *pb.Message
respChan chan response
}
func (p *protocol) addRequest(req request) {
p.lock.Lock()
p.requests[req.msg.RequestID] = req
p.lock.Unlock()
}
// cut request from internal stack
func (p *protocol) cutRequest(id string) (request, error) {
p.lock.Lock()
req, exist := p.requests[id]
// in case it exist we want to delete it to free the map
if exist {
delete(p.requests, id)
}
p.lock.Unlock()
if !exist {
return request{}, errors.New(fmt.Sprintf("couldn't find request for ID: %s", id))
}
return req, nil
}
// respond to request
func (p *protocol) respond(id string, response *pb.Message, str net.Stream) error {
w := bufio.NewWriter(str)
enc := protoEnc.Multicodec(nil).Encoder(w)
response.RequestID = id
// encode msg and send it
err := enc.Encode(response)
if err != nil {
return err
}
return w.Flush()
}
// send a message
func (p *protocol) request(msg *pb.Message, str net.Stream, timeOut time.Duration) (response, error) {
w := bufio.NewWriter(str)
enc := protoEnc.Multicodec(nil).Encoder(w)
// resp chan
c := make(chan response)
// create request id
id, err := uuid.NewV4()
if err != nil {
return response{}, err
}
msg.RequestID = id.String()
// encode msg and send it
err = enc.Encode(msg)
if err != nil {
return response{}, err
}
if err := w.Flush(); err != nil {
return response{}, err
}
p.addRequest(request{
msg: msg,
respChan: c,
})
select {
case res := <-c:
return res, nil
case <-time.After(timeOut):
// but the request from stack which will remove it
p.cutRequest(msg.RequestID)
return response{}, errors.New(fmt.Sprintf("request timeout for ID: %s", msg.RequestID))
}
}