Automatically close idle sessions
This commit is contained in:
45
pkg/api/session_cleanup.go
Normal file
45
pkg/api/session_cleanup.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/sosedoff/pgweb/pkg/command"
|
||||
)
|
||||
|
||||
func cleanupIdleSessions() {
|
||||
ids := []string{}
|
||||
|
||||
// Figure out which sessions are idle
|
||||
for id, client := range DbSessions {
|
||||
if client.IsIdle() {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Close and delete idle sessions
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StartSessionCleanup() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
|
||||
for {
|
||||
<-ticker.C
|
||||
|
||||
if command.Opts.Debug {
|
||||
log.Println("Triggering idle session deletion")
|
||||
}
|
||||
|
||||
cleanupIdleSessions()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user