rpc: Strip down the base RPC client interface. (#6971)

* rpc: Strip down the base RPC client interface.

Prior to this change, the RPC client interface requires implementing the entire
Service interface, but most of the methods of Service are not needed by the
concrete clients. Dissociate the Client interface from the Service interface.

- Extract only those methods of Service that are necessary to make the existing
  clients work.

- Update the clients to combine Start/Onstart and Stop/OnStop. This does not
  change what the clients do to start or stop. Only the websocket clients make
  use of this functionality anyway.

  The websocket implementation uses some plumbing from the BaseService helper.
  We should be able to excising that entirely, but the current interface
  dependencies among the clients would require a much larger change, and one
  that leaks into other (non-RPC) packages.

  As a less-invasive intermediate step, preserve the existing client behaviour
  (and tests) by extracting the necessary subset of the BaseService
  functionality to an analogous RunState helper for clients. I plan to obsolete
  that type in a future PR, but for now this makes a useful waypoint.

Related:
- Clean up client implementations.
- Update mocks.
This commit is contained in:
M. J. Fromberger
2021-09-22 14:26:35 -07:00
committed by GitHub
parent d04b6c2a5e
commit 1995ef2572
8 changed files with 138 additions and 149 deletions
-6
View File
@@ -6,7 +6,6 @@ import (
"time"
"github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/coretypes"
jsonrpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
@@ -158,11 +157,6 @@ func NewWithClientAndWSOptions(remote string, c *http.Client, wso WSOptions) (*H
var _ rpcclient.Client = (*HTTP)(nil)
// SetLogger sets a logger.
func (c *HTTP) SetLogger(l log.Logger) {
c.wsEvents.SetLogger(l)
}
// Remote returns the remote network address in a string form.
func (c *HTTP) Remote() string {
return c.remote
+12 -18
View File
@@ -10,14 +10,11 @@ import (
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
tmjson "github.com/tendermint/tendermint/libs/json"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
"github.com/tendermint/tendermint/libs/service"
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/coretypes"
jsonrpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
)
var errNotRunning = errors.New("client is not running. Use .Start() method to start")
// WSOptions for the WS part of the HTTP client.
type WSOptions struct {
Path string // path (e.g. "/ws")
@@ -48,7 +45,7 @@ func (wso WSOptions) Validate() error {
// wsEvents is a wrapper around WSClient, which implements EventsClient.
type wsEvents struct {
service.BaseService
*rpcclient.RunState
ws *jsonrpcclient.WSClient
mtx tmsync.RWMutex
@@ -78,7 +75,7 @@ func newWsEvents(remote string, wso WSOptions) (*wsEvents, error) {
w := &wsEvents{
subscriptions: make(map[string]*wsSubscription),
}
w.BaseService = *service.NewBaseService(nil, "wsEvents", w)
w.RunState = rpcclient.NewRunState("wsEvents", nil)
var err error
w.ws, err = jsonrpcclient.NewWSWithOptions(remote, wso.Path, wso.WSOptions)
@@ -94,23 +91,20 @@ func newWsEvents(remote string, wso WSOptions) (*wsEvents, error) {
return w, nil
}
// OnStart implements service.Service by starting WSClient and event loop.
func (w *wsEvents) OnStart() error {
// Start starts the websocket client and the event loop.
func (w *wsEvents) Start() error {
if err := w.ws.Start(); err != nil {
return err
}
go w.eventListener()
return nil
}
// OnStop implements service.Service by stopping WSClient.
func (w *wsEvents) OnStop() {
if err := w.ws.Stop(); err != nil {
w.Logger.Error("Can't stop ws client", "err", err)
}
}
// IsRunning reports whether the websocket client is running.
func (w *wsEvents) IsRunning() bool { return w.ws.IsRunning() }
// Stop shuts down the websocket client.
func (w *wsEvents) Stop() error { return w.ws.Stop() }
// Subscribe implements EventsClient by using WSClient to subscribe given
// subscriber to query. By default, it returns a channel with cap=1. Error is
@@ -128,7 +122,7 @@ func (w *wsEvents) Subscribe(ctx context.Context, subscriber, query string,
outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) {
if !w.IsRunning() {
return nil, errNotRunning
return nil, rpcclient.ErrClientNotRunning
}
if err := w.ws.Subscribe(ctx, query); err != nil {
@@ -156,7 +150,7 @@ func (w *wsEvents) Subscribe(ctx context.Context, subscriber, query string,
// It returns an error if wsEvents is not running.
func (w *wsEvents) Unsubscribe(ctx context.Context, subscriber, query string) error {
if !w.IsRunning() {
return errNotRunning
return rpcclient.ErrClientNotRunning
}
if err := w.ws.Unsubscribe(ctx, query); err != nil {
@@ -182,7 +176,7 @@ func (w *wsEvents) Unsubscribe(ctx context.Context, subscriber, query string) er
// It returns an error if wsEvents is not running.
func (w *wsEvents) UnsubscribeAll(ctx context.Context, subscriber string) error {
if !w.IsRunning() {
return errNotRunning
return rpcclient.ErrClientNotRunning
}
if err := w.ws.UnsubscribeAll(ctx); err != nil {