Tasks
4 days ago
SendingErrorHandler.php
2 years ago
SendingQueue.php
3 weeks ago
SendingThrottlingHandler.php
3 months ago
index.php
3 years ago
SendingThrottlingHandler.php
90 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers\SendingQueue; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Logging\LoggerFactory; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use MailPoetVendor\Monolog\Logger; |
| 12 | |
| 13 | class SendingThrottlingHandler { |
| 14 | public const BATCH_SIZE = 20; |
| 15 | public const SETTINGS_KEY = 'mta_throttling'; |
| 16 | public const SUCCESS_THRESHOLD_TO_INCREASE = 10; |
| 17 | |
| 18 | /** @var Logger */ |
| 19 | private $logger; |
| 20 | |
| 21 | /** @var SettingsController */ |
| 22 | private $settings; |
| 23 | |
| 24 | /** @var WPFunctions */ |
| 25 | private $wp; |
| 26 | |
| 27 | public function __construct( |
| 28 | LoggerFactory $loggerFactory, |
| 29 | SettingsController $settings, |
| 30 | WPFunctions $wp |
| 31 | ) { |
| 32 | $this->logger = $loggerFactory->getLogger(LoggerFactory::TOPIC_SENDING); |
| 33 | $this->settings = $settings; |
| 34 | $this->wp = $wp; |
| 35 | } |
| 36 | |
| 37 | public function getBatchSize(): int { |
| 38 | $throttlingSettings = $this->loadSettings(); |
| 39 | if (isset($throttlingSettings['batch_size'])) { |
| 40 | return $throttlingSettings['batch_size']; |
| 41 | } |
| 42 | return $this->getMaxBatchSize(); |
| 43 | } |
| 44 | |
| 45 | private function getMaxBatchSize(): int { |
| 46 | $batchSize = $this->wp->applyFilters('mailpoet_cron_worker_sending_queue_batch_size', self::BATCH_SIZE); |
| 47 | return is_int($batchSize) ? $batchSize : self::BATCH_SIZE; |
| 48 | } |
| 49 | |
| 50 | public function throttleBatchSize(): int { |
| 51 | $batchSize = $this->getBatchSize(); |
| 52 | if ($batchSize > 1) { |
| 53 | $batchSize = (int)ceil($this->getBatchSize() / 2); |
| 54 | $throttlingSettings = $this->loadSettings(); |
| 55 | $throttlingSettings['batch_size'] = $batchSize; |
| 56 | unset($throttlingSettings['success_count']); |
| 57 | $this->logger->error("MailPoet throttling: decrease batch_size to: {$batchSize}"); |
| 58 | $this->saveSettings($throttlingSettings); |
| 59 | } |
| 60 | |
| 61 | return $batchSize; |
| 62 | } |
| 63 | |
| 64 | public function processSuccess(): void { |
| 65 | $throttlingSettings = $this->loadSettings(); |
| 66 | if (!isset($throttlingSettings['batch_size'])) { |
| 67 | return; |
| 68 | } |
| 69 | $throttlingSettings['success_count'] = isset($throttlingSettings['success_count']) ? ++$throttlingSettings['success_count'] : 1; |
| 70 | $this->logger->info("MailPoet throttling: increase success_count to: {$throttlingSettings['success_count']}"); |
| 71 | if ($throttlingSettings['success_count'] >= self::SUCCESS_THRESHOLD_TO_INCREASE) { |
| 72 | unset($throttlingSettings['success_count']); |
| 73 | $throttlingSettings['batch_size'] = min($this->getMaxBatchSize(), $throttlingSettings['batch_size'] * 2); |
| 74 | $this->logger->info("MailPoet throttling: increase batch_size to: {$throttlingSettings['batch_size']}"); |
| 75 | if ($this->getMaxBatchSize() === $throttlingSettings['batch_size']) { |
| 76 | unset($throttlingSettings['batch_size']); |
| 77 | } |
| 78 | } |
| 79 | $this->saveSettings($throttlingSettings); |
| 80 | } |
| 81 | |
| 82 | private function loadSettings(): ?array { |
| 83 | return $this->settings->get(self::SETTINGS_KEY); |
| 84 | } |
| 85 | |
| 86 | private function saveSettings(array $settings): void { |
| 87 | $this->settings->set(self::SETTINGS_KEY, $settings); |
| 88 | } |
| 89 | } |
| 90 |