From 831fed82f78393122e2379d5e1c47b41eb35513f Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 28 Jun 2026 09:23:11 -0600 Subject: [PATCH] go delete success from more than 12h ago --- cmd/pinger/main.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/cmd/pinger/main.go b/cmd/pinger/main.go index 48f4b06..847f456 100644 --- a/cmd/pinger/main.go +++ b/cmd/pinger/main.go @@ -42,6 +42,7 @@ func run(ctx context.Context) error { defer wg.Wait() for _, foo := range []func(context.Context) error{ s.pingAndRecord, + s.collectGarbage, s.listen, } { foo := foo @@ -62,10 +63,10 @@ func (s S) init(ctx context.Context) error { , addr TEXT NOT NULL UNIQUE )`, `CREATE TABLE IF NOT EXISTS "pinger_pings" ( - start TIMESTAMP NOT NULL + start TIMESTAMP NOT NULL , ms NUMBER NOT NULL , ok BOOLEAN NOT NULL DEFAULT false - , to_id NUMBER + , to_id NUMBER , FOREIGN KEY (to_id) REFERENCES "pinger_to"(id) )`, //`ALTER TABLE "pinger_pings" SET UNLOGGED`, @@ -141,6 +142,27 @@ func (s S) listen(ctx context.Context) error { return nil } +func (s S) collectGarbage(ctx context.Context) error { + c := time.NewTicker(time.Hour) + defer c.Stop() + + for range c.C { + if _, err := s.ExecContext(ctx, ` + DELETE FROM "pinger_pings" + WHERE + start < $1 + AND ( + ms < 100 + OR ok + ) + `, time.Now().Add(-1*12*time.Hour).Unix()); err != nil { + log.Println("!", err) + } + } + + return nil +} + func (s S) pingAndRecord(ctx context.Context) error { ctx, can := context.WithCancel(ctx) defer can() @@ -183,7 +205,7 @@ func (s S) pingAndRecord(ctx context.Context) error { go func() { for record := range records { if _, err := s.ExecContext(ctx, ` - INSERT INTO "pinger_pings" + INSERT INTO "pinger_pings" (start, ms, ok, to_id) VALUES ($1, $2, $3, $4)