From 8a4535e6adb45f719f8f73827dc5fd1ebe824bc5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 24 Jan 2018 12:36:29 +0000 Subject: [PATCH] Re-use timer instead of creating new ones on every sample The docs for `time.After()` note that "The underlying Timer is not recovered by the garbage collector until the timer fires". --- storage/remote/queue_manager.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index 5e30fd0f6..9c7f66a94 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -430,6 +430,8 @@ func (s *shards) runShard(i int) { // anyways. pendingSamples := model.Samples{} + timer := time.NewTimer(s.qm.cfg.BatchSendDeadline) + for { select { case sample, ok := <-queue: @@ -449,7 +451,11 @@ func (s *shards) runShard(i int) { s.sendSamples(pendingSamples[:s.qm.cfg.MaxSamplesPerSend]) pendingSamples = pendingSamples[s.qm.cfg.MaxSamplesPerSend:] } - case <-time.After(s.qm.cfg.BatchSendDeadline): + if !timer.Stop() { + <-timer.C + } + timer.Reset(s.qm.cfg.BatchSendDeadline) + case <-timer.C: if len(pendingSamples) > 0 { s.sendSamples(pendingSamples) pendingSamples = pendingSamples[:0]