Skip to content

Commit c615d3a

Browse files
committed
fix: cleanup and do not accept empty versions and heartbeats
1 parent d9b0e35 commit c615d3a

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

database/queries/query.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

instances_handler.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"log/slog"
77
"net/http"
8+
"strings"
89
"time"
910

1011
"github.com/steveiliop56/tinyauth-analytics/database/queries"
@@ -60,6 +61,15 @@ func (h *InstancesHandler) Heartbeat(w http.ResponseWriter, r *http.Request) {
6061
return
6162
}
6263

64+
if strings.TrimSpace(heartbeat.UUID) == "" || strings.TrimSpace(heartbeat.Version) == "" {
65+
w.WriteHeader(http.StatusBadRequest)
66+
render.JSON(w, r, map[string]string{
67+
"status": "400",
68+
"message": "Invalid request payload",
69+
})
70+
return
71+
}
72+
6373
_, err = h.queries.GetInstance(r.Context(), heartbeat.UUID)
6474

6575
if err != nil && !errors.Is(err, sql.ErrNoRows) {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func cleanUpOldInstances(queries *queries.Queries) {
116116
ticker := time.NewTicker(24 * time.Hour)
117117
defer ticker.Stop()
118118

119-
for ; true; <-ticker.C {
119+
for range ticker.C {
120120
slog.Info("cleaning up old instances")
121121

122122
cutoffTime := time.Now().Add(-48 * time.Hour).UnixMilli()

query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ WHERE uuid = ?;
2323

2424
-- name: DeleteOldInstances :many
2525
DELETE FROM instances
26-
WHERE last_seen < ?
27-
RETURNING *;
26+
WHERE last_seen < ? OR version = '' OR uuid = ''
27+
RETURNING *;

0 commit comments

Comments
 (0)