Skip to content

Commit

Permalink
added commands left to headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavvajpayee committed Oct 29, 2024
1 parent 6302b60 commit 5febcce
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strconv"
"strings"
"time"

"server/config"
"server/internal/db"
"server/internal/middleware"
"server/internal/server/utils"
Expand Down Expand Up @@ -188,6 +190,26 @@ func (s *HTTPServer) getNextCleanupTime() (int64, error) {
}

func (s *HTTPServer) getCommandsLeft() (int64, error) {
// clarification required
return 1, nil
configValue := config.LoadConfig()
currentWindow := time.Now().Unix() / int64(configValue.Server.RequestWindowSec)
key := fmt.Sprintf("request_count:%d", currentWindow)

val, err := s.DiceClient.Client.Get(context.Background(), key).Result()
if err != nil {
if errors.Is(err, dicedb.Nil) {
return 1000, nil
}
return 0, err
}

requestCount, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, err
}

remaining := int64(1000) - requestCount
if remaining < 0 {
remaining = 0
}
return remaining, nil
}

0 comments on commit 5febcce

Please sign in to comment.