Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ func (p *Persister) CleanupDatabase(ctx context.Context, wait time.Duration, old
}
time.Sleep(wait)

p.r.Logger().Println("Cleaning up inactive sessions")
if err := p.DeleteInactiveSessions(ctx, currentTime, batchSize); err != nil {
return err
}
time.Sleep(wait)

p.r.Logger().Println("Cleaning up expired continuity containers")
if err := p.DeleteExpiredContinuitySessions(ctx, currentTime, batchSize); err != nil {
return err
Expand Down
21 changes: 20 additions & 1 deletion persistence/sql/persister_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (p *Persister) DeleteExpiredSessions(ctx context.Context, expiresAt time.Ti

//#nosec G201 -- TableName is static
err = p.GetConnection(ctx).RawQuery(fmt.Sprintf(
"DELETE FROM %s WHERE id in (SELECT id FROM (SELECT id FROM %s c WHERE (expires_at <= ? or active = false) and nid = ? ORDER BY expires_at ASC LIMIT %d ) AS s )",
"DELETE FROM %s WHERE id in (SELECT id FROM (SELECT id FROM %s c WHERE expires_at <= ? and nid = ? ORDER BY expires_at ASC LIMIT %d ) AS s )",
new(session.Session).TableName(ctx),
new(session.Session).TableName(ctx),
limit,
Expand All @@ -516,3 +516,22 @@ func (p *Persister) DeleteExpiredSessions(ctx context.Context, expiresAt time.Ti
}
return nil
}

func (p *Persister) DeleteInactiveSessions(ctx context.Context, expiresAt time.Time, limit int) (err error) {
ctx, span := p.r.Tracer(ctx).Tracer().Start(ctx, "persistence.sql.DeleteInactiveSessions")
defer otelx.End(span, &err)

//#nosec G201 -- TableName is static
err = p.GetConnection(ctx).RawQuery(fmt.Sprintf(
"DELETE FROM %s WHERE id in (SELECT id FROM (SELECT id FROM %s c WHERE active = false and nid = ? ORDER BY expires_at ASC LIMIT %d ) AS s )",
new(session.Session).TableName(ctx),
new(session.Session).TableName(ctx),
limit,
),
p.NetworkID(ctx),
).Exec()
if err != nil {
return sqlcon.HandleError(err)
}
return nil
}
3 changes: 3 additions & 0 deletions session/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Persister interface {
// DeleteExpiredSessions deletes sessions that expired before the given time.
DeleteExpiredSessions(context.Context, time.Time, int) error

// DeleteInactiveSessions deletes sessions that have been marked inactive.
DeleteInactiveSessions(context.Context, time.Time, int) error

// DeleteSessionByToken deletes a session associated with the given token.
//
// Functionality is similar to DeleteSession but accepts a session token
Expand Down
Loading