ThrottlerPlugin.php 4.1KB

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