From 07d82824813170c2d482a28849ac596f703dba44 Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Mon, 4 May 2026 17:44:53 +0200 Subject: [PATCH 1/4] Add a session handler to migrate from pdo to redis, add managed gc times - symfony uses the php default garbage collector probabilities, but that is set to 0 in debian and ubuntu, so manage it ourselves so the sessions get cleared out - add a service definition for redis, which the redis session handler needs, this includes splitting up the dsn in our .env file --- .env.example | 4 +++- .env.example_docker | 6 ++++-- compose.yaml | 2 +- config/packages/framework.yaml | 11 ++++++++++- config/services.yaml | 15 +++++++++++++++ src/MigratePdoToRedisSessionHandler.php | 15 +++++++++++++++ 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/MigratePdoToRedisSessionHandler.php diff --git a/.env.example b/.env.example index e3106cc35d..8dca2111c5 100644 --- a/.env.example +++ b/.env.example @@ -89,7 +89,9 @@ TRUSTED_PROXIES= # Redis REDIS_PASSWORD=!ChangeThisRedisPass! -REDIS_DNS=redis://${REDIS_PASSWORD}@127.0.0.1:6379 +REDIS_HOST=127.0.0.1 +REDIS_PORT=6379 +REDIS_DNS=redis://${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT} # Or when using Redis Socket: #REDIS_DNS=redis://${REDIS_PASSWORD}/var/run/redis/redis-server.sock # Or socket without password: diff --git a/.env.example_docker b/.env.example_docker index 5b8b921987..0bb432a041 100644 --- a/.env.example_docker +++ b/.env.example_docker @@ -93,8 +93,10 @@ HCAPTCHA_SECRET= TRUSTED_PROXIES= # Valkey -VALKEY_PASSWORD=!ChangeThisValkeyPass! -REDIS_DNS=redis://${VALKEY_PASSWORD}@valkey:6379 +REDIS_PASSWORD=!ChangeThisValkeyPass! +REDIS_HOST=valkey +REDIS_PORT=6379 +REDIS_DNS="redis://${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}" # S3 storage (optional) S3_KEY= diff --git a/compose.yaml b/compose.yaml index b0f698134e..ec999a8eeb 100644 --- a/compose.yaml +++ b/compose.yaml @@ -86,7 +86,7 @@ services: image: valkey/valkey:trixie restart: unless-stopped user: ${MBIN_USER} - command: valkey-server /valkey.conf --requirepass ${VALKEY_PASSWORD} + command: valkey-server /valkey.conf --requirepass ${REDIS_PASSWORD} healthcheck: test: ['CMD', 'valkey-cli', 'ping'] volumes: diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 9800402700..147238c5dd 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -24,11 +24,20 @@ framework: # We set our cookie session lifetime to the same value as remember_me token. # More info: https://symfony.com/doc/current/session.html#session-idle-time-keep-alive session: - handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler + # previous + #handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler + # transition + handler_id: App\MigratePdoToRedisSessionHandler + # new + #handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler cookie_secure: auto cookie_samesite: lax cookie_lifetime: 10512000 # 4 months long lifetime storage_factory_id: session.storage.factory.native + # see: https://symfony.com/doc/7.4/session.html#configuring-garbage-collection + # debian sets this value to 0, so since we cannot control that, make the garbage collection a 1/1000 chance + gc_probability: 1 + gc_divisor: 1000 http_client: default_options: diff --git a/config/services.yaml b/config/services.yaml index 7dd6606278..4ca91324ca 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -289,3 +289,18 @@ services: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler: arguments: - '%env(DATABASE_URL)%' + + Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: + arguments: + - '@Redis' + + Redis: + class: Redis + calls: + - connect: + - '%env(REDIS_HOST)%' + - '%env(int:REDIS_PORT)%' + + # uncomment the following if your Redis server requires a password + - auth: + - '%env(REDIS_PASSWORD)%' diff --git a/src/MigratePdoToRedisSessionHandler.php b/src/MigratePdoToRedisSessionHandler.php new file mode 100644 index 0000000000..298112f2fb --- /dev/null +++ b/src/MigratePdoToRedisSessionHandler.php @@ -0,0 +1,15 @@ + Date: Mon, 4 May 2026 17:55:34 +0200 Subject: [PATCH 2/4] Fix linter --- src/MigratePdoToRedisSessionHandler.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/MigratePdoToRedisSessionHandler.php b/src/MigratePdoToRedisSessionHandler.php index 298112f2fb..ddb5b9ae80 100644 --- a/src/MigratePdoToRedisSessionHandler.php +++ b/src/MigratePdoToRedisSessionHandler.php @@ -1,5 +1,7 @@ Date: Sat, 11 Jul 2026 12:42:43 +0200 Subject: [PATCH 3/4] Add changes to the devcontainer .env file --- .devcontainer/.env.devcontainer | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.devcontainer/.env.devcontainer b/.devcontainer/.env.devcontainer index 4cc01ba158..0eac7d10f9 100644 --- a/.devcontainer/.env.devcontainer +++ b/.devcontainer/.env.devcontainer @@ -66,7 +66,9 @@ TRUSTED_PROXIES=::1,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16 # Redis REDIS_PASSWORD= -REDIS_DNS=redis://127.0.0.1:6379 +REDIS_HOST=127.0.0.1 +REDIS_PORT=6379 +REDIS_DNS=redis://${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT} # S3 storage (optional) S3_KEY= From 79ecde2ad8a16df286fbdfcceb95a2891b77475a Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Sat, 11 Jul 2026 12:43:13 +0200 Subject: [PATCH 4/4] Improve comment in the services.yaml --- config/services.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 4ca91324ca..31feac7341 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -302,5 +302,5 @@ services: - '%env(int:REDIS_PORT)%' # uncomment the following if your Redis server requires a password - - auth: - - '%env(REDIS_PASSWORD)%' + #- auth: + # - '%env(REDIS_PASSWORD)%'