57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
|
package web
|
||
|
|
||
|
import (
|
||
|
"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 {
|
||
|
if oldAge.After(*sessionData.lastAccess) {
|
||
|
pods.DestroyContainer(session)
|
||
|
invalidSessions = append(invalidSessions, sessionData.sessionID)
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|