Skip to content

Commit

Permalink
jsonrpc: Add Close API to close the socket.
Browse files Browse the repository at this point in the history
Also, be sure to close the socket when the run is done.
  • Loading branch information
dajohi authored and miki-totefu committed Feb 16, 2023
1 parent 9b3ac76 commit 0d85ba1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions clientrpc/jsonrpc/wspeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type wsPeer struct {
lastWriter io.WriteCloser
}

func (p *wsPeer) close() error {
return p.conn.Close()
}

func (p *wsPeer) request(ctx context.Context, method string, req, res proto.Message) error {
return p.p.request(ctx, method, req, res)
}
Expand Down Expand Up @@ -67,6 +71,8 @@ func (p *wsPeer) flushLastWrite() error {
}

func (p *wsPeer) run(ctx context.Context) error {
defer p.close()

g, gctx := errgroup.WithContext(ctx)

pongChan := make(chan [pingPayloadSize]byte)
Expand Down Expand Up @@ -171,6 +177,16 @@ type WSClient struct {
waitingPeer []chan *wsPeer
}

func (c *WSClient) Close() error {
c.mtx.Lock()
var err error
if c.peer != nil {
err = c.peer.close()
}
c.mtx.Unlock()
return err
}

// nextPeer returns the currently running peer or waits until the next peer
// is available on which to execute a request.
func (c *WSClient) nextPeer(ctx context.Context) (*wsPeer, error) {
Expand Down

0 comments on commit 0d85ba1

Please sign in to comment.