From 6172d5389dd8088d4bb6b0336a6444cb3231a409 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Thu, 16 Apr 2026 08:38:40 +0800 Subject: [PATCH] fix(example): close HTTP response body and add retry backoff The sendCountRequest function in the exactly-once-delivery-counter example has two issues: 1. resp.Body is never closed, leaking file descriptors and TCP connections under load. 2. On error or non-204 status, the function retries immediately in a tight loop with no backoff, potentially overwhelming the server. Close the response body after reading the status code and add a 100ms sleep between retries. Fixes #664 --- .../exactly-once-delivery-counter/run.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/_examples/real-world-examples/exactly-once-delivery-counter/run.go b/_examples/real-world-examples/exactly-once-delivery-counter/run.go index cb4ead13d..585f186d7 100644 --- a/_examples/real-world-examples/exactly-once-delivery-counter/run.go +++ b/_examples/real-world-examples/exactly-once-delivery-counter/run.go @@ -139,12 +139,18 @@ func sendCountRequest(counterUUID string) { for { resp, err := http.Post("http://localhost:8080/count/"+counterUUID, "", nil) if err != nil { + time.Sleep(100 * time.Millisecond) continue } - if resp.StatusCode == http.StatusNoContent { + status := resp.StatusCode + resp.Body.Close() + + if status == http.StatusNoContent { break } + + time.Sleep(100 * time.Millisecond) } }