aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/client/client.go
blob: 1f12ced9bfb51da1b0ab58bff181da2b9a0cb1de (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package client

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"math/rand"
	"strconv"
	"sync/atomic"
	"time"

	"github.com/gorilla/websocket"
	log "github.com/sirupsen/logrus"

	"github.com/karyontech/karyon-go/jsonrpc/message"
)

const (
	// JsonRPCVersion Defines the version of the JSON-RPC protocol being used.
	JsonRPCVersion = "2.0"

	// Default timeout for receiving requests from the server, in milliseconds.
	DefaultTimeout = 3000

	// The default buffer size for a subscription.
	DefaultSubscriptionBufferSize = 10000
)

var (
	ClientIsDisconnectedErr  = errors.New("Client is disconnected and closed")
	TimeoutError             = errors.New("Timeout Error")
	InvalidResponseIDErr     = errors.New("Invalid response ID")
	InvalidResponseResultErr = errors.New("Invalid response result")
	receivedStopSignalErr    = errors.New("Received stop signal")
)

// RPCClientConfig Holds the configuration settings for the RPC client.
type RPCClientConfig struct {
	Timeout                int    // Timeout for receiving requests from the server, in milliseconds.
	Addr                   string // Address of the RPC server.
	SubscriptionBufferSize int    // The buffer size for a subscription.
}

// RPCClient RPC Client
type RPCClient struct {
	config        RPCClientConfig
	conn          *websocket.Conn
	requests      *messageDispatcher
	subscriptions *subscriptions
	stopSignal    chan struct{}
	isClosed      atomic.Bool
}

// NewRPCClient Creates a new instance of RPCClient with the provided configuration.
// It establishes a WebSocket connection to the RPC server.
func NewRPCClient(config RPCClientConfig) (*RPCClient, error) {
	conn, _, err := websocket.DefaultDialer.Dial(config.Addr, nil)
	if err != nil {
		return nil, err
	}
	log.Infof("Successfully connected to the server: %s", config.Addr)

	if config.Timeout <= 0 {
		config.Timeout = DefaultTimeout
	}

	if config.SubscriptionBufferSize <= 0 {
		config.SubscriptionBufferSize = DefaultSubscriptionBufferSize
	}

	stopSignal := make(chan struct{})

	requests := newMessageDispatcher()
	subscriptions := newSubscriptions(config.SubscriptionBufferSize)

	client := &RPCClient{
		conn:          conn,
		config:        config,
		requests:      requests,
		subscriptions: subscriptions,
		stopSignal:    stopSignal,
	}

	go func() {
		if err := client.backgroundReceivingLoop(stopSignal); err != nil {
			client.Close()
		}
	}()

	return client, nil
}

// Close Closes the underlying websocket connection and stop the receiving loop.
func (client *RPCClient) Close() {
	// Check if it's already closed
	if !client.isClosed.CompareAndSwap(false, true) {
		return
	}

	log.Warn("Close the rpc client...")
	// Send stop signal to the background receiving loop
	close(client.stopSignal)

	// Close the underlying websocket connection
	err := client.conn.Close()
	if err != nil {
		log.WithError(err).Error("Close websocket connection")
	}

	client.requests.clear()
	client.subscriptions.clear()
}

// Call Sends an RPC call to the server with the specified method and
// parameters, and returns the response.
func (client *RPCClient) Call(method string, params any) (json.RawMessage, error) {
	log.Tracef("Call -> method: %s, params: %v", method, params)
	response, err := client.sendRequest(method, params)
	if err != nil {
		return nil, err
	}

	return response.Result, nil
}

// Subscribe Sends a subscription request to the server with the specified
// method and parameters, and it returns the subscription.
func (client *RPCClient) Subscribe(method string, params any) (*Subscription, error) {
	log.Tracef("Sbuscribe ->  method: %s, params: %v", method, params)
	response, err := client.sendRequest(method, params)
	if err != nil {
		return nil, err
	}

	if response.Result == nil {
		return nil, InvalidResponseResultErr
	}

	var subID message.SubscriptionID
	err = json.Unmarshal(response.Result, &subID)
	if err != nil {
		return nil, err
	}

	sub := client.subscriptions.subscribe(subID)

	return sub, nil
}

// Unsubscribe Sends an unsubscription request to the server to cancel the
// given subscription.
func (client *RPCClient) Unsubscribe(method string, subID message.SubscriptionID) error {
	log.Tracef("Unsubscribe -> method: %s, subID: %d", method, subID)
	_, err := client.sendRequest(method, subID)
	if err != nil {
		return err
	}

	// On success unsubscribe
	client.subscriptions.unsubscribe(subID)

	return nil
}

// backgroundReceivingLoop Starts reading new messages from the underlying connection.
func (client *RPCClient) backgroundReceivingLoop(stopSignal <-chan struct{}) error {
	log.Debug("Background loop started")

	newMsgCh := make(chan []byte)
	receiveErrCh := make(chan error)

	// Start listing for new messages
	go func() {
		for {
			_, msg, err := client.conn.ReadMessage()
			if err != nil {
				receiveErrCh <- err
				return
			}
			select {
			case <-client.stopSignal:
				return
			case newMsgCh <- msg:
			}
		}
	}()

	for {
		select {
		case err := <-receiveErrCh:
			log.WithError(err).Error("Read a new msg")
			return err
		case <-stopSignal:
			log.Debug("Background receiving loop stopped %w", receivedStopSignalErr)
			return nil
		case msg := <-newMsgCh:
			err := client.handleNewMsg(msg)
			if err != nil {
				log.WithError(err).Error("Handle a msg")
				return err
			}
		}
	}
}

// handleNewMsg Attempts to decode the received message into either a Response
// or Notification.
func (client *RPCClient) handleNewMsg(msg []byte) error {
	// Check if the received message is of type Response
	response := message.Response{}
	decoder := json.NewDecoder(bytes.NewReader(msg))
	decoder.DisallowUnknownFields()
	if err := decoder.Decode(&response); err == nil {
		if response.ID == nil {
			return InvalidResponseIDErr
		}

		err := client.requests.dispatch(*response.ID, response)
		if err != nil {
			return fmt.Errorf("Dispatch a response: %w", err)
		}

		return nil
	}

	// Check if the received message is of type Notification
	notification := message.Notification{}
	if err := json.Unmarshal(msg, &notification); err == nil {

		ntRes := message.NotificationResult{}
		if err := json.Unmarshal(notification.Params, &ntRes); err != nil {
			return fmt.Errorf("Failed to unmarshal notification params: %w", err)
		}

		err := client.subscriptions.notify(ntRes.Subscription, ntRes.Result)
		if err != nil {
			return fmt.Errorf("Notify a subscriber: %w", err)
		}

		log.Debugf("<-- %s", notification.String())

		return nil
	}

	return fmt.Errorf("Receive unexpected msg: %s", msg)
}

// sendRequest Sends a request and wait for the response
func (client *RPCClient) sendRequest(method string, params any) (message.Response, error) {
	response := message.Response{}

	if client.isClosed.Load() {
		return response, ClientIsDisconnectedErr
	}

	params_bytes, err := json.Marshal(params)
	if err != nil {
		return response, err
	}

	params_raw := json.RawMessage(params_bytes)

	// Generate a new id
	id := strconv.Itoa(rand.Int())
	req := message.Request{
		JSONRPC: JsonRPCVersion,
		ID:      id,
		Method:  method,
		Params:  &params_raw,
	}

	reqJSON, err := json.Marshal(req)
	if err != nil {
		return response, err
	}

	err = client.conn.WriteMessage(websocket.TextMessage, []byte(string(reqJSON)))
	if err != nil {
		return response, err
	}

	log.Debugf("--> %s", req.String())

	rx_ch := client.requests.register(id)
	defer client.requests.unregister(id)

	// Waits the response, it fails and return error if it exceed the timeout
	select {
	case response = <-rx_ch:
	case <-client.stopSignal:
		return response, ClientIsDisconnectedErr
	case <-time.After(time.Duration(client.config.Timeout) * time.Millisecond):
		return response, TimeoutError
	}

	err = validateResponse(&response, id)
	if err != nil {
		return response, err
	}

	log.Debugf("<-- %s", response.String())

	return response, nil
}

// validateResponse Checks the error field and whether the request id is the
// same as the response id
func validateResponse(res *message.Response, reqID message.RequestID) error {
	if res.Error != nil {
		return fmt.Errorf("Receive An Error: %s", res.Error.String())
	}

	if res.ID != nil {
		if *res.ID != reqID {
			return InvalidResponseIDErr
		}
	}

	return nil
}