From 15f78f0d7aef06256dcc2d73f751417264ff9d49 Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Wed, 17 Jul 2024 23:06:44 +0200 Subject: [PATCH] [CLIENT-3022] Optimize the ticker --- README.md | 2 +- proxy_auth_interceptor.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 36e82911..976cf1c9 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ To make the library both flexible and fast, we had to integrate the reflection A ## Proxy Client / DBAAS -To compile the client for the proxy server in out database as a service environment, pass `-tags as_proxy` to the compiler on build. +To compile the client for the proxy server in our database-as-a-service (dbaas) environment, pass `-tags as_proxy` to the compiler on build. ## License diff --git a/proxy_auth_interceptor.go b/proxy_auth_interceptor.go index 788d7386..3f567841 100644 --- a/proxy_auth_interceptor.go +++ b/proxy_auth_interceptor.go @@ -92,11 +92,13 @@ func (interceptor *authInterceptor) tokenRefresher() { // provide 5 secs of buffer before expiry due to network latency wait := interceptor.expiry.Sub(time.Now()) - 5*time.Second + ticker := time.NewTicker(wait) + defer ticker.Close() + for { - ticker := time.NewTicker(wait) + ticker.Reset(wait) select { case <-ticker.C: - ticker.Stop() // prevent goroutine leak err := interceptor.refreshToken() if err != nil { wait = time.Second @@ -105,7 +107,6 @@ func (interceptor *authInterceptor) tokenRefresher() { } case <-interceptor.closer: - ticker.Stop() // prevent goroutine leak // channel closed; return from the goroutine return }