From 4a12e55d6d2c9ded952dc92ebcf524fd7c7c3bd1 Mon Sep 17 00:00:00 2001 From: Powerfool Date: Wed, 27 Mar 2024 19:24:48 +0800 Subject: [PATCH] fix(http): closed http resp that occupies underlying connection (#265) --- internal/telemetry/throttler.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/telemetry/throttler.go b/internal/telemetry/throttler.go index 2409e2ab3..37e050e64 100644 --- a/internal/telemetry/throttler.go +++ b/internal/telemetry/throttler.go @@ -92,15 +92,18 @@ func (t *throttler) startWorkers() { return } res, err := t.sendTelemetryRecord(record) - if debugMode { - if err != nil { - getLogger().Printf("send telemetry record error: %v\n", err) + if err == nil && res != nil && res.Body != nil { + if debugMode { + bts, err := io.ReadAll(res.Body) + if err != nil { + getLogger().Printf("read response body error: %v\n", err) + } else { + getLogger().Printf("[Event %s.%s] %s\n", record.ResourceType, record.EventType, string(bts)) + } + } else { + _, _ = io.Copy(io.Discard, res.Body) } - bts, err := io.ReadAll(res.Body) - if err != nil { - getLogger().Printf("read response body error: %v\n", err) - } - getLogger().Printf("[Event %s.%s] %s\n", record.ResourceType, record.EventType, string(bts)) + _ = res.Body.Close() } case <-ctx.Done(): getLogger().Println(ctx.Err())