-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_request.go
57 lines (45 loc) · 1.3 KB
/
node_request.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
package zstack
import (
"context"
"errors"
)
func AnyResponse(v interface{}) bool {
return true
}
var ReplyDoesNotReportSuccess = errors.New("reply struct does not support Successor interface")
var NodeResponseWasNotSuccess = errors.New("response from node was not success")
func (z *ZStack) nodeRequest(ctx context.Context, request interface{}, reply interface{}, response interface{}, responseFilter func(interface{}) bool) (interface{}, error) {
replySuccessor, replySupportsSuccessor := reply.(Successor)
if !replySupportsSuccessor {
return nil, ReplyDoesNotReportSuccess
}
ch := make(chan interface{})
err, stop := z.subscriber.Subscribe(response, func(v interface{}) {
if responseFilter(v) {
select {
case ch <- v:
case <-ctx.Done():
}
}
})
defer stop()
if err != nil {
return nil, err
}
if err := z.requestResponder.RequestResponse(ctx, request, reply); err != nil {
return nil, err
}
if !replySuccessor.WasSuccessful() {
return nil, ErrorZFailure
}
select {
case v := <-ch:
responseSuccessor, responseSupportsSuccessor := v.(Successor)
if responseSupportsSuccessor && !responseSuccessor.WasSuccessful() {
return v, NodeResponseWasNotSuccess
}
return v, nil
case <-ctx.Done():
return nil, errors.New("context expired while waiting for response from node")
}
}