Skip to content

Commit

Permalink
rpcclient: Stop client on ctx done.
Browse files Browse the repository at this point in the history
The passed context was ignored until a reconnect. This kills the client
when the passed context is done. This is much more useful for consumers
who need the client to shut down when their context if finished.
  • Loading branch information
JoeGruffins committed May 16, 2020
1 parent 1f2b1ac commit 3edcbbd
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,8 @@ func New(config *ConnConfig, ntfnHandlers *NotificationHandlers) (*Client, error
//
// This method will error if the client is not configured for websockets, if the
// connection has already been established, or if none of the connection
// attempts were successful.
// attempts were successful. The client will be shut down when the passed
// context is terminated.
func (c *Client) Connect(ctx context.Context, retry bool) error {
c.mtx.Lock()
defer c.mtx.Unlock()
Expand Down Expand Up @@ -1398,6 +1399,10 @@ func (c *Client) Connect(ctx context.Context, retry bool) error {
c.wsConn = wsConn
close(c.connEstablished)
c.start()
go func() {
<-ctx.Done()
c.Shutdown()
}()
if !c.config.DisableAutoReconnect {
c.wg.Add(2)
go c.wsReconnectHandler(ctx)
Expand Down

0 comments on commit 3edcbbd

Please sign in to comment.