-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
191 lines (158 loc) · 5.07 KB
/
call.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright 2019 Insolar Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package api
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/insolar/insolar/api/seedmanager"
"github.com/insolar/insolar/application/extractor"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/core/reply"
"github.com/insolar/insolar/core/utils"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/instracer"
"github.com/insolar/insolar/metrics"
"github.com/pkg/errors"
)
// Request is a representation of request struct to api
type Request struct {
Reference string `json:"reference"`
Method string `json:"method"`
Params []byte `json:"params"`
Seed []byte `json:"seed"`
Signature []byte `json:"signature"`
}
type answer struct {
Error string `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
TraceID string `json:"traceID,omitempty"`
}
// UnmarshalRequest unmarshals request to api
func UnmarshalRequest(req *http.Request, params interface{}) ([]byte, error) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, errors.Wrap(err, "[ UnmarshalRequest ] Can't read body. So strange")
}
if len(body) == 0 {
return nil, errors.New("[ UnmarshalRequest ] Empty body")
}
err = json.Unmarshal(body, ¶ms)
if err != nil {
return body, errors.Wrap(err, "[ UnmarshalRequest ] Can't unmarshal input params")
}
return body, nil
}
func (ar *Runner) checkSeed(paramsSeed []byte) error {
seed := seedmanager.SeedFromBytes(paramsSeed)
if seed == nil {
return errors.New("[ checkSeed ] Bad seed param")
}
if !ar.SeedManager.Exists(*seed) {
return errors.New("[ checkSeed ] Incorrect seed")
}
return nil
}
func (ar *Runner) makeCall(ctx context.Context, params Request) (interface{}, error) {
ctx, span := instracer.StartSpan(ctx, "SendRequest "+params.Method)
defer span.End()
reference, err := core.NewRefFromBase58(params.Reference)
if err != nil {
return nil, errors.Wrap(err, "[ makeCall ] failed to parse params.Reference")
}
res, err := ar.ContractRequester.SendRequest(
ctx,
reference,
"Call",
[]interface{}{*ar.CertificateManager.GetCertificate().GetRootDomainReference(), params.Method, params.Params, params.Seed, params.Signature},
)
if err != nil {
return nil, errors.Wrap(err, "[ makeCall ] Can't send request")
}
result, contractErr, err := extractor.CallResponse(res.(*reply.CallMethod).Result)
if err != nil {
return nil, errors.Wrap(err, "[ makeCall ] Can't extract response")
}
if contractErr != nil {
return nil, errors.Wrap(errors.New(contractErr.S), "[ makeCall ] Error in called method")
}
return result, nil
}
func processError(err error, extraMsg string, resp *answer, insLog core.Logger) {
resp.Error = err.Error()
insLog.Error(errors.Wrapf(err, "[ CallHandler ] %s", extraMsg))
}
func (ar *Runner) callHandler() func(http.ResponseWriter, *http.Request) {
return func(response http.ResponseWriter, req *http.Request) {
traceID := utils.RandTraceID()
ctx, insLog := inslogger.WithTraceField(context.Background(), traceID)
ctx, span := instracer.StartSpan(ctx, "callHandler")
defer span.End()
params := Request{}
resp := answer{}
startTime := time.Now()
defer func() {
success := "success"
if resp.Error != "" {
success = "fail"
}
metrics.APIContractExecutionTime.WithLabelValues(params.Method, success).Observe(time.Since(startTime).Seconds())
}()
resp.TraceID = traceID
insLog.Infof("[ callHandler ] Incoming request: %s", req.RequestURI)
defer func() {
res, err := json.MarshalIndent(resp, "", " ")
if err != nil {
res = []byte(`{"error": "can't marshal answer to json'"}`)
}
response.Header().Add("Content-Type", "application/json")
_, err = response.Write(res)
if err != nil {
insLog.Errorf("Can't write response\n")
}
}()
_, err := UnmarshalRequest(req, ¶ms)
if err != nil {
processError(err, "Can't unmarshal request", &resp, insLog)
return
}
err = ar.checkSeed(params.Seed)
if err != nil {
processError(err, "Can't checkSeed", &resp, insLog)
return
}
var result interface{}
ch := make(chan interface{}, 1)
go func() {
result, err = ar.makeCall(ctx, params)
ch <- nil
}()
select {
case <-ch:
if err != nil {
processError(err, "Can't makeCall", &resp, insLog)
return
}
resp.Result = result
case <-time.After(time.Duration(ar.cfg.Timeout) * time.Second):
resp.Error = "Messagebus timeout exceeded"
return
}
resp.Result = result
}
}