ThrottlerPlugin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  79. {
  80. $time = $this->getTimestamp();
  81. if (!isset($this->start)) {
  82. $this->start = $time;
  83. }
  84. $duration = $time - $this->start;
  85. switch ($this->mode) {
  86. case self::BYTES_PER_MINUTE:
  87. $sleep = $this->throttleBytesPerMinute($duration);
  88. break;
  89. case self::MESSAGES_PER_SECOND:
  90. $sleep = $this->throttleMessagesPerSecond($duration);
  91. break;
  92. case self::MESSAGES_PER_MINUTE:
  93. $sleep = $this->throttleMessagesPerMinute($duration);
  94. break;
  95. default:
  96. $sleep = 0;
  97. break;
  98. }
  99. if ($sleep > 0) {
  100. $this->sleep($sleep);
  101. }
  102. }
  103. /**
  104. * Invoked when a Message is sent.
  105. */
  106. public function sendPerformed(Swift_Events_SendEvent $evt)
  107. {
  108. parent::sendPerformed($evt);
  109. ++$this->messages;
  110. }
  111. /**
  112. * Sleep for $seconds.
  113. *
  114. * @param int $seconds
  115. */
  116. public function sleep($seconds)
  117. {
  118. if (isset($this->sleeper)) {
  119. $this->sleeper->sleep($seconds);
  120. } else {
  121. sleep($seconds);
  122. }
  123. }
  124. /**
  125. * Get the current UNIX timestamp.
  126. *
  127. * @return int
  128. */
  129. public function getTimestamp()
  130. {
  131. if (isset($this->timer)) {
  132. return $this->timer->getTimestamp();
  133. }
  134. return time();
  135. }
  136. /**
  137. * Get a number of seconds to sleep for.
  138. *
  139. * @param int $timePassed
  140. *
  141. * @return int
  142. */
  143. private function throttleBytesPerMinute($timePassed)
  144. {
  145. $expectedDuration = $this->getBytesOut() / ($this->rate / 60);
  146. return (int) ceil($expectedDuration - $timePassed);
  147. }
  148. /**
  149. * Get a number of seconds to sleep for.
  150. *
  151. * @param int $timePassed
  152. *
  153. * @return int
  154. */
  155. private function throttleMessagesPerSecond($timePassed)
  156. {
  157. $expectedDuration = $this->messages / $this->rate;
  158. return (int) ceil($expectedDuration - $timePassed);
  159. }
  160. /**
  161. * Get a number of seconds to sleep for.
  162. *
  163. * @param int $timePassed
  164. *
  165. * @return int
  166. */
  167. private function throttleMessagesPerMinute($timePassed)
  168. {
  169. $expectedDuration = $this->messages / ($this->rate / 60);
  170. return (int) ceil($expectedDuration - $timePassed);
  171. }
  172. }