Skip to content

Commit

Permalink
WIP: add cleanup and commands left in /health headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavvajpayee committed Oct 28, 2024
1 parent e0fc3e2 commit 6302b60
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
3 changes: 1 addition & 2 deletions internal/middleware/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ func calculateNextCleanupTime(ctx context.Context, client *db.DiceDB, cronFreque

lastCleanupTime := time.UnixMilli(lastCronCleanupTime)
nextCleanupTime := lastCleanupTime.Add(cronFrequencyInterval)
timeDifference := nextCleanupTime.Sub(time.Now())
return int64(timeDifference.Seconds()), nil
return int64(time.Until(nextCleanupTime).Seconds()), nil
}

func MockRateLimiter(client *mock.DiceDBMock, next http.Handler, limit int64, window float64) http.Handler {
Expand Down
49 changes: 49 additions & 0 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"errors"
"log/slog"
"net/http"
"strconv"
"strings"
"time"

"server/internal/db"
"server/internal/middleware"
"server/internal/server/utils"
util "server/util"

"github.com/dicedb/dicedb-go"
)

type HTTPServer struct {
Expand Down Expand Up @@ -100,6 +104,22 @@ func (s *HTTPServer) Shutdown() error {
}

func (s *HTTPServer) HealthCheck(w http.ResponseWriter, request *http.Request) {
nextCleanup, err := s.getNextCleanupTime()
if err != nil {
slog.Error("Failed to get the cleanupTime", slog.Any("err", err))
http.Error(w, errorResponse("internal server error"), http.StatusInternalServerError)
return
}

commandsLeft, err := s.getCommandsLeft()
if err != nil {
slog.Error("Failed to get the commands Left", slog.Any("err", err))
http.Error(w, errorResponse("internal server error"), http.StatusInternalServerError)
return
}

w.Header().Set("x-next-cleanup-time", strconv.FormatInt(nextCleanup, 10))
w.Header().Set("x-commands-left", strconv.FormatInt(commandsLeft, 10))
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "server is running"})
}

Expand Down Expand Up @@ -142,3 +162,32 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) {
func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) {
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "search results"})
}

func (s *HTTPServer) getNextCleanupTime() (int64, error) {
resp := s.DiceClient.Client.Get(context.Background(), utils.LastCronCleanupTimeUnixMs)
if resp.Err() != nil {
if errors.Is(resp.Err(), dicedb.Nil) {
return time.Now().UnixMilli(), nil
}
return 0, resp.Err()
}

lastCleanupStr := resp.Val()
if lastCleanupStr == "" {
return 0, resp.Err()
}

lastCleanup, err := strconv.ParseInt(lastCleanupStr, 10, 64)
if err != nil {
return 0, err
}
lastCleanupTime := time.UnixMilli(lastCleanup)
nextCleanupTime := lastCleanupTime.Add(15 * time.Minute).UnixMilli()

return nextCleanupTime, nil
}

func (s *HTTPServer) getCommandsLeft() (int64, error) {
// clarification required
return 1, nil
}

0 comments on commit 6302b60

Please sign in to comment.