From 2469d2c08d78e79b1ff1e5962d96a36b0374775e Mon Sep 17 00:00:00 2001 From: Jarno Rajahalme Date: Thu, 25 Apr 2024 12:14:10 +0200 Subject: [PATCH] shared_client: Bump request id Only fail out if non-conflicting request id can not be found. This works on the premise that the callers are fine with the request id being modified at this point. Current use sets a random id just prior to Exchange call, so this premise is satisfied. Signed-off-by: Jarno Rajahalme --- shared_client.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/shared_client.go b/shared_client.go index 0b8bbeec7..b170c73c4 100644 --- a/shared_client.go +++ b/shared_client.go @@ -232,10 +232,19 @@ func handler(wg *sync.WaitGroup, client *Client, conn *Conn, requests chan reque // Due to birthday paradox and the fact that ID is uint16 // it's likely to happen with small number (~200) of concurrent requests // which would result in goroutine leak as we would never close req.ch - if _, ok := waitingResponses[req.msg.Id]; ok { - req.ch <- sharedClientResponse{nil, 0, fmt.Errorf("duplicate request id %d", req.msg.Id)} - close(req.ch) - continue + if _, duplicate := waitingResponses[req.msg.Id]; duplicate { + // find next available ID + for id := req.msg.Id + 1; id != req.msg.Id; id++ { + if _, duplicate = waitingResponses[id]; !duplicate { + req.msg.Id = id + break + } + } + if duplicate { + req.ch <- sharedClientResponse{nil, 0, fmt.Errorf("duplicate request id %d", req.msg.Id)} + close(req.ch) + continue + } } err := client.SendContext(req.ctx, req.msg, conn, start)