ThrottlerPlugin.php 4.7 KB

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