jsonrpc2/rpc/server.go

128 lines
2.9 KiB
Go
Raw Normal View History

2022-01-31 20:17:31 +03:00
package rpc
2022-01-31 02:31:43 +03:00
import (
"context"
"encoding/json"
"io"
"sync"
)
const version = "2.0"
2022-01-31 20:17:31 +03:00
type RpcServer struct {
2022-01-31 02:31:43 +03:00
Logger Logger
IgnoreNotifications bool
handlers map[string]Handler
mu sync.RWMutex
}
2022-01-31 20:17:31 +03:00
func New() *RpcServer {
return &RpcServer{
2022-01-31 02:31:43 +03:00
Logger: nopLogger{},
IgnoreNotifications: true,
handlers: map[string]Handler{},
mu: sync.RWMutex{},
}
}
2022-01-31 20:17:31 +03:00
func (r *RpcServer) Register(method string, handler Handler) {
2022-01-31 02:31:43 +03:00
r.mu.Lock()
defer r.mu.Unlock()
r.handlers[method] = handler
}
2022-01-31 20:17:31 +03:00
func (r *RpcServer) SingleRequest(ctx context.Context, reader io.Reader, writer io.Writer) {
2022-01-31 02:31:43 +03:00
req := new(rpcRequest)
2022-01-31 20:17:31 +03:00
if err := json.NewDecoder(reader).Decode(req); err != nil {
2022-01-31 02:31:43 +03:00
r.Logger.Logf("Can't read body: %v", err)
2022-01-31 20:17:31 +03:00
WriteError(ErrCodeParseError, writer)
2022-01-31 02:31:43 +03:00
return
}
2022-01-31 20:17:31 +03:00
resp := r.callMethod(ctx, req)
2022-01-31 02:31:43 +03:00
if req.Id == nil && r.IgnoreNotifications {
// notification request
return
}
if err := json.NewEncoder(writer).Encode(resp); err != nil {
r.Logger.Logf("Can't write response: %v", err)
2022-01-31 20:17:31 +03:00
WriteError(ErrCodeInternalError, writer)
2022-01-31 02:31:43 +03:00
return
}
}
2022-01-31 20:17:31 +03:00
func (r *RpcServer) BatchRequest(ctx context.Context, reader io.Reader, writer io.Writer) {
2022-01-31 02:31:43 +03:00
var req []rpcRequest
2022-01-31 20:17:31 +03:00
if err := json.NewDecoder(reader).Decode(&req); err != nil {
2022-01-31 02:31:43 +03:00
r.Logger.Logf("Can't read body: %v", err)
2022-01-31 20:17:31 +03:00
WriteError(ErrCodeParseError, writer)
2022-01-31 02:31:43 +03:00
return
}
var responses []*rpcResponse
wg := sync.WaitGroup{}
wg.Add(len(req))
for _, j := range req {
go func(req rpcRequest) {
defer wg.Done()
2022-01-31 20:17:31 +03:00
resp := r.callMethod(ctx, &req)
2022-01-31 02:31:43 +03:00
if req.Id == nil && r.IgnoreNotifications {
// notification request
return
}
responses = append(responses, resp)
}(j)
}
wg.Wait()
if err := json.NewEncoder(writer).Encode(responses); err != nil {
r.Logger.Logf("Can't write response: %v", err)
2022-01-31 20:17:31 +03:00
WriteError(ErrCodeInternalError, writer)
2022-01-31 02:31:43 +03:00
}
}
2022-01-31 20:17:31 +03:00
func (r *RpcServer) callMethod(ctx context.Context, req *rpcRequest) *rpcResponse {
2022-01-31 02:31:43 +03:00
r.mu.RLock()
h, ok := r.handlers[req.Method]
r.mu.RUnlock()
if !ok {
return &rpcResponse{
Jsonrpc: version,
Error: NewError(ErrCodeMethodNotFound),
Id: req.Id,
}
}
resp, err := h(ctx, req.Params)
if err != nil {
r.Logger.Logf("User error %v", err)
return &rpcResponse{
Jsonrpc: version,
Error: err,
Id: req.Id,
}
}
return &rpcResponse{
Jsonrpc: version,
Result: resp,
Id: req.Id,
}
}
2022-01-31 20:17:31 +03:00
func WriteError(code int, w io.Writer) {
2022-01-31 02:31:43 +03:00
_ = json.NewEncoder(w).Encode(rpcResponse{
Jsonrpc: version,
Error: NewError(code),
})
}
type rpcRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
Id any `json:"id"`
}
type rpcResponse struct {
Jsonrpc string `json:"jsonrpc"`
Result json.RawMessage `json:"result,omitempty"`
Error error `json:"error,omitempty"`
Id any `json:"id,omitempty"`
}