@@ -58,12 +58,6 @@ const (
58
58
maxClientSubscriptionBuffer = 20000
59
59
)
60
60
61
- const (
62
- httpScheme = "http"
63
- wsScheme = "ws"
64
- ipcScheme = "ipc"
65
- )
66
-
67
61
// BatchElem is an element in a batch request.
68
62
type BatchElem struct {
69
63
Method string
@@ -80,7 +74,7 @@ type BatchElem struct {
80
74
// Client represents a connection to an RPC server.
81
75
type Client struct {
82
76
idgen func () ID // for subscriptions
83
- scheme string // connection type: http, ws or ipc
77
+ isHTTP bool // connection type: http, ws or ipc
84
78
services * serviceRegistry
85
79
86
80
idCounter uint32
@@ -115,11 +109,9 @@ type clientConn struct {
115
109
}
116
110
117
111
func (c * Client ) newClientConn (conn ServerCodec ) * clientConn {
118
- ctx := context .WithValue (context .Background (), clientContextKey {}, c )
119
- // Http connections have already set the scheme
120
- if ! c .isHTTP () && c .scheme != "" {
121
- ctx = context .WithValue (ctx , "scheme" , c .scheme )
122
- }
112
+ ctx := context .Background ()
113
+ ctx = context .WithValue (ctx , clientContextKey {}, c )
114
+ ctx = context .WithValue (ctx , peerInfoContextKey {}, conn .peerInfo ())
123
115
handler := newHandler (ctx , conn , c .idgen , c .services )
124
116
return & clientConn {conn , handler }
125
117
}
@@ -145,7 +137,7 @@ func (op *requestOp) wait(ctx context.Context, c *Client) (*jsonrpcMessage, erro
145
137
select {
146
138
case <- ctx .Done ():
147
139
// Send the timeout to dispatch so it can remove the request IDs.
148
- if ! c .isHTTP () {
140
+ if ! c .isHTTP {
149
141
select {
150
142
case c .reqTimeout <- op :
151
143
case <- c .closing :
@@ -212,18 +204,10 @@ func newClient(initctx context.Context, connect reconnectFunc) (*Client, error)
212
204
}
213
205
214
206
func initClient (conn ServerCodec , idgen func () ID , services * serviceRegistry ) * Client {
215
- scheme := ""
216
- switch conn .(type ) {
217
- case * httpConn :
218
- scheme = httpScheme
219
- case * websocketCodec :
220
- scheme = wsScheme
221
- case * jsonCodec :
222
- scheme = ipcScheme
223
- }
207
+ _ , isHTTP := conn .(* httpConn )
224
208
c := & Client {
209
+ isHTTP : isHTTP ,
225
210
idgen : idgen ,
226
- scheme : scheme ,
227
211
services : services ,
228
212
writeConn : conn ,
229
213
close : make (chan struct {}),
@@ -236,7 +220,7 @@ func initClient(conn ServerCodec, idgen func() ID, services *serviceRegistry) *C
236
220
reqSent : make (chan error , 1 ),
237
221
reqTimeout : make (chan * requestOp ),
238
222
}
239
- if ! c . isHTTP () {
223
+ if ! isHTTP {
240
224
go c .dispatch (conn )
241
225
}
242
226
return c
@@ -267,7 +251,7 @@ func (c *Client) SupportedModules() (map[string]string, error) {
267
251
268
252
// Close closes the client, aborting any in-flight requests.
269
253
func (c * Client ) Close () {
270
- if c .isHTTP () {
254
+ if c .isHTTP {
271
255
return
272
256
}
273
257
select {
@@ -281,7 +265,7 @@ func (c *Client) Close() {
281
265
// This method only works for clients using HTTP, it doesn't have
282
266
// any effect for clients using another transport.
283
267
func (c * Client ) SetHeader (key , value string ) {
284
- if ! c .isHTTP () {
268
+ if ! c .isHTTP {
285
269
return
286
270
}
287
271
conn := c .writeConn .(* httpConn )
@@ -315,7 +299,7 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
315
299
}
316
300
op := & requestOp {ids : []json.RawMessage {msg .ID }, resp : make (chan * jsonrpcMessage , 1 )}
317
301
318
- if c .isHTTP () {
302
+ if c .isHTTP {
319
303
err = c .sendHTTP (ctx , op , msg )
320
304
} else {
321
305
err = c .send (ctx , op , msg )
@@ -378,7 +362,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
378
362
}
379
363
380
364
var err error
381
- if c .isHTTP () {
365
+ if c .isHTTP {
382
366
err = c .sendBatchHTTP (ctx , op , msgs )
383
367
} else {
384
368
err = c .send (ctx , op , msgs )
@@ -417,7 +401,7 @@ func (c *Client) Notify(ctx context.Context, method string, args ...interface{})
417
401
}
418
402
msg .ID = nil
419
403
420
- if c .isHTTP () {
404
+ if c .isHTTP {
421
405
return c .sendHTTP (ctx , op , msg )
422
406
}
423
407
return c .send (ctx , op , msg )
@@ -450,12 +434,12 @@ func (c *Client) Subscribe(ctx context.Context, namespace string, channel interf
450
434
// Check type of channel first.
451
435
chanVal := reflect .ValueOf (channel )
452
436
if chanVal .Kind () != reflect .Chan || chanVal .Type ().ChanDir ()& reflect .SendDir == 0 {
453
- panic ("first argument to Subscribe must be a writable channel" )
437
+ panic (fmt . Sprintf ( "channel argument of Subscribe has type %T, need writable channel", channel ) )
454
438
}
455
439
if chanVal .IsNil () {
456
440
panic ("channel given to Subscribe must not be nil" )
457
441
}
458
- if c .isHTTP () {
442
+ if c .isHTTP {
459
443
return nil , ErrNotificationsUnsupported
460
444
}
461
445
@@ -509,8 +493,8 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
509
493
}
510
494
511
495
func (c * Client ) write (ctx context.Context , msg interface {}, retry bool ) error {
512
- // The previous write failed. Try to establish a new connection.
513
496
if c .writeConn == nil {
497
+ // The previous write failed. Try to establish a new connection.
514
498
if err := c .reconnect (ctx ); err != nil {
515
499
return err
516
500
}
@@ -657,7 +641,3 @@ func (c *Client) read(codec ServerCodec) {
657
641
c .readOp <- readOp {msgs , batch }
658
642
}
659
643
}
660
-
661
- func (c * Client ) isHTTP () bool {
662
- return c .scheme == httpScheme
663
- }
0 commit comments