Skip to content

Commit 19c46b0

Browse files
[11.x] init Timebox helper section (#10177)
* init new Timebox section * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 3445d66 commit 19c46b0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

helpers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Lottery](#lottery)
1010
- [Pipeline](#pipeline)
1111
- [Sleep](#sleep)
12+
- [Timebox](#timebox)
1213

1314
<a name="introduction"></a>
1415
## Introduction
@@ -2769,3 +2770,20 @@ $start->diffForHumans(); // 1 second ago
27692770
```
27702771

27712772
Laravel uses the `Sleep` class internally whenever it is pausing execution. For example, the [`retry`](#method-retry) helper uses the `Sleep` class when sleeping, allowing for improved testability when using that helper.
2773+
2774+
<a name="timebox"></a>
2775+
### Timebox
2776+
2777+
Laravel's `Timebox` class ensures that the given callback always takes a fixed amount of time to execute, even if its actual execution completes sooner. This is particularly useful for cryptographic operations and user authentication checks, where attackers might exploit variations in execution time to infer sensitive information.
2778+
2779+
If the execution exceeds the fixed duration, `Timebox` has no effect. It is up to the developer to choose a sufficiently long time as the fixed duration to account for worst-case scenarios.
2780+
2781+
The call method accepts a closure and a time limit in microseconds, and then executes the closure and waits until the time limit is reached:
2782+
2783+
use Illuminate\Support\Timebox;
2784+
2785+
(new Timebox)->call(function ($timebox) {
2786+
// ...
2787+
}, microseconds: 10000);
2788+
2789+
If an exception is thrown within the closure, this class will respect the defined delay and re-throw the exception after the delay.

0 commit comments

Comments
 (0)