terminate "completed" sessions (#477)

This commit is contained in:
Jim authored and GitHub committed 2020-10-01 10:13:57 -04:00
1 parent d851ab07d8
commit 19aecfefae
8 files changed
+416 -33

No files matched your search

@@ -147,6 +147,7 @@ func (c *Controller) Start() error {
c.startStatusTicking(c.baseContext)
c.startRecoveryNonceCleanupTicking(c.baseContext)
c.startTerminateCompletedSessionsTicking(c.baseContext)
c.started.Store(true)
return nil
+41 -1
View File
@@ -2,6 +2,7 @@ package controller
import (
"context"
"math/rand"
"time"
"github.com/hashicorp/boundary/internal/servers"
@@ -10,7 +11,8 @@ import (
// In the future we could make this configurable
const (
statusInterval = 10 * time.Second
statusInterval = 10 * time.Second
terminationInterval = 1 * time.Minute
)
// This is exported so it can be tweaked in tests
@@ -76,3 +78,41 @@ func (c *Controller) startRecoveryNonceCleanupTicking(cancelCtx context.Context)
}
}()
}
func (c *Controller) startTerminateCompletedSessionsTicking(cancelCtx context.Context) {
go func() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// desynchronize calls from the controllers, to ease the load on the DB.
getRandomInterval := func() time.Duration {
// 0 to 0.5 adjustment to the base
f := r.Float64() / 2
// Half a chance to be faster, not slower
if r.Float32() > 0.5 {
f = -1 * f
}
return terminationInterval + time.Duration(f*float64(time.Minute))
}
timer := time.NewTimer(0)
for {
select {
case <-cancelCtx.Done():
c.logger.Info("terminating completed sessions ticking shutting down")
return
case <-timer.C:
repo, err := c.SessionRepoFn()
if err != nil {
c.logger.Error("error fetching repository for terminating completed sessions", "error", err)
} else {
terminationCount, err := repo.TerminateCompletedSessions(cancelCtx)
if err != nil {
c.logger.Error("error performing termination of completed sessions", "error", err)
} else if terminationCount > 0 {
c.logger.Info("terminating completed sessions successful", "sessions_terminated", terminationCount)
}
}
timer.Reset(getRandomInterval())
}
}
}()
}