From 92b4922d1814a8b65375a0bafc59d92465d59a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Nussbaumer?= Date: Tue, 6 Feb 2024 18:17:32 +0100 Subject: [PATCH] chore: remove "caching" of results and simplify code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Clément Nussbaumer --- internal/kubenurse/handler.go | 8 +++---- .../kubenurse/{k8s-auth.go => k8s_auth.go} | 0 internal/kubenurse/server.go | 2 +- internal/servicecheck/cache.go | 22 ------------------- internal/servicecheck/servicecheck.go | 10 ++------- internal/servicecheck/types.go | 10 ++------- 6 files changed, 9 insertions(+), 43 deletions(-) rename internal/kubenurse/{k8s-auth.go => k8s_auth.go} (100%) delete mode 100644 internal/servicecheck/cache.go diff --git a/internal/kubenurse/handler.go b/internal/kubenurse/handler.go index 0b95ba70..e71dcb97 100644 --- a/internal/kubenurse/handler.go +++ b/internal/kubenurse/handler.go @@ -38,15 +38,15 @@ func (s *Server) aliveHandler() func(w http.ResponseWriter, r *http.Request) { Neighbourhood []servicecheck.Neighbour `json:"neighbourhood"` } - // Run checks now - res, haserr := s.checker.Run() - if haserr { + res := s.checker.LastCheckResult + if res == nil { w.WriteHeader(http.StatusInternalServerError) + return } // Add additional data out := Output{ - Result: res, + Result: *res, Headers: r.Header, UserAgent: r.UserAgent(), RequestURI: r.RequestURI, diff --git a/internal/kubenurse/k8s-auth.go b/internal/kubenurse/k8s_auth.go similarity index 100% rename from internal/kubenurse/k8s-auth.go rename to internal/kubenurse/k8s_auth.go diff --git a/internal/kubenurse/server.go b/internal/kubenurse/server.go index f85a927a..1059ab05 100644 --- a/internal/kubenurse/server.go +++ b/internal/kubenurse/server.go @@ -118,7 +118,7 @@ func New(ctx context.Context, c client.Client) (*Server, error) { //nolint:funle } // setup checker - chk, err := servicecheck.New(ctx, c, promRegistry, server.allowUnschedulable, 3*time.Second, histogramBuckets) + chk, err := servicecheck.New(ctx, c, promRegistry, server.allowUnschedulable, 1*time.Second, histogramBuckets) if err != nil { return nil, err } diff --git a/internal/servicecheck/cache.go b/internal/servicecheck/cache.go deleted file mode 100644 index 72fbf22c..00000000 --- a/internal/servicecheck/cache.go +++ /dev/null @@ -1,22 +0,0 @@ -package servicecheck - -import "time" - -// retrieveResultFromCache returns the latest check result from cache, if any. -// If the result is expired or none is available, this function will return nil. -func (c *Checker) retrieveResultFromCache() *Result { - if c.cachedResult != nil && c.cachedResult.expiration.After(time.Now()) { - return c.cachedResult.result - } - - return nil -} - -// cacheResult sets a check result to the cache and expires it after the -// Checker.cacheTTL is exceeded. -func (c *Checker) cacheResult(result *Result) { - c.cachedResult = &CachedResult{ - result: result, - expiration: time.Now().Add(c.cacheTTL), - } -} diff --git a/internal/servicecheck/servicecheck.go b/internal/servicecheck/servicecheck.go index 7178513f..eee65e37 100644 --- a/internal/servicecheck/servicecheck.go +++ b/internal/servicecheck/servicecheck.go @@ -96,12 +96,6 @@ func (c *Checker) Run() (Result, bool) { err error ) - // Check if a result is cached and return it - cacheRes := c.retrieveResultFromCache() - if cacheRes != nil { - return *cacheRes, false - } - // Run Checks res := Result{} @@ -134,8 +128,8 @@ func (c *Checker) Run() (Result, bool) { } } - // Cache result - c.cacheResult(&res) + // Cache result (used for /alive handler) + c.LastCheckResult = &res return res, haserr } diff --git a/internal/servicecheck/types.go b/internal/servicecheck/types.go index c1d72e5e..fede2c3c 100644 --- a/internal/servicecheck/types.go +++ b/internal/servicecheck/types.go @@ -45,8 +45,8 @@ type Checker struct { // Http Client for https requests httpClient *http.Client - // cachedResult represents a cached check result - cachedResult *CachedResult + // LastCheckResult represents a cached check result + LastCheckResult *Result // cacheTTL defines the TTL of how long a cached result is valid cacheTTL time.Duration @@ -67,9 +67,3 @@ type Result struct { // Check is the signature used by all checks that the checker can execute. type Check func(ctx context.Context) (string, error) - -// CachedResult represents a cached check result that is valid until the expiration. -type CachedResult struct { - result *Result - expiration time.Time -}