tendermint/rpc/lib/client/ws_client.go

404 lines
10 KiB
Go
Raw Normal View History

2016-01-12 13:50:06 -08:00
package rpcclient
import (
"context"
2016-01-13 15:37:35 -08:00
"encoding/json"
"fmt"
"math"
"net"
2016-01-12 13:50:06 -08:00
"net/http"
"sync"
2016-01-12 13:50:06 -08:00
"time"
"github.com/gorilla/websocket"
2017-03-09 07:00:05 -08:00
"github.com/pkg/errors"
2017-08-03 11:19:39 -07:00
metrics "github.com/rcrowley/go-metrics"
types "github.com/tendermint/tendermint/rpc/lib/types"
cmn "github.com/tendermint/tmlibs/common"
2016-01-12 13:50:06 -08:00
)
const (
// Time allowed to write a message to the server.
writeWait = 10 * time.Second
// Maximum reconnect attempts
maxReconnectAttempts = 25
defaultPongWait = 30 * time.Second
defaultPingPeriod = (defaultPongWait * 9) / 10
2016-01-12 13:50:06 -08:00
)
2017-08-07 14:56:38 -07:00
// WSClient is a WebSocket client. The methods of WSClient are safe for use by
// multiple goroutines.
2016-01-12 13:50:06 -08:00
type WSClient struct {
cmn.BaseService
conn *websocket.Conn
2016-02-18 18:05:24 -08:00
Address string // IP:PORT or /path/to/socket
Endpoint string // /websocket/url/endpoint
Dialer func(string, string) (net.Conn, error)
// Time between sending a ping and receiving a pong. See
// https://godoc.org/github.com/rcrowley/go-metrics#Timer.
2017-08-03 11:19:39 -07:00
PingPongLatencyTimer metrics.Timer
// user facing channels, closed only when the client is being stopped.
ResultsCh chan json.RawMessage
ErrorsCh chan error
// internal channels
2017-08-05 09:07:02 -07:00
send chan types.RPCRequest // user requests
backlog chan types.RPCRequest // stores a single user request received during a conn failure
reconnectAfter chan error // reconnect requests
readRoutineQuit chan struct{} // a way for readRoutine to close writeRoutine
2017-08-05 09:07:02 -07:00
wg sync.WaitGroup
mtx sync.RWMutex
sentLastPingAt time.Time
2017-08-07 14:56:38 -07:00
reconnecting bool
// Time allowed to read the next pong message from the server.
pongWait time.Duration
// Send pings to server with this period. Must be less than pongWait.
pingPeriod time.Duration
2016-01-12 13:50:06 -08:00
}
2017-08-07 14:56:38 -07:00
// NewWSClient returns a new client. See the commentary on the func(*WSClient)
// functions for a detailed description of how to configure ping period and
// pong wait time.
func NewWSClient(remoteAddr, endpoint string, options ...func(*WSClient)) *WSClient {
addr, dialer := makeHTTPDialer(remoteAddr)
c := &WSClient{
2017-08-03 11:19:39 -07:00
Address: addr,
Dialer: dialer,
Endpoint: endpoint,
PingPongLatencyTimer: metrics.NewTimer(),
pongWait: defaultPongWait,
pingPeriod: defaultPingPeriod,
}
c.BaseService = *cmn.NewBaseService(nil, "WSClient", c)
for _, option := range options {
option(c)
}
return c
}
// PingPong allows changing ping period and pong wait time. If ping period
// greater or equal to pong wait time, panic will be thrown.
func PingPong(pingPeriod, pongWait time.Duration) func(*WSClient) {
return func(c *WSClient) {
if pingPeriod >= pongWait {
panic(fmt.Sprintf("ping period (%v) must be less than pong wait time (%v)", pingPeriod, pongWait))
}
c.pingPeriod = pingPeriod
c.pongWait = pongWait
2016-01-12 13:50:06 -08:00
}
}
// String returns WS client full address.
func (c *WSClient) String() string {
return fmt.Sprintf("%s (%s)", c.Address, c.Endpoint)
2016-02-02 23:01:28 -08:00
}
// OnStart implements cmn.Service by dialing a server and creating read and
// write routines.
func (c *WSClient) OnStart() error {
err := c.dial()
2016-01-12 13:50:06 -08:00
if err != nil {
return err
}
c.ResultsCh = make(chan json.RawMessage)
c.ErrorsCh = make(chan error)
c.send = make(chan types.RPCRequest)
// 1 additional error may come from the read/write
// goroutine depending on which failed first.
c.reconnectAfter = make(chan error, 1)
// capacity for 1 request. a user won't be able to send more because the send
// channel is unbuffered.
c.backlog = make(chan types.RPCRequest, 1)
c.startReadWriteRoutines()
go c.reconnectRoutine()
2016-01-12 13:50:06 -08:00
return nil
}
// OnStop implements cmn.Service.
func (c *WSClient) OnStop() {}
// Stop overrides cmn.Service#Stop. There is no other way to wait until Quit
// channel is closed.
func (c *WSClient) Stop() bool {
success := c.BaseService.Stop()
// only close user-facing channels when we can't write to them
c.wg.Wait()
close(c.ResultsCh)
close(c.ErrorsCh)
return success
2016-01-12 13:50:06 -08:00
}
// IsReconnecting returns true if the client is reconnecting right now.
func (c *WSClient) IsReconnecting() bool {
2017-08-07 14:56:38 -07:00
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.reconnecting
}
// IsActive returns true if the client is running and not reconnecting.
func (c *WSClient) IsActive() bool {
return c.IsRunning() && !c.IsReconnecting()
}
2017-08-05 09:07:02 -07:00
// Send the given RPC request to the server. Results will be available on
// ResultsCh, errors, if any, on ErrorsCh. Will block until send succeeds or
// ctx.Done is closed.
func (c *WSClient) Send(ctx context.Context, request types.RPCRequest) error {
select {
case c.send <- request:
c.Logger.Info("sent a request", "req", request)
return nil
case <-ctx.Done():
return ctx.Err()
}
}
2016-02-18 18:05:24 -08:00
2017-08-05 09:07:02 -07:00
// Call the given method. See Send description.
func (c *WSClient) Call(ctx context.Context, method string, params map[string]interface{}) error {
request, err := types.MapToRequest("", method, params)
if err != nil {
return err
}
return c.Send(ctx, request)
}
2017-08-05 09:07:02 -07:00
// CallWithArrayParams the given method with params in a form of array. See
// Send description.
func (c *WSClient) CallWithArrayParams(ctx context.Context, method string, params []interface{}) error {
request, err := types.ArrayToRequest("", method, params)
if err != nil {
return err
}
return c.Send(ctx, request)
}
///////////////////////////////////////////////////////////////////////////////
// Private methods
func (c *WSClient) dial() error {
2016-02-18 18:05:24 -08:00
dialer := &websocket.Dialer{
NetDial: c.Dialer,
2016-02-18 18:05:24 -08:00
Proxy: http.ProxyFromEnvironment,
}
2016-01-12 13:50:06 -08:00
rHeader := http.Header{}
conn, _, err := dialer.Dial("ws://"+c.Address+c.Endpoint, rHeader)
2016-01-12 13:50:06 -08:00
if err != nil {
return err
}
c.conn = conn
2016-01-12 13:50:06 -08:00
return nil
}
// reconnect tries to redial up to maxReconnectAttempts with exponential
// backoff.
func (c *WSClient) reconnect() error {
attempt := 0
2016-01-12 13:50:06 -08:00
2017-08-07 14:56:38 -07:00
c.mtx.Lock()
c.reconnecting = true
2017-08-07 14:56:38 -07:00
c.mtx.Unlock()
defer func() {
2017-08-07 14:56:38 -07:00
c.mtx.Lock()
c.reconnecting = false
2017-08-07 14:56:38 -07:00
c.mtx.Unlock()
}()
2016-01-12 13:50:06 -08:00
for {
c.Logger.Info("reconnecting", "attempt", attempt+1)
d := time.Duration(math.Exp2(float64(attempt)))
time.Sleep(d * time.Second)
err := c.dial()
2016-01-12 13:50:06 -08:00
if err != nil {
c.Logger.Error("failed to redial", "err", err)
2016-01-12 13:50:06 -08:00
} else {
c.Logger.Info("reconnected")
return nil
}
attempt++
if attempt > maxReconnectAttempts {
return errors.Wrap(err, "reached maximum reconnect attempts")
}
}
}
func (c *WSClient) startReadWriteRoutines() {
c.wg.Add(2)
2017-08-05 09:07:02 -07:00
c.readRoutineQuit = make(chan struct{})
go c.readRoutine()
go c.writeRoutine()
}
func (c *WSClient) reconnectRoutine() {
for {
select {
case originalError := <-c.reconnectAfter:
2017-08-05 09:07:02 -07:00
// wait until writeRoutine and readRoutine finish
c.wg.Wait()
err := c.reconnect()
if err != nil {
c.Logger.Error("failed to reconnect", "err", err, "original_err", originalError)
c.Stop()
return
} else {
// drain reconnectAfter
LOOP:
for {
select {
case <-c.reconnectAfter:
default:
break LOOP
}
}
c.startReadWriteRoutines()
}
case <-c.Quit:
return
}
}
}
// The client ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *WSClient) writeRoutine() {
ticker := time.NewTicker(c.pingPeriod)
defer func() {
ticker.Stop()
c.conn.Close()
c.wg.Done()
}()
for {
select {
case request := <-c.backlog:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteJSON(request)
2016-01-12 13:50:06 -08:00
if err != nil {
c.Logger.Error("failed to resend request", "err", err)
c.reconnectAfter <- err
// add request to the backlog, so we don't lose it
c.backlog <- request
return
2016-01-12 13:50:06 -08:00
}
c.Logger.Info("resend a request", "req", request)
case request := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteJSON(request)
if err != nil {
c.Logger.Error("failed to send request", "err", err)
c.reconnectAfter <- err
// add request to the backlog, so we don't lose it
c.backlog <- request
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
c.Logger.Error("failed to write ping", "err", err)
c.reconnectAfter <- err
return
}
2017-08-03 11:19:39 -07:00
c.mtx.Lock()
c.sentLastPingAt = time.Now()
c.mtx.Unlock()
c.Logger.Debug("sent ping")
2017-08-05 09:07:02 -07:00
case <-c.readRoutineQuit:
return
case <-c.Quit:
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
2016-01-12 13:50:06 -08:00
}
}
}
// The client ensures that there is at most one reader to a connection by
// executing all reads from this goroutine.
2017-08-05 09:07:02 -07:00
func (c *WSClient) readRoutine() {
defer func() {
c.conn.Close()
c.wg.Done()
}()
2016-01-12 13:50:06 -08:00
c.conn.SetReadDeadline(time.Now().Add(c.pongWait))
2017-08-05 09:07:02 -07:00
c.conn.SetPongHandler(func(string) error {
c.conn.SetReadDeadline(time.Now().Add(c.pongWait))
2017-08-03 11:19:39 -07:00
c.mtx.RLock()
c.PingPongLatencyTimer.UpdateSince(c.sentLastPingAt)
c.mtx.RUnlock()
c.Logger.Debug("got pong")
return nil
})
2017-08-05 09:07:02 -07:00
for {
_, data, err := c.conn.ReadMessage()
if err != nil {
if !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
return
}
c.Logger.Error("failed to read response", "err", err)
2017-08-05 09:07:02 -07:00
close(c.readRoutineQuit)
c.reconnectAfter <- err
return
}
var response types.RPCResponse
err = json.Unmarshal(data, &response)
if err != nil {
c.Logger.Error("failed to parse response", "err", err, "data", string(data))
c.ErrorsCh <- err
continue
}
if response.Error != "" {
c.ErrorsCh <- errors.Errorf(response.Error)
continue
}
c.Logger.Info("got response", "resp", response.Result)
c.ResultsCh <- *response.Result
}
2016-01-12 13:50:06 -08:00
}
///////////////////////////////////////////////////////////////////////////////
// Predefined methods
// Subscribe to an event. Note the server must have a "subscribe" route
// defined.
func (c *WSClient) Subscribe(ctx context.Context, eventType string) error {
params := map[string]interface{}{"event": eventType}
return c.Call(ctx, "subscribe", params)
2016-01-12 13:50:06 -08:00
}
// Unsubscribe from an event. Note the server must have a "unsubscribe" route
// defined.
func (c *WSClient) Unsubscribe(ctx context.Context, eventType string) error {
params := map[string]interface{}{"event": eventType}
return c.Call(ctx, "unsubscribe", params)
2016-01-12 13:50:06 -08:00
}
// UnsubscribeAll from all. Note the server must have a "unsubscribe_all" route
// defined.
func (c *WSClient) UnsubscribeAll(ctx context.Context) error {
params := map[string]interface{}{}
return c.Call(ctx, "unsubscribe_all", params)
}