Skip to content

Commit 887b2a0

Browse files
committed
add ttl support to fs transport.
1 parent 2ec6e26 commit 887b2a0

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

‎pkg/amqp-ext/AmqpProducer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function send(PsrDestination $destination, PsrMessage $message)
9999
*/
100100
public function setDeliveryDelay($deliveryDelay)
101101
{
102-
throw new DeliveryDelayNotSupportedException('The provider does not support delivery delay feature');
102+
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
103103
}
104104

105105
/**

‎pkg/amqp-lib/AmqpProducer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function send(PsrDestination $destination, PsrMessage $message)
9494
*/
9595
public function setDeliveryDelay($deliveryDelay)
9696
{
97-
throw new DeliveryDelayNotSupportedException('The provider does not support delivery delay feature');
97+
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
9898
}
9999

100100
/**

‎pkg/fs/FsConsumer.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ public function receiveNoWait()
8989

9090
if ($rawMessage) {
9191
try {
92-
$this->preFetchedMessages[] = FsMessage::jsonUnserialize($rawMessage);
92+
$fetchedMessage = FsMessage::jsonUnserialize($rawMessage);
93+
$expireAt = $fetchedMessage->getHeader('x-expire-at');
94+
if ($expireAt && $expireAt - microtime(true) < 0) {
95+
// message has expired, just drop it.
96+
return;
97+
}
98+
99+
$this->preFetchedMessages[] = $fetchedMessage;
93100
} catch (\Exception $e) {
94101
throw new \LogicException(sprintf("Cannot decode json message '%s'", $rawMessage), null, $e);
95102
}

‎pkg/fs/FsProducer.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
namespace Enqueue\Fs;
44

5+
use Interop\Queue\DeliveryDelayNotSupportedException;
56
use Interop\Queue\InvalidDestinationException;
67
use Interop\Queue\InvalidMessageException;
8+
use Interop\Queue\PriorityNotSupportedException;
79
use Interop\Queue\PsrDestination;
810
use Interop\Queue\PsrMessage;
911
use Interop\Queue\PsrProducer;
1012
use Makasim\File\TempFile;
1113

1214
class FsProducer implements PsrProducer
1315
{
16+
/**
17+
* @var float|int|null
18+
*/
19+
private $timeToLive;
20+
1421
/**
1522
* @var FsContext
1623
*/
@@ -41,6 +48,10 @@ public function send(PsrDestination $destination, PsrMessage $message)
4148
return;
4249
}
4350

51+
if (null !== $this->timeToLive) {
52+
$message->setHeader('x-expire-at', microtime(true) + ($this->timeToLive / 1000));
53+
}
54+
4455
$rawMessage = '|'.json_encode($message);
4556

4657
if (JSON_ERROR_NONE !== json_last_error()) {
@@ -62,7 +73,7 @@ public function send(PsrDestination $destination, PsrMessage $message)
6273
*/
6374
public function setDeliveryDelay($deliveryDelay)
6475
{
65-
throw new \LogicException('Not implemented');
76+
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
6677
}
6778

6879
/**
@@ -78,7 +89,7 @@ public function getDeliveryDelay()
7889
*/
7990
public function setPriority($priority)
8091
{
81-
throw new \LogicException('Not implemented');
92+
throw PriorityNotSupportedException::providerDoestNotSupportIt();
8293
}
8394

8495
/**
@@ -94,7 +105,7 @@ public function getPriority()
94105
*/
95106
public function setTimeToLive($timeToLive)
96107
{
97-
throw new \LogicException('Not implemented');
108+
$this->timeToLive = $timeToLive;
98109
}
99110

100111
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Enqueue\Fs\Tests\Spec;
4+
5+
use Enqueue\Fs\FsConnectionFactory;
6+
use Enqueue\Fs\FsContext;
7+
use Interop\Queue\Spec\SendAndReceiveTimeToLiveMessagesFromQueueSpec;
8+
9+
class FsSendAndReceiveTimeToLiveMessagesFromQueueTest extends SendAndReceiveTimeToLiveMessagesFromQueueSpec
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*
14+
* @return FsContext
15+
*/
16+
protected function createContext()
17+
{
18+
return (new FsConnectionFactory())->createContext();
19+
}
20+
}

‎pkg/sqs/SqsProducer.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Interop\Queue\PsrDestination;
99
use Interop\Queue\PsrMessage;
1010
use Interop\Queue\PsrProducer;
11+
use Interop\Queue\TimeToLiveNotSupportedException;
1112

1213
class SqsProducer implements PsrProducer
1314
{
@@ -105,7 +106,7 @@ public function getDeliveryDelay()
105106
*/
106107
public function setPriority($priority)
107108
{
108-
throw new PriorityNotSupportedException('Provider does not support priority feature');
109+
throw PriorityNotSupportedException::providerDoestNotSupportIt();
109110
}
110111

111112
/**
@@ -121,7 +122,7 @@ public function getPriority()
121122
*/
122123
public function setTimeToLive($timeToLive)
123124
{
124-
throw new PriorityNotSupportedException('Provider does not support time to live feature');
125+
throw TimeToLiveNotSupportedException::providerDoestNotSupportIt();
125126
}
126127

127128
/**

0 commit comments

Comments
 (0)