Skip to content

Commit

Permalink
Merge pull request #260 from ChIoT-Tech/master
Browse files Browse the repository at this point in the history
Prevent RPC blocking when context cancelled
  • Loading branch information
MattBrittan authored Jul 29, 2024
2 parents 2c54a6d + 749c1ed commit ef148e4
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions paho/extensions/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (h *Handler) getCorrelIDChan(cID string) chan *paho.Publish {

func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (*paho.Publish, error) {
cID := fmt.Sprintf("%d", time.Now().UnixNano())
rChan := make(chan *paho.Publish)
rChan := make(chan *paho.Publish, 1) // Buffered to prevent goroutine leak when context cancelled

h.addCorrelID(cID, rChan)

Expand All @@ -95,8 +95,13 @@ func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (*paho.Publish,
return nil, err
}

resp := <-rChan
return resp, nil
select {
case resp := <-rChan:
return resp, nil
case <-ctx.Done():
return nil, ctx.Err()
}

}

func (h *Handler) responseHandler(pb *paho.Publish) {
Expand Down

0 comments on commit ef148e4

Please sign in to comment.