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:
parent
ed93454eb9
commit
e93dc8f061
13
main.go
13
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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user