61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package web
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"git.jmbit.de/jmb/podterminal/pods"
|
|
)
|
|
|
|
type sessionData struct {
|
|
lastAccess *time.Time
|
|
sessionID string
|
|
}
|
|
|
|
var invalidSessions []string
|
|
var sessionLastAccess map[string]*sessionData
|
|
|
|
func initSessionAging() error {
|
|
sessionLastAccess = make(map[string]*sessionData)
|
|
|
|
return nil
|
|
}
|
|
|
|
func updateSession(id string, sessionID string) {
|
|
nowTime := time.Now()
|
|
sessionLastAccess[id].lastAccess = &nowTime
|
|
sessionLastAccess[id].sessionID = sessionID
|
|
}
|
|
|
|
func deleteIdleSessions() {
|
|
idleTimout := viper.GetInt("session_timeout")
|
|
tenMinutesAgo := -time.Duration(idleTimout) * time.Second
|
|
oldAge := time.Now().Add(tenMinutesAgo)
|
|
for session, sessionData := range sessionLastAccess {
|
|
log.Printf("Session %s last connected at %s", session, sessionData.lastAccess.String())
|
|
if oldAge.After(*sessionData.lastAccess) {
|
|
pods.DestroyContainer(session)
|
|
invalidSessions = append(invalidSessions, sessionData.sessionID)
|
|
// Delete Proxy entry to avoid 502s
|
|
delete(proxies, session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func IdleSessionCleanup() error {
|
|
err := initSessionAging()
|
|
if err != nil {
|
|
println("Could not initialize Session aging")
|
|
return err
|
|
}
|
|
for {
|
|
deleteIdleSessions()
|
|
time.Sleep(time.Duration(viper.GetInt("session_timeout")) * time.Second)
|
|
}
|
|
}
|