pgweb/pkg/api/session_cleanup.go

42 lines
794 B
Go
Raw Normal View History

2017-09-22 22:44:32 -05:00
package api
import (
"log"
"time"
"github.com/sosedoff/pgweb/pkg/command"
)
2018-11-30 21:40:28 -06:00
// StartSessionCleanup starts a goroutine to cleanup idle database sessions
func StartSessionCleanup() {
for range time.Tick(time.Minute) {
if command.Opts.Debug {
log.Println("Triggering idle session deletion")
}
cleanupIdleSessions()
}
}
2017-09-22 22:44:32 -05:00
func cleanupIdleSessions() {
ids := []string{}
// Figure out which sessions are idle
for id, client := range DbSessions {
if client.IsIdle() {
ids = append(ids, id)
}
}
if len(ids) == 0 {
return
}
2018-11-30 21:40:28 -06:00
// Close and delete idle sessions
2017-09-22 22:44:32 -05:00
log.Println("Closing", len(ids), "idle sessions")
for _, id := range ids {
// TODO: concurrent map edit will trigger panic
if err := DbSessions[id].Close(); err == nil {
delete(DbSessions, id)
}
}
}