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
+10 -10
View File
@@ -14,7 +14,7 @@ import (
metrics "github.com/rcrowley/go-metrics"
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
"github.com/tendermint/tendermint/libs/service"
tmclient "github.com/tendermint/tendermint/rpc/client"
types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
@@ -41,6 +41,7 @@ func DefaultWSOptions() WSOptions {
//
// WSClient is safe for concurrent use by multiple goroutines.
type WSClient struct { // nolint: maligned
*tmclient.RunState
conn *websocket.Conn
Address string // IP:PORT or /path/to/socket
@@ -83,8 +84,6 @@ type WSClient struct { // nolint: maligned
// Send pings to server with this period. Must be less than readWait. If 0, no pings will be sent.
pingPeriod time.Duration
service.BaseService
// Time between sending a ping and receiving a pong. See
// https://godoc.org/github.com/rcrowley/go-metrics#Timer.
PingPongLatencyTimer metrics.Timer
@@ -114,6 +113,7 @@ func NewWSWithOptions(remoteAddr, endpoint string, opts WSOptions) (*WSClient, e
}
c := &WSClient{
RunState: tmclient.NewRunState("WSClient", nil),
Address: parsedURL.GetTrimmedHostWithPath(),
Dialer: dialFn,
Endpoint: endpoint,
@@ -127,7 +127,6 @@ func NewWSWithOptions(remoteAddr, endpoint string, opts WSOptions) (*WSClient, e
// sentIDs: make(map[types.JSONRPCIntID]bool),
}
c.BaseService = *service.NewBaseService(nil, "WSClient", c)
return c, nil
}
@@ -143,9 +142,11 @@ func (c *WSClient) String() string {
return fmt.Sprintf("WSClient{%s (%s)}", c.Address, c.Endpoint)
}
// OnStart implements service.Service by dialing a server and creating read and
// write routines.
func (c *WSClient) OnStart() error {
// Start dials the specified service address and starts the I/O routines.
func (c *WSClient) Start() error {
if err := c.RunState.Start(); err != nil {
return err
}
err := c.dial()
if err != nil {
return err
@@ -167,10 +168,9 @@ func (c *WSClient) OnStart() error {
return nil
}
// Stop overrides service.Service#Stop. There is no other way to wait until Quit
// channel is closed.
// Stop shuts down the client.
func (c *WSClient) Stop() error {
if err := c.BaseService.Stop(); err != nil {
if err := c.RunState.Stop(); err != nil {
return err
}
// only close user-facing channels when we can't write to them
+17 -12
View File
@@ -13,7 +13,7 @@ import (
"github.com/gorilla/websocket"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/coretypes"
types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
@@ -86,8 +86,8 @@ func (wm *WebsocketManager) WebsocketHandler(w http.ResponseWriter, r *http.Requ
}()
// register connection
con := newWSConnection(wsConn, wm.funcMap, wm.wsConnOptions...)
con.SetLogger(wm.logger.With("remote", wsConn.RemoteAddr()))
logger := wm.logger.With("remote", wsConn.RemoteAddr())
con := newWSConnection(wsConn, wm.funcMap, logger, wm.wsConnOptions...)
wm.logger.Info("New websocket connection", "remote", con.remoteAddr)
err = con.Start() // BLOCKING
if err != nil {
@@ -106,7 +106,7 @@ func (wm *WebsocketManager) WebsocketHandler(w http.ResponseWriter, r *http.Requ
//
// In case of an error, the connection is stopped.
type wsConnection struct {
service.BaseService
*client.RunState
remoteAddr string
baseConn *websocket.Conn
@@ -150,9 +150,11 @@ type wsConnection struct {
func newWSConnection(
baseConn *websocket.Conn,
funcMap map[string]*RPCFunc,
logger log.Logger,
options ...func(*wsConnection),
) *wsConnection {
wsc := &wsConnection{
RunState: client.NewRunState("wsConnection", logger),
remoteAddr: baseConn.RemoteAddr().String(),
baseConn: baseConn,
funcMap: funcMap,
@@ -166,7 +168,6 @@ func newWSConnection(
option(wsc)
}
wsc.baseConn.SetReadLimit(wsc.readLimit)
wsc.BaseService = *service.NewBaseService(nil, "wsConnection", wsc)
return wsc
}
@@ -218,9 +219,11 @@ func ReadLimit(readLimit int64) func(*wsConnection) {
}
}
// OnStart implements service.Service by starting the read and write routines. It
// blocks until there's some error.
func (wsc *wsConnection) OnStart() error {
// Start starts the client service routines and blocks until there is an error.
func (wsc *wsConnection) Start() error {
if err := wsc.RunState.Start(); err != nil {
return err
}
wsc.writeChan = make(chan types.RPCResponse, wsc.writeChanCapacity)
// Read subscriptions/unsubscriptions to events
@@ -231,16 +234,18 @@ func (wsc *wsConnection) OnStart() error {
return nil
}
// OnStop implements service.Service by unsubscribing remoteAddr from all
// subscriptions.
func (wsc *wsConnection) OnStop() {
// Stop unsubscribes the remote from all subscriptions.
func (wsc *wsConnection) Stop() error {
if err := wsc.RunState.Stop(); err != nil {
return err
}
if wsc.onDisconnect != nil {
wsc.onDisconnect(wsc.remoteAddr)
}
if wsc.ctx != nil {
wsc.cancel()
}
return nil
}
// GetRemoteAddr returns the remote address of the underlying connection.