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