pixelfederation / doctrine-resettable-em-bundle

使用可重置装饰器装饰默认实体管理器的 Symfony 扩展包。

8.1.0 2024-01-03 11:45 UTC

README

此扩展包适用于那些在每次请求中不初始化 Symfony,而是保持在内存中,并处理多个连续请求的负载(例如,当使用Swoole 扩展包时)。另一个用例是消息队列消费(例如,Symfony 消息传递),在此场景中,在处理消息后需要清除(并可能重置)实体管理器。

此扩展包的最佳功能是,它将所有配置的实体管理器包装在一个 ResettableEntityManager 实例中,该实例能够在遇到异常时重置实体管理器。在每个请求之后,如果请求处理期间发生异常,则清除或重置实体管理器。

另一个特性是在每个请求开始时,都会ping实体管理器连接,这样连接就不会在一段时间后关闭。

安装

composer require pixelfederation/doctrine-resettable-em-bundle

设置

// config/bundles.php
return [
    //...
    \PixelFederation\DoctrineResettableEmBundle\PixelFederationDoctrineResettableEmBundle::class => ['all' => true]
    //...
];
pixel_federation_doctrine_resettable_em:
  exclude_from_processing:
    # these entity managers won't be wrapped by the resettable entity manager:
    entity_managers:
        - readonly
    # these dbal connections won't be assigned to the keep alive handler
    dbal:
      - readonly
    # these redis cluster connections won't be assigned to the keep alive handler
    redis_cluster:
        - default
  # default 0 - if set, the connection ping operation will be executed each X seconds 
  # (instead of at the beginning of each request) 
  ping_interval: 10 
  # default false - if set to true, the app will check if there is an active transaction
  # in the processed connection, and it will rollback the transaction
  check_active_transactions: true
  # for reader writer connections, each has to be defined as 'reader' or 'writer' to be able for the symfony
  # app to reconnect after db failover. currently only AWS Aurora is supported.
  failover_connections:  
    default: writer
  # redis clusters which need to be failed over should be registered here
  # it's really important to set default timeouts to a low value, e.g. 2 seconds, so the app won't block for too long
  redis_cluster_connections:
    default: 'RedisCluster' # connection name (can be literally anything) => redis cluster service id

从 v5 版本迁移到 v6 版本

将所有对 PixelFederation\DoctrineResettableEmBundle\RequestCycle\InitializerInterface 的使用更改为 PixelFederation\DoctrineResettableEmBundle\RequestCycle\Initializer