-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcp.go
245 lines (228 loc) · 5.94 KB
/
rcp.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
)
const (
Version = "2.0" // JSON RPC Version
MaxBytesRead = 1 << 20 // 1mb
ExecutionTimeout = 15 * time.Second // execution timout
)
var (
defaultReq = Request{JsonRpc: Version}
DefaultOpts = Opts{
MaxBytesRead: MaxBytesRead,
ExecutionTimeout: ExecutionTimeout,
}
)
// NewServer creates a new JSON RPC Server that can handle requests.
func NewServer(opts Opts) *Service {
return NewService(opts)
}
// NewServer creates a new JSON RPC Server that can handle requests.
func NewDefaultServer() *Service {
return NewService(DefaultOpts)
}
type (
Opts struct {
// MaxBytesRead is the maximum bytes a request object can contain
MaxBytesRead int64
// ExecutionTimeout is the maximum time a method should execute for. If the
// execution exceeds the timeout, and ExectutionTimeout Error is returned for
// that request
ExecutionTimeout time.Duration
}
Service struct {
methodMap map[string]func(context.Context, *RequestParams) (any, error)
executionTimeout time.Duration
maxBytesRead int64
}
RequestFunc = func(context.Context, *RequestParams) (any, error)
RequestMap = map[string]RequestFunc
ServiceRegistrar interface {
Register() (string, RequestMap)
}
Request struct {
JsonRpc string `json:"jsonrpc"` // must always be 2.0
Id any `json:"id"` // should be a string, number or null.
Method string `json:"method"` // the method being called
Params any `json:"params"` // the params for the method being called
}
Response struct {
JsonRpc string `json:"jsonrpc,omitempty"` // must always be 2.0
Id any `json:"id"` // the id passed in the request object
Result any `json:"result,omitempty"` // required when the request is successful
Error *Error `json:"error,omitempty"` // required when the request is a failure
}
RequestParams struct {
Payload []byte
}
methodResp struct {
err error
resp any
}
)
func errorResponse(req *Request, err error) Response {
res := Response{
JsonRpc: req.JsonRpc,
Id: req.Id,
Error: &Error{},
}
switch err.(type) {
case Error:
e := err.(Error)
res.Error.Code = e.Code
res.Error.Message = e.Message
if res.Error.Code == InvalidRpcVersion.Code {
res.JsonRpc = ""
}
return res
default:
res.Error.Code = InternalError.Code
res.Error.Message = err.Error()
return res
}
}
func successResponse(req Request, body any) Response {
return Response{
JsonRpc: req.JsonRpc,
Id: req.Id,
Result: body,
}
}
func writeResponse(w http.ResponseWriter, response any) {
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
_ = enc.Encode(response)
}
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if r.Body == nil {
writeResponse(w, errorResponse(&defaultReq, RequestBodyIsEmpty))
return
}
r.Body = http.MaxBytesReader(w, r.Body, s.maxBytesRead)
buf := bytes.NewBuffer([]byte{})
n, err := io.Copy(buf, r.Body)
if err != nil {
if n > MaxBytesRead {
writeResponse(w, errorResponse(&defaultReq, RequestBodyTooLargeError))
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var reqPayload any
err = json.NewDecoder(buf).Decode(&reqPayload)
if err != nil {
writeResponse(w, errorResponse(&defaultReq, ParseError))
return
}
switch reqPayload.(type) {
case []any:
payloads := reqPayload.([]any)
resp := make([]Response, len(payloads))
wg := sync.WaitGroup{}
wg.Add(len(payloads))
for i, payload := range payloads {
go func(index int, p any) {
defer wg.Done()
resp[index] = s.handle(parseRequest(p.(map[string]any)))
}(i, payload)
}
wg.Wait()
writeResponse(w, resp)
case map[string]any:
payload := reqPayload.(map[string]any)
writeResponse(w, s.handle(parseRequest(payload)))
default:
writeResponse(w, errorResponse(&defaultReq, InvalidRequest))
}
}
func parseRequest(payload map[string]any) Request {
req := defaultReq
version, ok := payload["jsonrpc"]
if ok {
if v, ok := version.(string); ok {
req.JsonRpc = v
} else {
req.JsonRpc = ""
}
}
req.Id = payload["id"]
req.Method = payload["method"].(string)
req.Params = payload["params"]
return req
}
func (s *Service) handle(req Request) Response {
if req.JsonRpc != Version {
return errorResponse(&req, InvalidRpcVersion)
}
if strings.TrimSpace(req.Method) == "" {
return errorResponse(&req, InvalidMethodParam)
}
res, err := s.handleMethod(req)
if err != nil {
return errorResponse(&req, err)
}
return successResponse(req, res)
}
func (s *Service) handleMethod(req Request) (any, error) {
fn, ok := s.methodMap[req.Method]
if !ok {
return nil, MethodNotFound
}
payload, err := json.Marshal(req.Params)
if err != nil {
return nil, fmt.Errorf("%w: %s", InvalidRequest, err.Error())
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
result := make(chan methodResp)
go func() {
params := &RequestParams{Payload: payload}
res := methodResp{}
res.resp, res.err = fn(ctx, params)
result <- res
}()
delay := time.NewTimer(s.executionTimeout)
select {
case <-delay.C:
return nil, ExecutionTimeoutError
case r := <-result:
if !delay.Stop() {
<-delay.C
}
return r.resp, r.err
}
}
func (s Service) AddService(services ...ServiceRegistrar) {
for _, srv := range services {
name, requestMap := srv.Register()
nameFmt := "%s"
if name != "" {
nameFmt = "%s.%s"
}
for methodName, fn := range requestMap {
s.methodMap[fmt.Sprintf(nameFmt, name, methodName)] = fn
}
}
}
func NewService(opts Opts) *Service {
return &Service{
methodMap: map[string]func(context.Context, *RequestParams) (any, error){},
executionTimeout: opts.ExecutionTimeout,
maxBytesRead: opts.MaxBytesRead,
}
}