From 7350579a70a8e0b24ed13f1ccd3fd2d721998620 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Wed, 11 Oct 2023 10:10:38 -0400 Subject: [PATCH] CR's fixes --- clients/cache/cache.go | 1 + clients/cache/inmemory.go | 4 ++++ clients/cache/redis.go | 11 +++++++++++ service/cachemdw/cache.go | 4 ++++ service/handlers.go | 8 +++++++- 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/clients/cache/cache.go b/clients/cache/cache.go index addf846..cc6f9cb 100644 --- a/clients/cache/cache.go +++ b/clients/cache/cache.go @@ -12,4 +12,5 @@ type Cache interface { Set(ctx context.Context, key string, data []byte, expiration time.Duration) error Get(ctx context.Context, key string) ([]byte, error) Delete(ctx context.Context, key string) error + Healthcheck(ctx context.Context) error } diff --git a/clients/cache/inmemory.go b/clients/cache/inmemory.go index f298958..9293af2 100644 --- a/clients/cache/inmemory.go +++ b/clients/cache/inmemory.go @@ -95,3 +95,7 @@ func (c *InMemoryCache) Delete(ctx context.Context, key string) error { delete(c.data, key) return nil } + +func (c *InMemoryCache) Healthcheck(ctx context.Context) error { + return nil +} diff --git a/clients/cache/redis.go b/clients/cache/redis.go index 16da45a..f402652 100644 --- a/clients/cache/redis.go +++ b/clients/cache/redis.go @@ -2,6 +2,7 @@ package cache import ( "context" + "fmt" "time" "github.com/kava-labs/kava-proxy-service/logging" @@ -68,3 +69,13 @@ func (rc *RedisCache) Get( func (rc *RedisCache) Delete(ctx context.Context, key string) error { return rc.client.Del(ctx, key).Err() } + +func (rc *RedisCache) Healthcheck(ctx context.Context) error { + // Check if we can connect to Redis + _, err := rc.client.Ping(ctx).Result() + if err != nil { + return fmt.Errorf("error connecting to Redis: %v", err) + } + + return nil +} diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index a2607c4..6084c2a 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -111,3 +111,7 @@ func (c *ServiceCache) ValidateAndCacheQueryResponse( return nil } + +func (c *ServiceCache) Healthcheck(ctx context.Context) error { + return c.cacheClient.Healthcheck(ctx) +} diff --git a/service/handlers.go b/service/handlers.go index 338071e..c615b03 100644 --- a/service/handlers.go +++ b/service/handlers.go @@ -1,6 +1,7 @@ package service import ( + "context" "encoding/json" "errors" "fmt" @@ -20,12 +21,17 @@ func createHealthcheckHandler(service *ProxyService) func(http.ResponseWriter, * // check that the database is reachable err := service.Database.HealthCheck() - if err != nil { errMsg := fmt.Errorf("proxy service unable to connect to database") combinedErrors = errors.Join(combinedErrors, errMsg) } + err = service.Cache.Healthcheck(context.Background()) + if err != nil { + errMsg := fmt.Errorf("proxy service unable to connect to cache") + combinedErrors = errors.Join(combinedErrors, errMsg) + } + if combinedErrors != nil { w.WriteHeader(http.StatusInternalServerError)