Skip to content

Commit eb5b880

Browse files
Properly handle MT Throttling and SMS Throttle Time (#285)
* Throttle value handles as milliseconds instead of seconds * Changed dynamic throttle value regex to tighter message
1 parent a839b5c commit eb5b880

File tree

7 files changed

+103
-5
lines changed

7 files changed

+103
-5
lines changed

src/Message/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ function (ResponseInterface $response) {
108108
$e->setTimeout(1);
109109
$e->setEntity($data);
110110

111-
if (preg_match('#\[\s+(\d+)\s+]#', $part['error-text'], $match)) {
112-
$e->setTimeout((int)$match[1] + 1);
111+
if (preg_match('#Throughput Rate Exceeded - please wait \[\s+(\d+)\s+] and retry#', $part['error-text'], $match)) {
112+
$seconds = max((int)$match[1] / 1000, 1);
113+
$e->setTimeout($seconds);
113114
}
114115

115116
throw $e;

src/SMS/ExceptionErrorHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public function __invoke(ResponseInterface $response, RequestInterface $request)
5656
$e->setTimeout(1);
5757
$e->setEntity($data);
5858

59-
if (preg_match('#\[\s+(\d+)\s+]#', $part['error-text'], $match)) {
60-
$e->setTimeout((int)$match[1] + 1);
59+
if (preg_match('#Throughput Rate Exceeded - please wait \[\s+(\d+)\s+] and retry#', $part['error-text'], $match)) {
60+
$seconds = max((int)$match[1] / 1000, 1);
61+
$e->setTimeout($seconds);
6162
}
6263

6364
throw $e;

test/Message/ClientTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ public function testThrowsExceptionWhenSearchResultMismatchesQuery(): void
673673
*/
674674
public function testRateLimitRetries(): void
675675
{
676+
$start = microtime(true);
676677
$rate = $this->getResponse('ratelimit');
677678
$rate2 = $this->getResponse('ratelimit');
678679
$success = $this->getResponse('success');
@@ -692,8 +693,10 @@ public function testRateLimitRetries(): void
692693
}))->willReturn($rate, $rate2, $success);
693694

694695
$message = $this->messageClient->send(new Text($args['to'], $args['from'], $args['text']));
696+
$end = microtime(true);
695697

696698
$this->assertEquals($success, @$message->getResponse());
699+
$this->assertGreaterThanOrEqual(2, $end - $start);
697700
}
698701

699702
/**
@@ -731,6 +734,40 @@ public function testRateLimitRetriesWithDefault(): void
731734
$this->assertEquals($successData['messages'][0]['message-id'], $message->getMessageId());
732735
}
733736

737+
/**
738+
* @throws ClientExceptionInterface
739+
* @throws ClientException\Exception
740+
* @throws ClientException\Request
741+
* @throws ServerException
742+
*/
743+
public function testAPIRateLimitRetries(): void
744+
{
745+
$start = microtime(true);
746+
$rate = $this->getResponse('mt-limit');
747+
$rate2 = $this->getResponse('mt-limit');
748+
$success = $this->getResponse('success');
749+
750+
$args = [
751+
'to' => '14845551345',
752+
'from' => '1105551334',
753+
'text' => 'test message'
754+
];
755+
756+
$this->vonageClient->send(Argument::that(function (Request $request) use ($args) {
757+
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
758+
$this->assertRequestJsonBodyContains('from', $args['from'], $request);
759+
$this->assertRequestJsonBodyContains('text', $args['text'], $request);
760+
761+
return true;
762+
}))->willReturn($rate, $rate2, $success);
763+
764+
$message = $this->messageClient->send(new Text($args['to'], $args['from'], $args['text']));
765+
$end = microtime(true);
766+
767+
$this->assertEquals($success, @$message->getResponse());
768+
$this->assertGreaterThanOrEqual(2, $end - $start);
769+
}
770+
734771
/**
735772
* @dataProvider searchRejectionsProvider
736773
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"message-count": "1",
3+
"messages": [
4+
{
5+
"status": "1",
6+
"error-text": "Exceeded maximum MT throughput[ MAX [ 30 ] INTERVAL [ 1000 ] COUNT [ 30 ] LAST INT-START [ 1617784922564 ] TIME_SINCE_LAST_INTERVAL_START [ 529 ] ] "
7+
}
8+
]
9+
}

test/SMS/ClientTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public function testCanParseServerErrorsAndThrowException(): void
154154
*/
155155
public function testCanHandleRateLimitRequests(): void
156156
{
157+
$start = microtime(true);
157158
$rate = $this->getResponse('ratelimit');
158159
$rate2 = $this->getResponse('ratelimit');
159160
$success = $this->getResponse('send-success');
@@ -173,6 +174,7 @@ public function testCanHandleRateLimitRequests(): void
173174

174175
$response = $this->smsClient->send(new SMS($args['to'], $args['from'], $args['text']));
175176
$sentData = $response->current();
177+
$end = microtime(true);
176178

177179
$this->assertCount(1, $response);
178180
$this->assertSame($args['to'], $sentData->getTo());
@@ -181,6 +183,7 @@ public function testCanHandleRateLimitRequests(): void
181183
$this->assertSame("12345", $sentData->getNetwork());
182184
$this->assertSame("3.14159265", $sentData->getRemainingBalance());
183185
$this->assertSame(0, $sentData->getStatus());
186+
$this->assertGreaterThanOrEqual(2, $end - $start);
184187
}
185188

186189
/**
@@ -219,6 +222,44 @@ public function testCanHandleRateLimitRequestsWithNoDeclaredTimeout(): void
219222
$this->assertSame(0, $sentData->getStatus());
220223
}
221224

225+
/**
226+
* @throws ClientExceptionInterface
227+
* @throws Client\Exception\Exception
228+
*/
229+
public function testCanHandleAPIRateLimitRequests(): void
230+
{
231+
$start = microtime(true);
232+
$rate = $this->getResponse('mt-limit');
233+
$rate2 = $this->getResponse('mt-limit');
234+
$success = $this->getResponse('send-success');
235+
$args = [
236+
'to' => '447700900000',
237+
'from' => '1105551334',
238+
'text' => 'test message'
239+
];
240+
241+
$this->vonageClient->send(Argument::that(function (Request $request) use ($args) {
242+
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
243+
$this->assertRequestJsonBodyContains('from', $args['from'], $request);
244+
$this->assertRequestJsonBodyContains('text', $args['text'], $request);
245+
246+
return true;
247+
}))->willReturn($rate, $rate2, $success);
248+
249+
$response = $this->smsClient->send(new SMS($args['to'], $args['from'], $args['text']));
250+
$sentData = $response->current();
251+
$end = microtime(true);
252+
253+
$this->assertCount(1, $response);
254+
$this->assertSame($args['to'], $sentData->getTo());
255+
$this->assertSame('0A0000000123ABCD1', $sentData->getMessageId());
256+
$this->assertSame("0.03330000", $sentData->getMessagePrice());
257+
$this->assertSame("12345", $sentData->getNetwork());
258+
$this->assertSame("3.14159265", $sentData->getRemainingBalance());
259+
$this->assertSame(0, $sentData->getStatus());
260+
$this->assertGreaterThanOrEqual(2, $end - $start);
261+
}
262+
222263
/**
223264
* @throws ClientExceptionInterface
224265
* @throws Client\Exception\Exception

test/SMS/responses/mt-limit.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"message-count": "1",
3+
"messages": [
4+
{
5+
"status": "1",
6+
"error-text": "Exceeded maximum MT throughput[ MAX [ 30 ] INTERVAL [ 1000 ] COUNT [ 30 ] LAST INT-START [ 1617784922564 ] TIME_SINCE_LAST_INTERVAL_START [ 529 ] ] "
7+
}
8+
]
9+
}

test/SMS/responses/ratelimit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"to": "14843472194",
66
"status": "1",
7-
"error-text": "Throughput Rate Exceeded - please wait [ 2 ] and retry",
7+
"error-text": "Throughput Rate Exceeded - please wait [ 702 ] and retry",
88
"network": "310260"
99
}
1010
]

0 commit comments

Comments
 (0)