fix(auth): efficient user session projection (#7187)

* fix(auth): cache users during session projection

* fix(auth.user_sessions): add index for more efficient by user search
This commit is contained in:
Silvan
2024-01-09 19:36:46 +01:00
committed by GitHub
parent 039a1e793b
commit 43f1d59649
6 changed files with 142 additions and 4 deletions

26
cmd/setup/20.go Normal file
View File

@@ -0,0 +1,26 @@
package setup
import (
"context"
_ "embed"
"github.com/zitadel/zitadel/internal/database"
)
var (
//go:embed 20.sql
addByUserIndexToSession string
)
type AddByUserIndexToSession struct {
dbClient *database.DB
}
func (mig *AddByUserIndexToSession) Execute(ctx context.Context) error {
_, err := mig.dbClient.ExecContext(ctx, addByUserIndexToSession)
return err
}
func (mig *AddByUserIndexToSession) String() string {
return "20_add_by_user_index_on_session"
}

1
cmd/setup/20.sql Normal file
View File

@@ -0,0 +1 @@
CREATE INDEX CONCURRENTLY IF NOT EXISTS user_sessions_by_user ON auth.user_sessions (instance_id, user_id);

View File

@@ -77,6 +77,7 @@ type Steps struct {
s17AddOffsetToUniqueConstraints *AddOffsetToCurrentStates
s18AddLowerFieldsToLoginNames *AddLowerFieldsToLoginNames
s19AddCurrentStatesIndex *AddCurrentSequencesIndex
s20AddByUserSessionIndex *AddByUserIndexToSession
}
type encryptionKeyConfig struct {

View File

@@ -110,6 +110,7 @@ func Setup(config *Config, steps *Steps, masterKey string) {
steps.s17AddOffsetToUniqueConstraints = &AddOffsetToCurrentStates{dbClient: queryDBClient}
steps.s18AddLowerFieldsToLoginNames = &AddLowerFieldsToLoginNames{dbClient: queryDBClient}
steps.s19AddCurrentStatesIndex = &AddCurrentSequencesIndex{dbClient: queryDBClient}
steps.s20AddByUserSessionIndex = &AddByUserIndexToSession{dbClient: queryDBClient}
err = projection.Create(ctx, projectionDBClient, eventstoreClient, config.Projections, nil, nil, nil)
logging.OnError(err).Fatal("unable to start projections")
@@ -156,6 +157,8 @@ func Setup(config *Config, steps *Steps, masterKey string) {
logging.WithFields("name", steps.s17AddOffsetToUniqueConstraints.String()).OnError(err).Fatal("migration failed")
err = migration.Migrate(ctx, eventstoreClient, steps.s19AddCurrentStatesIndex)
logging.WithFields("name", steps.s19AddCurrentStatesIndex.String()).OnError(err).Fatal("migration failed")
err = migration.Migrate(ctx, eventstoreClient, steps.s20AddByUserSessionIndex)
logging.WithFields("name", steps.s20AddByUserSessionIndex.String()).OnError(err).Fatal("migration failed")
for _, repeatableStep := range repeatableSteps {
err = migration.Migrate(ctx, eventstoreClient, repeatableStep)