-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_send.go
147 lines (127 loc) · 3.33 KB
/
handler_send.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
147
package digest
import (
"bytes"
"context"
"encoding/json"
"github.com/ProtoconNet/mitum-currency/v3/common"
"github.com/ProtoconNet/mitum2/network/quicmemberlist"
"github.com/ProtoconNet/mitum2/network/quicstream"
"io"
"net/http"
"sync"
"time"
"github.com/ProtoconNet/mitum2/base"
"github.com/pkg/errors"
)
func (hd *Handlers) handleQueueSend(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}
if _, err := io.Copy(body, r.Body); err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
var req = RequestWrapper{body: body}
hd.queue <- req
HTTP2WriteHal(hd.enc, w, NewBaseHal("Send operation successfully", HalLink{}), http.StatusOK)
}
func (hd *Handlers) handleSend(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}
if _, err := io.Copy(body, r.Body); err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
var hal Hal
var v json.RawMessage
if err := json.Unmarshal(body.Bytes(), &v); err != nil {
HTTP2ProblemWithError(w, common.ErrDecodeJson.Wrap(err), http.StatusBadRequest)
return
} else if hinter, err := hd.enc.Decode(body.Bytes()); err != nil {
nerr := err
if !errors.Is(err, common.ErrDecodeJson) {
nerr = common.ErrDecodeJson.Wrap(err)
}
HTTP2ProblemWithError(w, nerr, http.StatusBadRequest)
return
} else if h, err := hd.sendItem(hinter); err != nil {
HTTP2ProblemWithError(w, err, http.StatusBadRequest)
return
} else {
hal = h
}
HTTP2WriteHal(hd.enc, w, hal, http.StatusOK)
}
func (hd *Handlers) sendItem(v interface{}) (Hal, error) {
switch t := v.(type) {
case base.Operation:
if err := t.IsValid(hd.networkID); err != nil {
return nil, err
}
default:
return nil, errors.Errorf("Unsupported message type, %T", v)
}
return hd.sendOperation(v)
}
func (hd *Handlers) sendOperation(v interface{}) (Hal, error) {
op, ok := v.(base.Operation)
if !ok {
return nil, errors.Errorf("expected Operation, not %T", v)
}
client, memberList, nodeList, err := hd.client()
switch {
case err != nil:
return nil, err
default:
var wg sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
connInfo := make(map[string]quicstream.ConnInfo)
memberList.Members(func(node quicmemberlist.Member) bool {
connInfo[node.ConnInfo().String()] = node.ConnInfo()
return true
})
for _, c := range nodeList {
connInfo[c.String()] = c
}
errCh := make(chan error, len(connInfo))
sentCh := make(chan bool, len(connInfo))
for _, ci := range connInfo {
wg.Add(1)
go func(node quicstream.ConnInfo) {
defer wg.Done()
sent, err := client.SendOperation(ctx, node, op)
if err != nil {
errCh <- err
}
if sent {
sentCh <- sent
}
}(ci)
}
wg.Wait()
close(errCh)
close(sentCh)
var errList []error
var sentList []bool
for err := range errCh {
if err != nil {
errList = append(errList, err)
}
}
for sent := range sentCh {
if sent {
sentList = append(sentList, sent)
}
}
if len(sentList) < 1 {
if len(errList) > 0 {
return nil, errList[0]
} else {
return nil, errors.Errorf("Failed to send operation to node")
}
}
}
return hd.buildSealHal(op)
}
func (hd *Handlers) buildSealHal(op base.Operation) (Hal, error) {
var hal Hal = NewBaseHal(op, HalLink{})
return hal, nil
}