Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
intervalToSeconds example
public
function
setExpiresAt
(
int
$time
)
: void
{
$this
->expiresAt =
$time
;
}
public
function
shouldThrottle
(
int
$count
, int
$now
)
: bool
{
$limit
=
$this
->
getLimit
(
$count
)
;
$elapsed
=
$now
-
$this
->timer;
return
$limit
!== null &&
$elapsed
<
$this
->
intervalToSeconds
(
$limit
[
'interval'
]
)
;
}
public
function
getRetryAfter
(
)
: \DateTimeImmutable
{
$limit
=
$this
->
getLimit
(
$this
->attempts + 1
)
;
if
(
$limit
=== null
)
{
$retryAfter
=
time
(
)
;
}
else
{
$retryAfter
=
$this
->timer +
$this
->
intervalToSeconds
(
$limit
[
'interval'
]
)
;
}
$backoff
->
setAttempts
(
3
)
;
static
::
assertEquals
(
3,
$backoff
->
getAttempts
(
)
)
;
foreach
(
$this
->config
[
'limits'
]
as
$limit
)
{
for
(
$i
= 0;
$i
< 2; ++
$i
)
{
// request should be thorttled for new request
static
::
assertTrue
(
$backoff
->
shouldThrottle
(
$backoff
->
getAttempts
(
)
+ 1,
time
(
)
)
)
;
static
::
assertEquals
(
0,
$backoff
->
getAvailableAttempts
(
time
(
)
)
)
;
// after wait time, request could be send again
static
::
assertFalse
(
$backoff
->
shouldThrottle
(
$backoff
->
getAttempts
(
)
+ 1,
time
(
)
+
$this
->
intervalToSeconds
(
$limit
[
'interval'
]
)
)
)
;
// new request sent
$backoff
->
setTimer
(
time
(
)
)
;
$backoff
->
setAttempts
(
$backoff
->
getAttempts
(
)
+ 1
)
;
// request should be thorttled again
static
::
assertTrue
(
$backoff
->
shouldThrottle
(
$backoff
->
getAttempts
(
)
,
time
(
)
)
)
;
static
::
assertEquals
(
0,
$backoff
->
getAvailableAttempts
(
time
(
)
)
)
;
// after wait time, request could be send again
$time
=
time
(
)
+
$this
->
intervalToSeconds
(
$limit
[
'interval'
]
)
;