From 97208710888c83eb0a94637e275361a0e93480e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emir=20Ribi=C4=87?= Date: Fri, 19 Jul 2024 17:44:18 +0300 Subject: [PATCH] Init limits if nil (#844) --- transport.go | 10 +++++++++- transport_test.go | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/transport.go b/transport.go index 20f69941f..02fc1d4f1 100644 --- a/transport.go +++ b/transport.go @@ -357,7 +357,6 @@ func NewHTTPTransport() *HTTPTransport { transport := HTTPTransport{ BufferSize: defaultBufferSize, Timeout: defaultTimeout, - limits: make(ratelimit.Map), } return &transport } @@ -550,9 +549,14 @@ func (t *HTTPTransport) worker() { } Logger.Printf("Sending %s failed with the following error: %s", eventType, string(b)) } + t.mu.Lock() + if t.limits == nil { + t.limits = make(ratelimit.Map) + } t.limits.Merge(ratelimit.FromResponse(response)) t.mu.Unlock() + // Drain body up to a limit and close it, allowing the // transport to reuse TCP connections. _, _ = io.CopyN(io.Discard, response.Body, maxDrainResponseBytes) @@ -690,6 +694,10 @@ func (t *HTTPSyncTransport) SendEventWithContext(ctx context.Context, event *Eve } t.mu.Lock() + if t.limits == nil { + t.limits = make(ratelimit.Map) + } + t.limits.Merge(ratelimit.FromResponse(response)) t.mu.Unlock() diff --git a/transport_test.go b/transport_test.go index 9d70db2f7..b371cae45 100644 --- a/transport_test.go +++ b/transport_test.go @@ -434,6 +434,7 @@ func TestHTTPTransport(t *testing.T) { e.EventID = EventID(id) transport.SendEvent(e) + t.Logf("[CLIENT] {%.4s} event sent", e.EventID) return id }