From 54903adeff36c250a00e47ded2b4fd3899a24253 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 3 Aug 2017 14:20:17 -0400 Subject: [PATCH] add IsReconnecting and IsActive methods --- rpc/lib/client/ws_client.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rpc/lib/client/ws_client.go b/rpc/lib/client/ws_client.go index 9e456c6d..b70ed359 100644 --- a/rpc/lib/client/ws_client.go +++ b/rpc/lib/client/ws_client.go @@ -54,6 +54,8 @@ type WSClient struct { reconnectAfter chan error // reconnect requests receiveRoutineQuit chan struct{} // a way for receiveRoutine to close writeRoutine + reconnecting bool + wg sync.WaitGroup mtx sync.RWMutex } @@ -115,6 +117,16 @@ func (c *WSClient) Stop() bool { return success } +// IsReconnecting returns true if the client is reconnecting right now. +func (c *WSClient) IsReconnecting() bool { + return c.reconnecting +} + +// IsActive returns true if the client is running and not reconnecting. +func (c *WSClient) IsActive() bool { + return c.IsRunning() && !c.IsReconnecting() +} + // Send asynchronously sends the given RPCRequest to the server. Results will // be available on ResultsCh, errors, if any, on ErrorsCh. func (c *WSClient) Send(ctx context.Context, request types.RPCRequest) error { @@ -170,6 +182,11 @@ func (c *WSClient) dial() error { func (c *WSClient) reconnect() error { attempt := 0 + c.reconnecting = true + defer func() { + c.reconnecting = false + }() + for { c.Logger.Info("reconnecting", "attempt", attempt+1)