2017-09-23 06:44:32 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
|
|
|
)
|
|
|
|
|
2018-12-01 06:40:28 +03: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-23 06:44:32 +03: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-12-01 06:40:28 +03:00
|
|
|
|
|
|
|
// Close and delete idle sessions
|
2017-09-23 06:44:32 +03: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|