Refactor api package

This commit is contained in:
Dan Sosedoff
2018-11-30 21:40:28 -06:00
parent b86a849e00
commit 72af00f1b7
5 changed files with 148 additions and 123 deletions

View File

@@ -7,6 +7,16 @@ import (
"github.com/sosedoff/pgweb/pkg/command"
)
// 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()
}
}
func cleanupIdleSessions() {
ids := []string{}
@@ -16,11 +26,11 @@ func cleanupIdleSessions() {
ids = append(ids, id)
}
}
// Close and delete idle sessions
if len(ids) == 0 {
return
}
// Close and delete idle sessions
log.Println("Closing", len(ids), "idle sessions")
for _, id := range ids {
// TODO: concurrent map edit will trigger panic
@@ -29,17 +39,3 @@ func cleanupIdleSessions() {
}
}
}
func StartSessionCleanup() {
ticker := time.NewTicker(time.Minute)
for {
<-ticker.C
if command.Opts.Debug {
log.Println("Triggering idle session deletion")
}
cleanupIdleSessions()
}
}