Skip to content

Commit

Permalink
Setup Job To Cleanup Empty Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanDerr committed Sep 27, 2024
1 parent 7d3fbf3 commit 3e4d978
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion internal/daemon/worker/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"time"

Expand Down Expand Up @@ -50,7 +51,7 @@ func (w *Worker) startStatusTicking(cancelCtx context.Context, sessionManager se
}
return common.StatusInterval + time.Duration(f*float64(time.Second))
}

w.startBackgroundCleanup(1*time.Minute, sessionManager)
timer := time.NewTimer(0)
for {
select {
Expand Down Expand Up @@ -516,3 +517,36 @@ func (w *Worker) isPastGrace() (bool, time.Time, time.Duration) {
v := time.Since(t)
return v > u, t, u
}

func (w *Worker) cleanupPendingSessions(sessionManager session.Manager) {
const op = "worker.(Worker).cleanupPendingSessions"
closeInfo := make(map[string]*session.ConnectionCloseData)
cleanSessionIds := make([]string, 0)

sessionManager.ForEachLocalSession(func(s session.Session) bool {
if len(s.GetLocalConnections()) == 0 {
cleanSessionIds = append(cleanSessionIds, s.GetId())
event.WriteSysEvent(context.Background(), op, "terminated session due to no active channels", "session_id", s.GetId())
}
return true
})

if len(closeInfo) > 0 {
_ = sessionManager.RequestCloseConnections(context.Background(), closeInfo)
}
sessionManager.DeleteLocalSession(cleanSessionIds)
}

func (w *Worker) startBackgroundCleanup(interval time.Duration, sessionManager session.Manager) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Println("Cleaning up pending sessions")
w.cleanupPendingSessions(sessionManager)
}
}
}()
}

0 comments on commit 3e4d978

Please sign in to comment.