4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 //@require 'Swift/Events/SendEvent.php';
12 //@require 'Swift/Plugins/BandwidthMonitorPlugin.php';
13 //@require 'Swift/Plugins/Sleeper.php';
14 //@require 'Swift/Plugins/Timer.php';
17 * Throttles the rate at which emails are sent.
20 * @author Chris Corbyn
22 class Swift_Plugins_ThrottlerPlugin
23 extends Swift_Plugins_BandwidthMonitorPlugin
24 implements Swift_Plugins_Sleeper
, Swift_Plugins_Timer
27 /** Flag for throttling in bytes per minute */
28 const BYTES_PER_MINUTE
= 0x01;
30 /** Flag for throttling in emails per minute */
31 const MESSAGES_PER_MINUTE
= 0x10;
34 * The Sleeper instance for sleeping.
35 * @var Swift_Plugins_Sleeper
41 * The Timer instance which provides the timestamp.
42 * @var Swift_Plugins_Timer
48 * The time at which the first email was sent.
55 * The rate at which messages should be sent.
62 * The mode for throttling.
63 * This is {@link BYTES_PER_MINUTE} or {@link MESSAGES_PER_MINUTE}
70 * An internal counter of the number of messages sent.
74 private $_messages = 0;
77 * Create a new ThrottlerPlugin.
79 * @param int $mode, defaults to {@link BYTES_PER_MINUTE}
80 * @param Swift_Plugins_Sleeper $sleeper (only needed in testing)
81 * @param Swift_Plugins_Timer $timer (only needed in testing)
83 public function __construct($rate, $mode = self
::BYTES_PER_MINUTE
,
84 Swift_Plugins_Sleeper
$sleeper = null, Swift_Plugins_Timer
$timer = null)
88 $this->_sleeper
= $sleeper;
89 $this->_timer
= $timer;
93 * Invoked immediately before the Message is sent.
94 * @param Swift_Events_SendEvent $evt
96 public function beforeSendPerformed(Swift_Events_SendEvent
$evt)
98 $time = $this->getTimestamp();
99 if (!isset($this->_start
))
101 $this->_start
= $time;
103 $duration = $time - $this->_start
;
105 if (self
::BYTES_PER_MINUTE
== $this->_mode
)
107 $sleep = $this->_throttleBytesPerMinute($duration);
111 $sleep = $this->_throttleMessagesPerMinute($duration);
116 $this->sleep($sleep);
121 * Invoked when a Message is sent.
122 * @param Swift_Events_SendEvent $evt
124 public function sendPerformed(Swift_Events_SendEvent
$evt)
126 parent
::sendPerformed($evt);
131 * Sleep for $seconds.
132 * @param int $seconds
134 public function sleep($seconds)
136 if (isset($this->_sleeper
))
138 $this->_sleeper
->sleep($seconds);
147 * Get the current UNIX timestamp
150 public function getTimestamp()
152 if (isset($this->_timer
))
154 return $this->_timer
->getTimestamp();
162 // -- Private methods
165 * Get a number of seconds to sleep for.
166 * @param int $timePassed
170 private function _throttleBytesPerMinute($timePassed)
172 $expectedDuration = $this->getBytesOut() / ($this->_rate
/ 60);
173 return (int) ceil($expectedDuration - $timePassed);
177 * Get a number of seconds to sleep for.
178 * @param int $timePassed
182 private function _throttleMessagesPerMinute($timePassed)
184 $expectedDuration = $this->_messages
/ ($this->_rate
/ 60);
185 return (int) ceil($expectedDuration - $timePassed);