Skip to content

Commit

Permalink
CR's fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 11, 2023
1 parent 0e0018f commit 7350579
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions clients/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions clients/cache/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions clients/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"context"
"fmt"
"time"

"github.com/kava-labs/kava-proxy-service/logging"
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ func (c *ServiceCache) ValidateAndCacheQueryResponse(

return nil
}

func (c *ServiceCache) Healthcheck(ctx context.Context) error {
return c.cacheClient.Healthcheck(ctx)
}
8 changes: 7 additions & 1 deletion service/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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)

Expand Down

0 comments on commit 7350579

Please sign in to comment.