Add scale limit constants, stale-agent indexed query, and tests.
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled

Extract fleet caps (128 hosts, sem=16, 250ms coalesce, syscheck=20) into
named constants with Go tests; wire ListStaleOnlineAgents for stale sweeps
instead of full ListAgents scans.
This commit is contained in:
AetherForge
2026-06-07 06:40:12 -07:00
parent 148c9e2248
commit 72ae457cca
12 changed files with 240 additions and 9 deletions

View File

@@ -0,0 +1,30 @@
package db
import (
"time"
"crypto-miner-server/internal/models"
)
// ListStaleOnlineAgents returns online agents whose last_seen is older than
// the given threshold. Uses indexed status+last_seen filters instead of a full
// table scan.
func (d *Database) ListStaleOnlineAgents(olderThan time.Duration) ([]*models.Agent, error) {
cutoff := time.Now().Add(-olderThan)
query := `SELECT ` + agentSelectCols + ` FROM agents WHERE status = 'online' AND last_seen < ? ORDER BY last_seen ASC`
rows, err := d.Query(query, cutoff)
if err != nil {
return nil, err
}
defer rows.Close()
var agents []*models.Agent
for rows.Next() {
a, err := d.scanAgent(rows)
if err != nil {
return nil, err
}
agents = append(agents, a)
}
return agents, rows.Err()
}