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.
This commit is contained in:
Auke Kok 2018-01-31 11:20:07 -08:00
parent ed93454eb9
commit e93dc8f061

13
main.go
View File

@ -31,6 +31,7 @@ func (writer logWriter) Write(bytes []byte) (int, error) {
// DB related stuff // DB related stuff
var db *sql.DB var db *sql.DB
var pruned int64
type Token struct { type Token struct {
token string token string
cookie string cookie string
@ -749,7 +750,17 @@ send_response:
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write(output) 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() { func main() {