AntiFloodPlugin.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Reduces network flooding when sending large amounts of mail.
  11. * @package Swift
  12. * @subpackage Plugins
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Plugins_AntiFloodPlugin
  16. implements Swift_Events_SendListener, Swift_Plugins_Sleeper
  17. {
  18. /**
  19. * The number of emails to send before restarting Transport.
  20. * @var int
  21. * @access private
  22. */
  23. private $_threshold;
  24. /**
  25. * The number of seconds to sleep for during a restart.
  26. * @var int
  27. * @access private
  28. */
  29. private $_sleep;
  30. /**
  31. * The internal counter.
  32. * @var int
  33. * @access private
  34. */
  35. private $_counter = 0;
  36. /**
  37. * The Sleeper instance for sleeping.
  38. * @var Swift_Plugins_Sleeper
  39. * @access private
  40. */
  41. private $_sleeper;
  42. /**
  43. * Create a new AntiFloodPlugin with $threshold and $sleep time.
  44. * @param int $threshold
  45. * @param int $sleep time
  46. * @param Swift_Plugins_Sleeper $sleeper (not needed really)
  47. */
  48. public function __construct($threshold = 99, $sleep = 0,
  49. Swift_Plugins_Sleeper $sleeper = null)
  50. {
  51. $this->setThreshold($threshold);
  52. $this->setSleepTime($sleep);
  53. $this->_sleeper = $sleeper;
  54. }
  55. /**
  56. * Set the number of emails to send before restarting.
  57. * @param int $threshold
  58. */
  59. public function setThreshold($threshold)
  60. {
  61. $this->_threshold = $threshold;
  62. }
  63. /**
  64. * Get the number of emails to send before restarting.
  65. * @return int
  66. */
  67. public function getThreshold()
  68. {
  69. return $this->_threshold;
  70. }
  71. /**
  72. * Set the number of seconds to sleep for during a restart.
  73. * @param int $sleep time
  74. */
  75. public function setSleepTime($sleep)
  76. {
  77. $this->_sleep = $sleep;
  78. }
  79. /**
  80. * Get the number of seconds to sleep for during a restart.
  81. * @return int
  82. */
  83. public function getSleepTime()
  84. {
  85. return $this->_sleep;
  86. }
  87. /**
  88. * Invoked immediately before the Message is sent.
  89. * @param Swift_Events_SendEvent $evt
  90. */
  91. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  92. {
  93. }
  94. /**
  95. * Invoked immediately after the Message is sent.
  96. * @param Swift_Events_SendEvent $evt
  97. */
  98. public function sendPerformed(Swift_Events_SendEvent $evt)
  99. {
  100. ++$this->_counter;
  101. if ($this->_counter >= $this->_threshold)
  102. {
  103. $transport = $evt->getTransport();
  104. $transport->stop();
  105. if ($this->_sleep)
  106. {
  107. $this->sleep($this->_sleep);
  108. }
  109. $transport->start();
  110. $this->_counter = 0;
  111. }
  112. }
  113. /**
  114. * Sleep for $seconds.
  115. * @param int $seconds
  116. */
  117. public function sleep($seconds)
  118. {
  119. if (isset($this->_sleeper))
  120. {
  121. $this->_sleeper->sleep($seconds);
  122. }
  123. else
  124. {
  125. sleep($seconds);
  126. }
  127. }
  128. }