diff --git a/pkg/database/disc.go b/pkg/database/disc.go index 65556f4..ef50ca9 100644 --- a/pkg/database/disc.go +++ b/pkg/database/disc.go @@ -139,7 +139,7 @@ func (db *DB) UpsertNode(ctx context.Context, tx pgx.Tx, node *enode.Node) error ) VALUES ( @node_id, now(), - now() + now() + random_seconds(300) ) ON CONFLICT (node_id) DO UPDATE SET last_found = now(), diff --git a/pkg/database/migrate.go b/pkg/database/migrate.go index 87b2b08..b8ffc9e 100644 --- a/pkg/database/migrate.go +++ b/pkg/database/migrate.go @@ -48,6 +48,7 @@ func (db *DB) Migrate(geoipdb string) error { }, "function client.upsert": migrations.ClientUpsertStrings, "function execution.capabilities_upsert": migrations.ExecutionCapabilitiesUpsert, + "function random_seconds": migrations.RandomSeconds, "geoip": func(ctx context.Context, tx pgx.Tx) error { return migrations.UpdateGeoIPData(ctx, tx, geoipdb) }, diff --git a/pkg/database/migrations/upsert_strings.go b/pkg/database/migrations/upsert_strings.go index b584e6f..eb33551 100644 --- a/pkg/database/migrations/upsert_strings.go +++ b/pkg/database/migrations/upsert_strings.go @@ -7,6 +7,23 @@ import ( "github.com/jackc/pgx/v5" ) +func RandomSeconds(ctx context.Context, tx pgx.Tx) error { + _, err := tx.Exec( + ctx, + ` + CREATE OR REPLACE FUNCTION random_seconds(max_seconds INTEGER) RETURNS INTERVAL + AS 'SELECT make_interval(secs => random() * max_seconds);' + IMMUTABLE + LANGUAGE SQL + `, + ) + if err != nil { + return fmt.Errorf("exec: %w", err) + } + + return nil +} + func ExecutionCapabilitiesUpsert(ctx context.Context, tx pgx.Tx) error { _, err := tx.Exec( ctx,