ThrottlerPlugin.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Throttles the rate at which emails are sent.
  11. *
  12. * @package Swift
  13. * @subpackage Plugins
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Plugins_ThrottlerPlugin extends Swift_Plugins_BandwidthMonitorPlugin implements Swift_Plugins_Sleeper, Swift_Plugins_Timer
  17. {
  18. /** Flag for throttling in bytes per minute */
  19. const BYTES_PER_MINUTE = 0x01;
  20. /** Flag for throttling in emails per second (Amazon SES) */
  21. const MESSAGES_PER_SECOND = 0x11;
  22. /** Flag for throttling in emails per minute */
  23. const MESSAGES_PER_MINUTE = 0x10;
  24. /**
  25. * The Sleeper instance for sleeping.
  26. *
  27. * @var Swift_Plugins_Sleeper
  28. */
  29. private $_sleeper;
  30. /**
  31. * The Timer instance which provides the timestamp.
  32. *
  33. * @var Swift_Plugins_Timer
  34. */
  35. private $_timer;
  36. /**
  37. * The time at which the first email was sent.
  38. *
  39. * @var int
  40. */
  41. private $_start;
  42. /**
  43. * The rate at which messages should be sent.
  44. *
  45. * @var int
  46. */
  47. private $_rate;
  48. /**
  49. * The mode for throttling.
  50. *
  51. * This is {@link BYTES_PER_MINUTE} or {@link MESSAGES_PER_MINUTE}
  52. *
  53. * @var int
  54. */
  55. private $_mode;
  56. /**
  57. * An internal counter of the number of messages sent.
  58. *
  59. * @var int
  60. */
  61. private $_messages = 0;
  62. /**
  63. * Create a new ThrottlerPlugin.
  64. *
  65. * @param integer $rate
  66. * @param integer $mode, defaults to {@link BYTES_PER_MINUTE}
  67. * @param Swift_Plugins_Sleeper $sleeper (only needed in testing)
  68. * @param Swift_Plugins_Timer $timer (only needed in testing)
  69. */
  70. public function __construct($rate, $mode = self::BYTES_PER_MINUTE, Swift_Plugins_Sleeper $sleeper = null, Swift_Plugins_Timer $timer = null)
  71. {
  72. $this->_rate = $rate;
  73. $this->_mode = $mode;
  74. $this->_sleeper = $sleeper;
  75. $this->_timer = $timer;
  76. }
  77. /**
  78. * Invoked immediately before the Message is sent.
  79. *
  80. * @param Swift_Events_SendEvent $evt
  81. */
  82. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  83. {
  84. $time = $this->getTimestamp();
  85. if (!isset($this->_start)) {
  86. $this->_start = $time;
  87. }
  88. $duration = $time - $this->_start;
  89. switch($this->_mode) {
  90. case self::BYTES_PER_MINUTE :
  91. $sleep = $this->_throttleBytesPerMinute($duration);
  92. break;
  93. case self::MESSAGES_PER_SECOND :
  94. $sleep = $this->_throttleMessagesPerSecond($duration);
  95. break;
  96. case self::MESSAGES_PER_MINUTE :
  97. $sleep = $this->_throttleMessagesPerMinute($duration);
  98. break;
  99. default :
  100. $sleep = 0;
  101. break;
  102. }
  103. if ($sleep > 0) {
  104. $this->sleep($sleep);
  105. }
  106. }
  107. /**
  108. * Invoked when a Message is sent.
  109. *
  110. * @param Swift_Events_SendEvent $evt
  111. */
  112. public function sendPerformed(Swift_Events_SendEvent $evt)
  113. {
  114. parent::sendPerformed($evt);
  115. ++$this->_messages;
  116. }
  117. /**
  118. * Sleep for $seconds.
  119. *
  120. * @param integer $seconds
  121. */
  122. public function sleep($seconds)
  123. {
  124. if (isset($this->_sleeper)) {
  125. $this->_sleeper->sleep($seconds);
  126. } else {
  127. sleep($seconds);
  128. }
  129. }
  130. /**
  131. * Get the current UNIX timestamp.
  132. *
  133. * @return int
  134. */
  135. public function getTimestamp()
  136. {
  137. if (isset($this->_timer)) {
  138. return $this->_timer->getTimestamp();
  139. } else {
  140. return time();
  141. }
  142. }
  143. // -- Private methods
  144. /**
  145. * Get a number of seconds to sleep for.
  146. *
  147. * @param integer $timePassed
  148. *
  149. * @return int
  150. */
  151. private function _throttleBytesPerMinute($timePassed)
  152. {
  153. $expectedDuration = $this->getBytesOut() / ($this->_rate / 60);
  154. return (int) ceil($expectedDuration - $timePassed);
  155. }
  156. /**
  157. * Get a number of seconds to sleep for.
  158. *
  159. * @param int $timePassed
  160. *
  161. * @return int
  162. */
  163. private function _throttleMessagesPerSecond($timePassed)
  164. {
  165. $expectedDuration = $this->_messages / ($this->_rate);
  166. return (int) ceil($expectedDuration - $timePassed);
  167. }
  168. /**
  169. * Get a number of seconds to sleep for.
  170. *
  171. * @param integer $timePassed
  172. *
  173. * @return int
  174. */
  175. private function _throttleMessagesPerMinute($timePassed)
  176. {
  177. $expectedDuration = $this->_messages / ($this->_rate / 60);
  178. return (int) ceil($expectedDuration - $timePassed);
  179. }
  180. }