ThrottlerPlugin.php 3.8KB

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