Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
feat(session): Add in-memory syfmony session storage (#73)
Browse files Browse the repository at this point in the history
Container `Swoole/Table` is based on shared memory and its safe for concurrent read/write. Class `SwooleSessionStorage` allow using sessions when sever is ran as single instance (session data is stored in shared memory)

See `tests/Fixtures/Symfony/app/config/session/swoole.yaml` for example configuration.
  • Loading branch information
k911 authored Oct 31, 2019
1 parent 3436fd9 commit 4ccdca0
Show file tree
Hide file tree
Showing 19 changed files with 974 additions and 63 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}],
"require": {
"php": "^7.3",
"ext-json": "*",
"ext-swoole": "~4.4.7",
"beberlei/assert": "^3.0",
"symfony/config": "^4.3.1",
Expand Down Expand Up @@ -114,6 +115,7 @@
}
},
"config": {
"process-timeout": 600,
"sort-packages": true,
"platform": {
"php": "7.3.10",
Expand Down
3 changes: 2 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('entity_manager_handler')
->defaultNull()
->end()
->booleanNode('session_cookie_event_listener')
->defaultFalse()
->treatNullLike(false)
->end()
->end()
->end() // drivers
->arrayNode('settings')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use K911\Swoole\Bridge\Doctrine\ORM\EntityManagerHandler;
use K911\Swoole\Bridge\Symfony\HttpFoundation\CloudFrontRequestFactory;
use K911\Swoole\Bridge\Symfony\HttpFoundation\RequestFactoryInterface;
use K911\Swoole\Bridge\Symfony\HttpFoundation\Session\SetSessionCookieEventListener;
use K911\Swoole\Bridge\Symfony\HttpFoundation\TrustAllProxiesRequestHandler;
use K911\Swoole\Bridge\Symfony\HttpKernel\DebugHttpKernelRequestHandler;
use K911\Swoole\Bridge\Symfony\Messenger\SwooleServerTaskTransportFactory;
Expand Down Expand Up @@ -253,6 +254,14 @@ private function registerHttpServerServices(array $config, ContainerBuilder $con
->setDecoratedService(RequestHandlerInterface::class, null, -50)
;
}

if ($config['session_cookie_event_listener']) {
$container->register(SetSessionCookieEventListener::class)
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
;
}
}

private function isBundleLoaded(ContainerBuilder $container, string $bundleName): bool
Expand Down
16 changes: 8 additions & 8 deletions src/Bridge/Symfony/Bundle/Resources/config/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ services:
class: K911\Swoole\Server\Configurator\CallableChainConfigurator
factory: 'K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory:make'
arguments:
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_request_handler'
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_request_handler'
autoconfigure: false

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerStartCommand':
Expand All @@ -30,9 +30,9 @@ services:
class: K911\Swoole\Server\Configurator\CallableChainConfigurator
factory: 'K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory:make'
arguments:
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_request_handler'
- '@swoole_bundle.server.http_server.configurator.with_sigint_handler'
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_request_handler'
- '@swoole_bundle.server.http_server.configurator.with_sigint_handler'
autoconfigure: false

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerRunCommand':
Expand All @@ -44,9 +44,9 @@ services:
class: K911\Swoole\Server\Configurator\CallableChainConfigurator
factory: 'K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory:make'
arguments:
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_limited_request_handler'
- '@swoole_bundle.server.http_server.configurator.with_sigint_handler'
- '@swoole_bundle.server.http_server.configurator_collection'
- '@swoole_bundle.server.http_server.configurator.with_limited_request_handler'
- '@swoole_bundle.server.http_server.configurator.with_sigint_handler'
autoconfigure: false

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerProfileCommand':
Expand Down
107 changes: 59 additions & 48 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,130 @@ services:
autoconfigure: true
public: false

'K911\Swoole\Component\AtomicCounter':
K911\Swoole\Component\AtomicCounter:
factory: ['K911\Swoole\Component\AtomicCounter', fromZero]

'K911\Swoole\Bridge\Symfony\HttpFoundation\SetRequestRuntimeConfiguration':
K911\Swoole\Bridge\Symfony\HttpFoundation\SetRequestRuntimeConfiguration:

'K911\Swoole\Bridge\Symfony\HttpFoundation\RequestFactoryInterface':
K911\Swoole\Bridge\Symfony\HttpFoundation\RequestFactoryInterface:
class: K911\Swoole\Bridge\Symfony\HttpFoundation\RequestFactory

'K911\Swoole\Bridge\Symfony\HttpFoundation\ResponseProcessorInterface':
K911\Swoole\Bridge\Symfony\HttpFoundation\ResponseProcessorInterface:
class: K911\Swoole\Bridge\Symfony\HttpFoundation\ResponseProcessor

'K911\Swoole\Server\RequestHandler\RequestHandlerInterface':
K911\Swoole\Server\RequestHandler\RequestHandlerInterface:
alias: K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler

'K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler':
K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler:

'K911\Swoole\Server\RequestHandler\LimitedRequestHandler':
K911\Swoole\Server\RequestHandler\LimitedRequestHandler:

'K911\Swoole\Server\LifecycleHandler\SigIntHandler':
K911\Swoole\Server\LifecycleHandler\SigIntHandler:

'K911\Swoole\Server\Runtime\CallableBootManagerFactory':
K911\Swoole\Server\Runtime\CallableBootManagerFactory:

'K911\Swoole\Server\Runtime\BootableInterface':
class: 'K911\Swoole\Server\Runtime\CallableBootManager'
factory: 'K911\Swoole\Server\Runtime\CallableBootManagerFactory:make'
K911\Swoole\Server\Session\SwooleTableStorage:
factory: ['K911\Swoole\Server\Session\SwooleTableStorage', 'fromDefaults']

K911\Swoole\Server\Session\StorageInterface:
alias: K911\Swoole\Server\Session\SwooleTableStorage

K911\Swoole\Bridge\Symfony\HttpFoundation\Session\SwooleSessionStorage:

K911\Swoole\Server\Runtime\BootableInterface:
class: K911\Swoole\Server\Runtime\CallableBootManager
factory: K911\Swoole\Server\Runtime\CallableBootManagerFactory:make
arguments: [!tagged 'swoole_bundle.bootable_service']
autoconfigure: false

'K911\Swoole\Server\HttpServer':
K911\Swoole\Server\HttpServer:

'K911\Swoole\Server\WorkerHandler\WorkerStartHandlerInterface':
K911\Swoole\Server\WorkerHandler\WorkerStartHandlerInterface:
class: K911\Swoole\Server\WorkerHandler\NoOpWorkerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerStartHandlerInterface':
K911\Swoole\Server\LifecycleHandler\ServerStartHandlerInterface:
class: K911\Swoole\Server\LifecycleHandler\NoOpServerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerShutdownHandlerInterface':
K911\Swoole\Server\LifecycleHandler\ServerShutdownHandlerInterface:
class: K911\Swoole\Server\LifecycleHandler\NoOpServerShutdownHandler

'K911\Swoole\Server\LifecycleHandler\ServerManagerStartHandlerInterface':
K911\Swoole\Server\LifecycleHandler\ServerManagerStartHandlerInterface:
class: K911\Swoole\Server\LifecycleHandler\NoOpServerManagerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerManagerStopHandlerInterface':
K911\Swoole\Server\LifecycleHandler\ServerManagerStopHandlerInterface:
class: K911\Swoole\Server\LifecycleHandler\NoOpServerManagerStopHandler

'K911\Swoole\Server\TaskHandler\TaskHandlerInterface':
K911\Swoole\Server\TaskHandler\TaskHandlerInterface:
class: K911\Swoole\Server\TaskHandler\NoOpTaskHandler

'K911\Swoole\Server\TaskHandler\TaskFinishedHandlerInterface':
K911\Swoole\Server\TaskHandler\TaskFinishedHandlerInterface:
class: K911\Swoole\Server\TaskHandler\NoOpTaskFinishedHandler

'K911\Swoole\Server\Api\ApiServerClientFactory':
K911\Swoole\Server\Api\ApiServerClientFactory:

'K911\Swoole\Server\Api\ApiServerClient':
factory: 'K911\Swoole\Server\Api\ApiServerClientFactory:newClient'
K911\Swoole\Server\Api\ApiServerClient:
factory: K911\Swoole\Server\Api\ApiServerClientFactory:newClient

'K911\Swoole\Server\Api\ApiServerInterface':
K911\Swoole\Server\Api\ApiServerInterface:
class: K911\Swoole\Server\Api\ApiServer

# Could be helpful for projects that uses/have included proxy-manager
# lazy: true
# tags:
# - { name: proxy, interface: K911\Swoole\Server\Api\ApiServerInterface }
# Could be helpful for projects that uses/have included proxy-manager
# lazy: true
# tags:
# - { name: proxy, interface: K911\Swoole\Server\Api\ApiServerInterface }

'K911\Swoole\Server\Config\Sockets':
K911\Swoole\Server\Config\Sockets:

'K911\Swoole\Server\HttpServerConfiguration':
K911\Swoole\Server\HttpServerConfiguration:

'K911\Swoole\Server\Configurator\WithHttpServerConfiguration':
K911\Swoole\Server\Configurator\WithHttpServerConfiguration:

'K911\Swoole\Server\Configurator\WithServerShutdownHandler':
K911\Swoole\Server\Configurator\WithServerShutdownHandler:

'K911\Swoole\Server\Configurator\WithServerStartHandler':
K911\Swoole\Server\Configurator\WithServerStartHandler:

'K911\Swoole\Server\Configurator\WithServerManagerStartHandler':
K911\Swoole\Server\Configurator\WithServerManagerStartHandler:

'K911\Swoole\Server\Configurator\WithServerManagerStopHandler':
K911\Swoole\Server\Configurator\WithServerManagerStopHandler:

'K911\Swoole\Server\Configurator\WithWorkerStartHandler':
K911\Swoole\Server\Configurator\WithWorkerStartHandler:

'K911\Swoole\Server\Configurator\WithTaskHandler':
K911\Swoole\Server\Configurator\WithTaskHandler:

'K911\Swoole\Server\Configurator\WithTaskFinishedHandler':
K911\Swoole\Server\Configurator\WithTaskFinishedHandler:

'K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory':
K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory:

'K911\Swoole\Server\Api\WithApiServerConfiguration':
K911\Swoole\Server\Api\WithApiServerConfiguration:
arguments:
$requestHandler: '@swoole_bundle.server.api_server.request_handler'

'K911\Swoole\Server\Api\ApiServerRequestHandler':
K911\Swoole\Server\Api\ApiServerRequestHandler:

'swoole_bundle.server.api_server.request_handler':
swoole_bundle.server.api_server.request_handler:
alias: K911\Swoole\Server\Api\ApiServerRequestHandler

'swoole_bundle.server.http_server.configurator_collection':
swoole_bundle.server.http_server.configurator_collection:
class: K911\Swoole\Component\GeneratedCollection
arguments: [!tagged 'swoole_bundle.server_configurator']

'K911\Swoole\Server\Configurator\ConfiguratorInterface':
alias: 'swoole_bundle.server.http_server.configurator'
K911\Swoole\Server\Configurator\ConfiguratorInterface:
alias: swoole_bundle.server.http_server.configurator

swoole_bundle.session.table_storage:
alias: K911\Swoole\Bridge\Symfony\HttpFoundation\Session\SwooleSessionStorage

'swoole_bundle.server.http_server.configurator.with_request_handler':
swoole_bundle.server.http_server.configurator.with_request_handler:
class: K911\Swoole\Server\Configurator\WithRequestHandler
autoconfigure: false

'swoole_bundle.server.http_server.configurator.with_limited_request_handler':
swoole_bundle.server.http_server.configurator.with_limited_request_handler:
class: K911\Swoole\Server\Configurator\WithRequestHandler
autoconfigure: false
arguments:
$requestHandler: '@K911\Swoole\Server\RequestHandler\LimitedRequestHandler'

'swoole_bundle.server.http_server.configurator.with_sigint_handler':
swoole_bundle.server.http_server.configurator.with_sigint_handler:
class: K911\Swoole\Server\Configurator\WithServerStartHandler
autoconfigure: false
arguments:
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function process(HttpFoundationResponse $httpFoundationResponse, SwooleRe
$cookie->getPath(),
$cookie->getDomain() ?? '',
$cookie->isSecure(),
$cookie->isHttpOnly()
$cookie->isHttpOnly(),
$cookie->getSameSite() ?? ''
);
}

Expand Down
Loading

0 comments on commit 4ccdca0

Please sign in to comment.