gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/rpc/rpcclient/client.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"bytes"
    21  	"container/list"
    22  	"context"
    23  	"encoding/json"
    24  	"errors"
    25  	"fmt"
    26  	"net"
    27  	"net/url"
    28  	"os"
    29  	"reflect"
    30  	"strconv"
    31  	"strings"
    32  	"sync"
    33  	"sync/atomic"
    34  	"time"
    35  
    36  	"gitlab.com/aquachain/aquachain/common/log"
    37  	"gitlab.com/aquachain/aquachain/rpc"
    38  )
    39  
    40  var (
    41  	ErrClientQuit                = errors.New("client is closed")
    42  	ErrNoResult                  = errors.New("no result in JSON-RPC response")
    43  	ErrSubscriptionQueueOverflow = errors.New("subscription queue overflow")
    44  )
    45  
    46  var (
    47  	// ErrNotificationsUnsupported is returned when the connection doesn't support notifications
    48  	ErrNotificationsUnsupported = errors.New("notifications not supported")
    49  	// ErrNotificationNotFound is returned when the notification for the given id is not found
    50  	ErrSubscriptionNotFound = errors.New("subscription not found")
    51  )
    52  
    53  const (
    54  	// Timeouts
    55  	tcpKeepAliveInterval = 30 * time.Second
    56  	defaultDialTimeout   = 30 * time.Second // used when dialing if the context has no deadline
    57  	defaultWriteTimeout  = 30 * time.Second // used for calls if the context has no deadline
    58  )
    59  
    60  const (
    61  	// Subscriptions are removed when the subscriber cannot keep up.
    62  	//
    63  	// This can be worked around by supplying a channel with sufficiently sized buffer,
    64  	// but this can be inconvenient and hard to explain in the docs. Another issue with
    65  	// buffered channels is that the buffer is static even though it might not be needed
    66  	// most of the time.
    67  	//
    68  	// The approach taken here is to maintain a per-subscription linked list buffer
    69  	// shrinks on demand. If the buffer reaches the size below, the subscription is
    70  	// dropped.
    71  	maxClientSubscriptionBuffer = 8000
    72  )
    73  
    74  // BatchElem is an element in a batch request.
    75  type BatchElem struct {
    76  	Method string
    77  	Args   []interface{}
    78  	// The result is unmarshaled into this field. Result must be set to a
    79  	// non-nil pointer value of the desired type, otherwise the response will be
    80  	// discarded.
    81  	Result interface{}
    82  	// Error is set if the server returns an error for this request, or if
    83  	// unmarshaling into Result fails. It is not set for I/O errors.
    84  	Error error
    85  }
    86  
    87  // A value of this type can a JSON-RPC request, notification, successful response or
    88  // error response. Which one it is depends on the fields.
    89  type jsonrpcMessage struct {
    90  	Version string          `json:"jsonrpc"`
    91  	ID      json.RawMessage `json:"id,omitempty"`
    92  	Method  string          `json:"method,omitempty"`
    93  	Params  json.RawMessage `json:"params,omitempty"`
    94  	Error   *rpc.JsonError  `json:"error,omitempty"`
    95  	Result  json.RawMessage `json:"result,omitempty"`
    96  }
    97  
    98  func (msg *jsonrpcMessage) isNotification() bool {
    99  	return msg.ID == nil && msg.Method != ""
   100  }
   101  
   102  func (msg *jsonrpcMessage) isResponse() bool {
   103  	return msg.hasValidID() && msg.Method == "" && len(msg.Params) == 0
   104  }
   105  
   106  func (msg *jsonrpcMessage) hasValidID() bool {
   107  	return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '['
   108  }
   109  
   110  func (msg *jsonrpcMessage) String() string {
   111  	b, _ := json.Marshal(msg)
   112  	return string(b)
   113  }
   114  
   115  // Client represents a connection to an RPC server.
   116  type Client struct {
   117  	idCounter   uint32
   118  	connectFunc func(ctx context.Context) (net.Conn, error)
   119  	isHTTP      bool
   120  
   121  	// writeConn is only safe to access outside dispatch, with the
   122  	// write lock held. The write lock is taken by sending on
   123  	// requestOp and released by sending on sendDone.
   124  	writeConn net.Conn
   125  
   126  	// for dispatch
   127  	close       chan struct{}
   128  	didQuit     chan struct{}                  // closed when client quits
   129  	reconnected chan net.Conn                  // where write/reconnect sends the new connection
   130  	readErr     chan error                     // errors from read
   131  	readResp    chan []*jsonrpcMessage         // valid messages from read
   132  	requestOp   chan *requestOp                // for registering response IDs
   133  	sendDone    chan error                     // signals write completion, releases write lock
   134  	respWait    map[string]*requestOp          // active requests
   135  	subs        map[string]*ClientSubscription // active subscriptions
   136  }
   137  
   138  type requestOp struct {
   139  	ids  []json.RawMessage
   140  	err  error
   141  	resp chan *jsonrpcMessage // receives up to len(ids) responses
   142  	sub  *ClientSubscription  // only set for AquaSubscribe requests
   143  }
   144  
   145  func (op *requestOp) wait(ctx context.Context) (*jsonrpcMessage, error) {
   146  	select {
   147  	case <-ctx.Done():
   148  		return nil, ctx.Err()
   149  	case resp := <-op.resp:
   150  		return resp, op.err
   151  	}
   152  }
   153  
   154  // Dial creates a new client for the given URL.
   155  //
   156  // The currently supported URL schemes are "http", "https", "ws" and "wss". If rawurl is a
   157  // file name with no URL scheme, a local socket connection is established using UNIX
   158  // domain sockets on supported platforms and named pipes on Windows. If you want to
   159  // configure transport options, use DialHTTP, DialWebsocket or DialIPC instead.
   160  //
   161  // For websocket connections, the origin is set to the local host name.
   162  //
   163  // The client reconnects automatically if the connection is lost.
   164  func Dial(rawurl string) (*Client, error) {
   165  	return DialContext(context.Background(), rawurl)
   166  }
   167  
   168  // DialContext creates a new RPC client, just like Dial.
   169  //
   170  // The context is used to cancel or time out the initial connection establishment. It does
   171  // not affect subsequent interactions with the client.
   172  func DialContext(ctx context.Context, rawurl string) (*Client, error) {
   173  	u, err := url.Parse(rawurl)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	switch u.Scheme {
   178  	case "http", "https":
   179  		return DialHTTP(rawurl)
   180  	case "ws", "wss":
   181  		return DialWebsocket(ctx, rawurl, "")
   182  	case "":
   183  		return DialIPC(ctx, rawurl)
   184  	default:
   185  		return nil, fmt.Errorf("no known transport for URL scheme %q", u.Scheme)
   186  	}
   187  }
   188  
   189  func newClient(initctx context.Context, connectFunc func(context.Context) (net.Conn, error)) (*Client, error) {
   190  	conn, err := connectFunc(initctx)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  	_, isHTTP := conn.(*httpConn)
   195  
   196  	c := &Client{
   197  		writeConn:   conn,
   198  		isHTTP:      isHTTP,
   199  		connectFunc: connectFunc,
   200  		close:       make(chan struct{}),
   201  		didQuit:     make(chan struct{}),
   202  		reconnected: make(chan net.Conn),
   203  		readErr:     make(chan error),
   204  		readResp:    make(chan []*jsonrpcMessage),
   205  		requestOp:   make(chan *requestOp),
   206  		sendDone:    make(chan error, 1),
   207  		respWait:    make(map[string]*requestOp),
   208  		subs:        make(map[string]*ClientSubscription),
   209  	}
   210  	if !isHTTP {
   211  		go c.dispatch(conn)
   212  	}
   213  	return c, nil
   214  }
   215  
   216  func (c *Client) nextID() json.RawMessage {
   217  	id := atomic.AddUint32(&c.idCounter, 1)
   218  	return []byte(strconv.FormatUint(uint64(id), 10))
   219  }
   220  
   221  // SupportedModules calls the rpc_modules method, retrieving the list of
   222  // APIs that are available on the server.
   223  func (c *Client) SupportedModules() (map[string]string, error) {
   224  	var result map[string]string
   225  	ctx, cancel := context.WithTimeout(context.Background(), defaultDialTimeout)
   226  	defer cancel()
   227  	err := c.CallContext(ctx, &result, "rpc_modules")
   228  	return result, err
   229  }
   230  
   231  // Close closes the client, aborting any in-flight requests.
   232  func (c *Client) Close() {
   233  	if c.isHTTP {
   234  		return
   235  	}
   236  	select {
   237  	case c.close <- struct{}{}:
   238  		<-c.didQuit
   239  	case <-c.didQuit:
   240  	}
   241  }
   242  
   243  // Call performs a JSON-RPC call with the given arguments and unmarshals into
   244  // result if no error occurred.
   245  //
   246  // The result must be a pointer so that package json can unmarshal into it. You
   247  // can also pass nil, in which case the result is ignored.
   248  func (c *Client) Call(result interface{}, method string, args ...interface{}) error {
   249  	ctx := context.Background()
   250  	return c.CallContext(ctx, result, method, args...)
   251  }
   252  
   253  // CallContext performs a JSON-RPC call with the given arguments. If the context is
   254  // canceled before the call has successfully returned, CallContext returns immediately.
   255  //
   256  // The result must be a pointer so that package json can unmarshal into it. You
   257  // can also pass nil, in which case the result is ignored.
   258  func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
   259  	msg, err := c.newMessage(method, args...)
   260  	if err != nil {
   261  		return err
   262  	}
   263  	op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)}
   264  
   265  	if c.isHTTP {
   266  		err = c.sendHTTP(ctx, op, msg)
   267  	} else {
   268  		err = c.send(ctx, op, msg)
   269  	}
   270  	if err != nil {
   271  		return err
   272  	}
   273  
   274  	// dispatch has accepted the request and will close the channel it when it quits.
   275  	switch resp, err := op.wait(ctx); {
   276  	case err != nil:
   277  		return err
   278  	case resp.Error != nil:
   279  		return resp.Error
   280  	case len(resp.Result) == 0:
   281  		return ErrNoResult
   282  	default:
   283  		return json.Unmarshal(resp.Result, &result)
   284  	}
   285  }
   286  
   287  // BatchCall sends all given requests as a single batch and waits for the server
   288  // to return a response for all of them.
   289  //
   290  // In contrast to Call, BatchCall only returns I/O errors. Any error specific to
   291  // a request is reported through the Error field of the corresponding BatchElem.
   292  //
   293  // Note that batch calls may not be executed atomically on the server side.
   294  func (c *Client) BatchCall(b []BatchElem) error {
   295  	ctx := context.Background()
   296  	return c.BatchCallContext(ctx, b)
   297  }
   298  
   299  // BatchCall sends all given requests as a single batch and waits for the server
   300  // to return a response for all of them. The wait duration is bounded by the
   301  // context's deadline.
   302  //
   303  // In contrast to CallContext, BatchCallContext only returns errors that have occurred
   304  // while sending the request. Any error specific to a request is reported through the
   305  // Error field of the corresponding BatchElem.
   306  //
   307  // Note that batch calls may not be executed atomically on the server side.
   308  func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
   309  	msgs := make([]*jsonrpcMessage, len(b))
   310  	op := &requestOp{
   311  		ids:  make([]json.RawMessage, len(b)),
   312  		resp: make(chan *jsonrpcMessage, len(b)),
   313  	}
   314  	for i, elem := range b {
   315  		msg, err := c.newMessage(elem.Method, elem.Args...)
   316  		if err != nil {
   317  			return err
   318  		}
   319  		msgs[i] = msg
   320  		op.ids[i] = msg.ID
   321  	}
   322  
   323  	var err error
   324  	if c.isHTTP {
   325  		err = c.sendBatchHTTP(ctx, op, msgs)
   326  	} else {
   327  		err = c.send(ctx, op, msgs)
   328  	}
   329  
   330  	// Wait for all responses to come back.
   331  	for n := 0; n < len(b) && err == nil; n++ {
   332  		var resp *jsonrpcMessage
   333  		resp, err = op.wait(ctx)
   334  		if err != nil {
   335  			break
   336  		}
   337  		// Find the element corresponding to this response.
   338  		// The element is guaranteed to be present because dispatch
   339  		// only sends valid IDs to our channel.
   340  		var elem *BatchElem
   341  		for i := range msgs {
   342  			if bytes.Equal(msgs[i].ID, resp.ID) {
   343  				elem = &b[i]
   344  				break
   345  			}
   346  		}
   347  		if resp.Error != nil {
   348  			elem.Error = resp.Error
   349  			continue
   350  		}
   351  		if len(resp.Result) == 0 {
   352  			elem.Error = ErrNoResult
   353  			continue
   354  		}
   355  		elem.Error = json.Unmarshal(resp.Result, elem.Result)
   356  	}
   357  	return err
   358  }
   359  
   360  // AquaSubscribe registers a subscripion under the "aqua" namespace.
   361  func (c *Client) AquaSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
   362  	return c.Subscribe(ctx, "aqua", channel, args...)
   363  }
   364  
   365  // ShhSubscribe registers a subscripion under the "shh" namespace.
   366  func (c *Client) ShhSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
   367  	return c.Subscribe(ctx, "shh", channel, args...)
   368  }
   369  
   370  // Subscribe calls the "<namespace>_subscribe" method with the given arguments,
   371  // registering a subscription. Server notifications for the subscription are
   372  // sent to the given channel. The element type of the channel must match the
   373  // expected type of content returned by the subscription.
   374  //
   375  // The context argument cancels the RPC request that sets up the subscription but has no
   376  // effect on the subscription after Subscribe has returned.
   377  //
   378  // Slow subscribers will be dropped eventually. Client buffers up to 8000 notifications
   379  // before considering the subscriber dead. The subscription Err channel will receive
   380  // ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure
   381  // that the channel usually has at least one reader to prevent this issue.
   382  func (c *Client) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
   383  	// Check type of channel first.
   384  	chanVal := reflect.ValueOf(channel)
   385  	if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 {
   386  		panic("first argument to Subscribe must be a writable channel")
   387  	}
   388  	if chanVal.IsNil() {
   389  		panic("channel given to Subscribe must not be nil")
   390  	}
   391  	msg, err := c.newMessage(namespace+rpc.SubscribeMethodSuffix, args...)
   392  	if err != nil {
   393  		return nil, err
   394  	}
   395  	op := &requestOp{
   396  		ids:  []json.RawMessage{msg.ID},
   397  		resp: make(chan *jsonrpcMessage),
   398  		sub:  newClientSubscription(c, namespace, chanVal),
   399  	}
   400  
   401  	// Send the subscription request.
   402  	// The arrival and validity of the response is signaled on sub.quit.
   403  	if err := c.send(ctx, op, msg); err != nil {
   404  		return nil, err
   405  	}
   406  	if _, err := op.wait(ctx); err != nil {
   407  		return nil, err
   408  	}
   409  	return op.sub, nil
   410  }
   411  
   412  func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
   413  	params, err := json.Marshal(paramsIn)
   414  	if err != nil {
   415  		return nil, err
   416  	}
   417  	return &jsonrpcMessage{Version: "2.0", ID: c.nextID(), Method: method, Params: params}, nil
   418  }
   419  
   420  var isdebug = os.Getenv("DEBUG_RPC") != ""
   421  
   422  // send registers op with the dispatch loop, then sends msg on the connection.
   423  // if sending fails, op is deregistered.
   424  func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error {
   425  	select {
   426  	case c.requestOp <- op:
   427  		if isdebug {
   428  			fmt.Println(">>>sending", msg)
   429  		}
   430  
   431  		err := c.write(ctx, msg)
   432  		c.sendDone <- err
   433  		return err
   434  	case <-ctx.Done():
   435  		// This can happen if the client is overloaded or unable to keep up with
   436  		// subscription notifications.
   437  		return ctx.Err()
   438  	case <-c.didQuit:
   439  		return ErrClientQuit
   440  	}
   441  }
   442  
   443  func (c *Client) write(ctx context.Context, msg interface{}) error {
   444  	deadline, ok := ctx.Deadline()
   445  	if !ok {
   446  		deadline = time.Now().Add(defaultWriteTimeout)
   447  	}
   448  	// The previous write failed. Try to establish a new connection.
   449  	if c.writeConn == nil {
   450  		if err := c.reconnect(ctx); err != nil {
   451  			return err
   452  		}
   453  	}
   454  	c.writeConn.SetWriteDeadline(deadline)
   455  	err := json.NewEncoder(c.writeConn).Encode(msg)
   456  	c.writeConn.SetWriteDeadline(time.Time{})
   457  	if err != nil {
   458  		c.writeConn = nil
   459  	}
   460  	return err
   461  }
   462  
   463  func (c *Client) reconnect(ctx context.Context) error {
   464  	newconn, err := c.connectFunc(ctx)
   465  	if err != nil {
   466  		log.Trace(fmt.Sprintf("reconnect failed: %v", err))
   467  		return err
   468  	}
   469  	select {
   470  	case c.reconnected <- newconn:
   471  		c.writeConn = newconn
   472  		return nil
   473  	case <-c.didQuit:
   474  		newconn.Close()
   475  		return ErrClientQuit
   476  	}
   477  }
   478  
   479  // dispatch is the main loop of the client.
   480  // It sends read messages to waiting calls to Call and BatchCall
   481  // and subscription notifications to registered subscriptions.
   482  func (c *Client) dispatch(conn net.Conn) {
   483  	// Spawn the initial read loop.
   484  	go c.read(conn)
   485  
   486  	var (
   487  		lastOp        *requestOp    // tracks last send operation
   488  		requestOpLock = c.requestOp // nil while the send lock is held
   489  		reading       = true        // if true, a read loop is running
   490  	)
   491  	defer close(c.didQuit)
   492  	defer func() {
   493  		c.closeRequestOps(ErrClientQuit)
   494  		conn.Close()
   495  		if reading {
   496  			// Empty read channels until read is dead.
   497  			for {
   498  				select {
   499  				case <-c.readResp:
   500  				case <-c.readErr:
   501  					return
   502  				}
   503  			}
   504  		}
   505  	}()
   506  
   507  	for {
   508  		select {
   509  		case <-c.close:
   510  			return
   511  
   512  		// Read path.
   513  		case batch := <-c.readResp:
   514  			for _, msgv := range batch {
   515  				msg := msgv
   516  				switch {
   517  				case msg.isNotification():
   518  					log.Trace("", "msg", log.Lazy{Fn: func() string {
   519  						msg := msg
   520  						return fmt.Sprint("<-readResp: notification ", msg)
   521  					}})
   522  					c.handleNotification(msg)
   523  				case msg.isResponse():
   524  					log.Trace("", "msg", log.Lazy{Fn: func() string {
   525  						msg := msg
   526  						return fmt.Sprint("<-readResp: response ", msg)
   527  					}})
   528  					c.handleResponse(msg)
   529  				default:
   530  					log.Debug("", "msg", log.Lazy{Fn: func() string {
   531  						msg := msg
   532  						return fmt.Sprint("<-readResp: dropping weird message", msg)
   533  					}})
   534  					// TODO: maybe close
   535  				}
   536  			}
   537  
   538  		case err := <-c.readErr:
   539  			log.Debug(fmt.Sprintf("<-readErr: %v", err))
   540  			c.closeRequestOps(err)
   541  			conn.Close()
   542  			reading = false
   543  
   544  		case newconn := <-c.reconnected:
   545  			log.Debug(fmt.Sprintf("<-reconnected: (reading=%t) %v", reading, conn.RemoteAddr()))
   546  			if reading {
   547  				// Wait for the previous read loop to exit. This is a rare case.
   548  				conn.Close()
   549  				<-c.readErr
   550  			}
   551  			go c.read(newconn)
   552  			reading = true
   553  			conn = newconn
   554  
   555  		// Send path.
   556  		case op := <-requestOpLock:
   557  			// Stop listening for further send ops until the current one is done.
   558  			requestOpLock = nil
   559  			lastOp = op
   560  			for _, id := range op.ids {
   561  				c.respWait[string(id)] = op
   562  			}
   563  
   564  		case err := <-c.sendDone:
   565  			if err != nil {
   566  				// Remove response handlers for the last send. We remove those here
   567  				// because the error is already handled in Call or BatchCall. When the
   568  				// read loop goes down, it will signal all other current operations.
   569  				for _, id := range lastOp.ids {
   570  					delete(c.respWait, string(id))
   571  				}
   572  			}
   573  			// Listen for send ops again.
   574  			requestOpLock = c.requestOp
   575  			lastOp = nil
   576  		}
   577  	}
   578  }
   579  
   580  // closeRequestOps unblocks pending send ops and active subscriptions.
   581  func (c *Client) closeRequestOps(err error) {
   582  	didClose := make(map[*requestOp]bool)
   583  
   584  	for id, op := range c.respWait {
   585  		// Remove the op so that later calls will not close op.resp again.
   586  		delete(c.respWait, id)
   587  
   588  		if !didClose[op] {
   589  			op.err = err
   590  			close(op.resp)
   591  			didClose[op] = true
   592  		}
   593  	}
   594  	for id, sub := range c.subs {
   595  		delete(c.subs, id)
   596  		sub.quitWithError(err, false)
   597  	}
   598  }
   599  
   600  func (c *Client) handleNotification(msg *jsonrpcMessage) {
   601  	if !strings.HasSuffix(msg.Method, rpc.NotificationMethodSuffix) {
   602  		log.Debug(fmt.Sprint("dropping non-subscription message: ", msg))
   603  		return
   604  	}
   605  	var subResult struct {
   606  		ID     string          `json:"subscription"`
   607  		Result json.RawMessage `json:"result"`
   608  	}
   609  	if err := json.Unmarshal(msg.Params, &subResult); err != nil {
   610  		log.Debug(fmt.Sprint("dropping invalid subscription message: ", msg))
   611  		return
   612  	}
   613  	if c.subs[subResult.ID] != nil {
   614  		c.subs[subResult.ID].deliver(subResult.Result)
   615  	}
   616  }
   617  
   618  func (c *Client) handleResponse(msg *jsonrpcMessage) {
   619  	op := c.respWait[string(msg.ID)]
   620  	if op == nil {
   621  		log.Debug(fmt.Sprintf("unsolicited response %v", msg))
   622  		return
   623  	}
   624  	delete(c.respWait, string(msg.ID))
   625  	// For normal responses, just forward the reply to Call/BatchCall.
   626  	if op.sub == nil {
   627  		op.resp <- msg
   628  		return
   629  	}
   630  	// For subscription responses, start the subscription if the server
   631  	// indicates success. AquaSubscribe gets unblocked in either case through
   632  	// the op.resp channel.
   633  	defer close(op.resp)
   634  	if msg.Error != nil {
   635  		op.err = msg.Error
   636  		return
   637  	}
   638  	if op.err = json.Unmarshal(msg.Result, &op.sub.subid); op.err == nil {
   639  		go op.sub.start()
   640  		c.subs[op.sub.subid] = op.sub
   641  	}
   642  }
   643  
   644  // Reading happens on a dedicated goroutine.
   645  
   646  func (c *Client) read(conn net.Conn) error {
   647  	var (
   648  		buf json.RawMessage
   649  		dec = json.NewDecoder(conn)
   650  	)
   651  	readMessage := func() (rs []*jsonrpcMessage, err error) {
   652  		buf = buf[:0]
   653  		if err = dec.Decode(&buf); err != nil {
   654  			return nil, err
   655  		}
   656  		if isBatch(buf) {
   657  			err = json.Unmarshal(buf, &rs)
   658  		} else {
   659  			rs = make([]*jsonrpcMessage, 1)
   660  			err = json.Unmarshal(buf, &rs[0])
   661  		}
   662  		return rs, err
   663  	}
   664  
   665  	for {
   666  		resp, err := readMessage()
   667  		if err != nil {
   668  			c.readErr <- err
   669  			return err
   670  		}
   671  		c.readResp <- resp
   672  	}
   673  }
   674  
   675  // Subscriptions.
   676  
   677  // A ClientSubscription represents a subscription established through AquaSubscribe.
   678  type ClientSubscription struct {
   679  	client    *Client
   680  	etype     reflect.Type
   681  	channel   reflect.Value
   682  	namespace string
   683  	subid     string
   684  	in        chan json.RawMessage
   685  
   686  	quitOnce sync.Once     // ensures quit is closed once
   687  	quit     chan struct{} // quit is closed when the subscription exits
   688  	errOnce  sync.Once     // ensures err is closed once
   689  	err      chan error
   690  }
   691  
   692  func newClientSubscription(c *Client, namespace string, channel reflect.Value) *ClientSubscription {
   693  	sub := &ClientSubscription{
   694  		client:    c,
   695  		namespace: namespace,
   696  		etype:     channel.Type().Elem(),
   697  		channel:   channel,
   698  		quit:      make(chan struct{}),
   699  		err:       make(chan error, 1),
   700  		in:        make(chan json.RawMessage),
   701  	}
   702  	return sub
   703  }
   704  
   705  // Err returns the subscription error channel. The intended use of Err is to schedule
   706  // resubscription when the client connection is closed unexpectedly.
   707  //
   708  // The error channel receives a value when the subscription has ended due
   709  // to an error. The received error is nil if Close has been called
   710  // on the underlying client and no other error has occurred.
   711  //
   712  // The error channel is closed when Unsubscribe is called on the subscription.
   713  func (sub *ClientSubscription) Err() <-chan error {
   714  	return sub.err
   715  }
   716  
   717  // Unsubscribe unsubscribes the notification and closes the error channel.
   718  // It can safely be called more than once.
   719  func (sub *ClientSubscription) Unsubscribe() {
   720  	sub.quitWithError(nil, true)
   721  	sub.errOnce.Do(func() { close(sub.err) })
   722  }
   723  
   724  func (sub *ClientSubscription) quitWithError(err error, unsubscribeServer bool) {
   725  	sub.quitOnce.Do(func() {
   726  		// The dispatch loop won't be able to execute the unsubscribe call
   727  		// if it is blocked on deliver. Close sub.quit first because it
   728  		// unblocks deliver.
   729  		close(sub.quit)
   730  		if unsubscribeServer {
   731  			sub.requestUnsubscribe()
   732  		}
   733  		if err != nil {
   734  			if err == ErrClientQuit {
   735  				err = nil // Adhere to subscription semantics.
   736  			}
   737  			sub.err <- err
   738  		}
   739  	})
   740  }
   741  
   742  func (sub *ClientSubscription) deliver(result json.RawMessage) (ok bool) {
   743  	select {
   744  	case sub.in <- result:
   745  		return true
   746  	case <-sub.quit:
   747  		return false
   748  	}
   749  }
   750  
   751  func (sub *ClientSubscription) start() {
   752  	sub.quitWithError(sub.forward())
   753  }
   754  
   755  func (sub *ClientSubscription) forward() (err error, unsubscribeServer bool) {
   756  	cases := []reflect.SelectCase{
   757  		{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(sub.quit)},
   758  		{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(sub.in)},
   759  		{Dir: reflect.SelectSend, Chan: sub.channel},
   760  	}
   761  	buffer := list.New()
   762  	defer buffer.Init()
   763  	for {
   764  		var chosen int
   765  		var recv reflect.Value
   766  		if buffer.Len() == 0 {
   767  			// Idle, omit send case.
   768  			chosen, recv, _ = reflect.Select(cases[:2])
   769  		} else {
   770  			// Non-empty buffer, send the first queued item.
   771  			cases[2].Send = reflect.ValueOf(buffer.Front().Value)
   772  			chosen, recv, _ = reflect.Select(cases)
   773  		}
   774  
   775  		switch chosen {
   776  		case 0: // <-sub.quit
   777  			return nil, false
   778  		case 1: // <-sub.in
   779  			val, err := sub.unmarshal(recv.Interface().(json.RawMessage))
   780  			if err != nil {
   781  				return err, true
   782  			}
   783  			if buffer.Len() == maxClientSubscriptionBuffer {
   784  				return ErrSubscriptionQueueOverflow, true
   785  			}
   786  			buffer.PushBack(val)
   787  		case 2: // sub.channel<-
   788  			cases[2].Send = reflect.Value{} // Don't hold onto the value.
   789  			buffer.Remove(buffer.Front())
   790  		}
   791  	}
   792  }
   793  
   794  func (sub *ClientSubscription) unmarshal(result json.RawMessage) (interface{}, error) {
   795  	val := reflect.New(sub.etype)
   796  	err := json.Unmarshal(result, val.Interface())
   797  	return val.Elem().Interface(), err
   798  }
   799  
   800  func (sub *ClientSubscription) requestUnsubscribe() error {
   801  	var result interface{}
   802  	return sub.client.Call(&result, sub.namespace+rpc.UnsubscribeMethodSuffix, sub.subid)
   803  }