From e93dc8f06190c785fbb8d3cc6c69fb8d572c775d Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Wed, 31 Jan 2018 11:20:07 -0800 Subject: [PATCH] Periodically prune tokens. Every 15 minutes, or whenever a packet is processed and more than 15 minutes have elapsed, we prune tokens that have expired and expired more than 24h earlier. We want to keep expired tokens around for a little bit so we can inform clients that their tokens were expired, instead of replying with less informative error messages. --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 2906877..dac3a35 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func (writer logWriter) Write(bytes []byte) (int, error) { // DB related stuff var db *sql.DB +var pruned int64 type Token struct { token string cookie string @@ -749,7 +750,17 @@ send_response: w.Header().Set("Content-Type", "application/json") w.Write(output) - //FIXME prune tokens + // prune tokens occasionally + if time.Now().Unix() > pruned + 900 { + pruned = time.Now().Unix() + cull := time.Now().Unix() - 86400 + + _, err = db.Exec("DELETE FROM tokens WHERE expired <= ?", cull) + if err != nil { + log.Println("Failed to prune tokens.") + } + } + } func main() {