Improve session cleanup (#18123)
- We move logging statements to the upper layer. Store functions are low-level methods and should return error upwards rather than logging. - Used IN instead of any (array ()) which is equivalent. - Made the delay to be of type time.Duration and un-exported it. - Unexported the batch size constant. ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2711262729
Коммит
d1b164ab1f
@@ -12,12 +12,11 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
||||
"github.com/mattermost/mattermost-server/v6/store"
|
||||
)
|
||||
|
||||
const (
|
||||
SessionsCleanupDelayMilliseconds = 100
|
||||
sessionsCleanupDelay = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
type SqlSessionStore struct {
|
||||
@@ -275,12 +274,10 @@ func (me SqlSessionStore) AnalyticsSessionCount() (int64, error) {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (me SqlSessionStore) Cleanup(expiryTime int64, batchSize int64) {
|
||||
mlog.Debug("Cleaning up session store.")
|
||||
|
||||
func (me SqlSessionStore) Cleanup(expiryTime int64, batchSize int64) error {
|
||||
var query string
|
||||
if me.DriverName() == model.DatabaseDriverPostgres {
|
||||
query = "DELETE FROM Sessions WHERE Id = any (array (SELECT Id FROM Sessions WHERE ExpiresAt != 0 AND :ExpiresAt > ExpiresAt LIMIT :Limit))"
|
||||
query = "DELETE FROM Sessions WHERE Id IN (SELECT Id FROM Sessions WHERE ExpiresAt != 0 AND :ExpiresAt > ExpiresAt LIMIT :Limit)"
|
||||
} else {
|
||||
query = "DELETE FROM Sessions WHERE ExpiresAt != 0 AND :ExpiresAt > ExpiresAt LIMIT :Limit"
|
||||
}
|
||||
@@ -290,16 +287,16 @@ func (me SqlSessionStore) Cleanup(expiryTime int64, batchSize int64) {
|
||||
for rowsAffected > 0 {
|
||||
sqlResult, err := me.GetMaster().Exec(query, map[string]interface{}{"ExpiresAt": expiryTime, "Limit": batchSize})
|
||||
if err != nil {
|
||||
mlog.Error("Unable to cleanup session store.", mlog.Err(err))
|
||||
return
|
||||
return errors.Wrap(err, "unable to delete sessions")
|
||||
}
|
||||
var rowErr error
|
||||
rowsAffected, rowErr = sqlResult.RowsAffected()
|
||||
if rowErr != nil {
|
||||
mlog.Error("Unable to cleanup session store.", mlog.Err(err))
|
||||
return
|
||||
return errors.Wrap(err, "unable to delete sessions")
|
||||
}
|
||||
|
||||
time.Sleep(SessionsCleanupDelayMilliseconds * time.Millisecond)
|
||||
time.Sleep(sessionsCleanupDelay)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user