-
Notifications
You must be signed in to change notification settings - Fork 48
/
rpcbus.go
189 lines (151 loc) · 4.35 KB
/
rpcbus.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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package rpcbus
import (
"bytes"
"errors"
"fmt"
"sync"
"time"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
)
// ErrMethodNotExists is returned when calling an unregistered method.
type ErrMethodNotExists struct {
topic topics.Topic
}
// Error obeys the error interface.
func (e *ErrMethodNotExists) Error() string {
return fmt.Sprintf("method not registered: %s", e.topic)
}
var (
// ErrRequestTimeout is returned when request timeout-ed.
ErrRequestTimeout = errors.New("timeout-ed request")
// ErrMethodExists is returned when method is already registered.
ErrMethodExists = errors.New("method exists already")
// ErrInvalidRequestChan is returned method is bound to nil chan.
ErrInvalidRequestChan = errors.New("invalid request channel")
// DefaultTimeout is used when 0 timeout is set on calling a method.
DefaultTimeout = 5 * time.Second
)
// RPCBus is a request–response mechanism for internal communication between node
// components/subsystems. Under the hood this is long-polling method based on
// "chan chan" technique.
type RPCBus struct {
mu sync.RWMutex
registry map[topics.Topic]chan<- Request
}
// Request is the request object forwarded to the RPC endpoint.
type Request struct {
Params interface{}
RespChan chan Response
}
// Response is the response to the request forwarded to the RPC endpoint.
type Response struct {
Resp interface{}
Err error
}
// EmptyRequest returns a Request instance with no parameters.
func EmptyRequest() Request {
return NewRequest(bytes.Buffer{})
}
// NewRequest builds a new request with params. It creates the response channel
// under the hood.
func NewRequest(p interface{}) Request {
return Request{
Params: p,
RespChan: make(chan Response, 1),
}
}
// NewResponse builds a new response.
func NewResponse(p interface{}, err error) Response {
return Response{
Resp: p,
Err: err,
}
}
// New creates an RPCBus instance.
func New() *RPCBus {
return &RPCBus{
registry: make(map[topics.Topic]chan<- Request),
}
}
// Register registers a method and binds it to a handler channel. methodName
// must be unique per node instance. If not, returns err.
func (bus *RPCBus) Register(t topics.Topic, req chan<- Request) error {
bus.mu.Lock()
defer bus.mu.Unlock()
if req == nil {
return ErrInvalidRequestChan
}
if _, ok := bus.registry[t]; ok {
return ErrMethodExists
}
bus.registry[t] = req
return nil
}
// Deregister removes a handler channel from a method.
func (bus *RPCBus) Deregister(t topics.Topic) {
bus.mu.Lock()
defer bus.mu.Unlock()
delete(bus.registry, t)
}
// Call runs a long-polling technique to request from the method Consumer to
// run the corresponding procedure and return a result or timeout.
func (bus *RPCBus) Call(t topics.Topic, req Request, timeOut time.Duration) (interface{}, error) {
reqChan, err := bus.getReqChan(t)
if err != nil {
return bytes.Buffer{}, err
}
if timeOut <= 0 {
timeOut = DefaultTimeout
}
return bus.callTimeout(reqChan, req, timeOut)
}
func (bus *RPCBus) callTimeout(reqChan chan<- Request, req Request, timeOut time.Duration) (interface{}, error) {
timer := time.NewTimer(timeOut)
select {
case reqChan <- req:
case <-timer.C:
return bytes.Buffer{}, ErrRequestTimeout
}
if !timer.Stop() {
<-timer.C
}
timer.Reset(timeOut)
var resp Response
select {
case resp = <-req.RespChan:
case <-timer.C:
return bytes.Buffer{}, ErrRequestTimeout
}
if !timer.Stop() {
<-timer.C
}
return resp.Resp, resp.Err
}
//nolint
func (bus *RPCBus) callNoTimeout(reqChan chan<- Request, req Request) (interface{}, error) {
reqChan <- req
resp := <-req.RespChan
return resp.Resp, resp.Err
}
func (bus *RPCBus) getReqChan(t topics.Topic) (chan<- Request, error) {
bus.mu.RLock()
defer bus.mu.RUnlock()
if reqChan, ok := bus.registry[t]; ok {
return reqChan, nil
}
return nil, &ErrMethodNotExists{t}
}
// Close the RPCBus by resetting the registry.
func (bus *RPCBus) Close() {
bus.mu.Lock()
defer bus.mu.Unlock()
// Channels should be closed only by the goroutines/components
// that make them
// Reset registry
bus.registry = make(map[topics.Topic]chan<- Request)
}