/
dispatcher.go
112 lines (95 loc) · 3.24 KB
/
dispatcher.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package jsonrpc
import (
"context"
"fmt"
"sort"
"strings"
"go.uber.org/zap"
)
type InterceptorFunc func(ctx context.Context, request Request) *ErrorDetails
// Dispatcher forward the request it gets in input to the associated method.
// Despite being useful for simple use cases, it may not fit more advanced
// use cases.
type Dispatcher struct {
log *zap.Logger
// commands maps a method to a command.
commands map[string]Command
// interceptors holds the pre-checks to run before dispatching a request.
interceptors []InterceptorFunc
}
func (a *Dispatcher) DispatchRequest(ctx context.Context, request Request) *Response {
traceID := TraceIDFromContext(ctx)
if err := request.Check(); err != nil {
a.log.Info("invalid request",
zap.String("trace-id", traceID),
zap.Error(err))
return NewErrorResponse(request.ID, NewInvalidRequest(err))
}
for _, interceptor := range a.interceptors {
if errDetails := interceptor(ctx, request); errDetails != nil {
return NewErrorResponse(request.ID, errDetails)
}
}
command, ok := a.commands[request.Method]
if !ok {
a.log.Info("invalid method",
zap.String("trace-id", traceID),
zap.String("method", request.Method))
return NewErrorResponse(request.ID, NewMethodNotFound(request.Method))
}
result, errorDetails := command.Handle(ctx, request.Params)
if errorDetails != nil {
a.log.Info("method failed",
zap.String("trace-id", traceID),
zap.Any("error", errorDetails))
return NewErrorResponse(request.ID, errorDetails)
}
a.log.Info("method succeeded",
zap.String("trace-id", traceID))
return NewSuccessfulResponse(request.ID, result)
}
func (a *Dispatcher) RegisterMethod(method string, handler Command) {
if len(strings.Trim(method, " \t\r\n")) == 0 {
a.log.Panic("method cannot be empty")
}
if handler == nil {
a.log.Panic("handler cannot be nil")
}
if _, ok := a.commands[method]; ok {
a.log.Panic(fmt.Sprintf("method %q is already registered", method))
}
a.commands[method] = handler
a.log.Info("new JSON-RPC method registered", zap.String("method", method))
}
func (a *Dispatcher) RegisteredMethods() []string {
methods := make([]string, 0, len(a.commands))
for method := range a.commands {
methods = append(methods, method)
}
sort.Strings(methods)
return methods
}
func (a *Dispatcher) AddInterceptor(interceptor InterceptorFunc) {
a.interceptors = append(a.interceptors, interceptor)
}
func NewDispatcher(log *zap.Logger) *Dispatcher {
return &Dispatcher{
log: log,
commands: map[string]Command{},
interceptors: []InterceptorFunc{},
}
}