From e8e09f0d0074287da122f9d4695bb06ba28132d9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 11 Apr 2022 13:47:29 +0300 Subject: [PATCH] [#393] rpc/grpc: Use `NewTimer` instead of `time.After` From the docs of `time.After`: ``` The underlying Timer is not recovered by the garbage collector until the timer fires. ``` We have 1 minute default timeout, which is pretty long given that most of the time we exchange small messages. Signed-off-by: Evgenii Stratonikov --- rpc/grpc/init.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rpc/grpc/init.go b/rpc/grpc/init.go index 9cf41d72..31a2ba78 100644 --- a/rpc/grpc/init.go +++ b/rpc/grpc/init.go @@ -57,10 +57,13 @@ func (w *streamWrapper) withTimeout(closure func() error) error { close(ch) }() + tt := time.NewTimer(w.timeout) + select { case err := <-ch: + tt.Stop() return err - case <-time.After(w.timeout): + case <-tt.C: w.cancel() return context.DeadlineExceeded }