Skip to content

Commit 5a57044

Browse files
committed
Added documentation for Timeouts
1 parent e78d2c6 commit 5a57044

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

couscous.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ menu:
114114
text: Databases
115115
url: /docs/environment/database.html
116116
title: Using a database from AWS Lambda
117+
timeouts:
118+
text: Timeouts
119+
url: /docs/environment/timeouts.html
120+
title: Configure and handle timeouts
117121
custom-domains:
118122
text: Custom domains
119123
url: /docs/environment/custom-domains.html

docs/environment/database.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ When possible, an alternative to NAT Gateways is to split the work done by a lam
5454

5555
Finally, another free alternative to NAT Gateway is to access AWS services by creating "*private VPC endpoints*": this is possible for S3, API Gateway, [and more](https://docs.aws.amazon.com/en_pv/vpc/latest/userguide/vpc-endpoints-access.html).
5656

57+
Read more in the section about [timeouts](/docs/environment/timeouts.md).
58+
5759
## Creating a database
5860

5961
On the [RDS console](https://console.aws.amazon.com/rds/home):

docs/environment/timeouts.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Timeouts
3+
current_menu: timeouts
4+
introduction: Configure and handle timeouts.
5+
---
6+
7+
When a Lambda function times out, it is like the power to the computer is suddenly
8+
just turned off. This does not give the application a chance to shutdown properly.
9+
This often leaves you without any logs and the problem could be hard to fix.
10+
11+
Bref will throw an `LambdaTimeout` exception just before the Lambda actually times
12+
out. This will allow your application to actually shutdown.
13+
14+
This feature is enabled automatically for the `php-xx` layer and the `console` layer.
15+
The `php-xx-fpm` layer needs to opt-in by adding the following to `index.php`.
16+
17+
```php
18+
if (isset($_SERVER['LAMBDA_TASK_ROOT'])) {
19+
\Bref\Timeout\Timeout::enable();
20+
}
21+
```
22+
23+
## Configuration
24+
25+
You may configure this behavior with the `BREF_TIMEOUT` environment variable. To
26+
always trigger an exception after 10 seconds, set `BREF_TIMEOUT=10`. To disable
27+
Bref throwing an exception use value `BREF_TIMEOUT=-1`. To automatically set the
28+
timeout just a hair shorter than the Lambda timeout, use `BREF_TIMEOUT=0`.
29+
30+
## Catching the exception
31+
32+
If you are using a framework, then the framework is probably catching all exceptions
33+
and displays an error page for the users. You may of course catch the exception
34+
yourself:
35+
36+
```php
37+
<?php
38+
39+
require dirname(__DIR__) . '/vendor/autoload.php';
40+
41+
use Bref\Context\Context;
42+
use Bref\Timeout\LambdaTimeout;
43+
44+
class Handler implements \Bref\Event\Handler
45+
{
46+
public function handle($event, Context $context)
47+
{
48+
try {
49+
$this->generateResponse();
50+
} catch (LambdaTimeout $e) {
51+
echo 'Oops, sorry. We spent too much time on this.';
52+
} catch (\Throwable $e) {
53+
echo 'Some unexpected error happened.';
54+
}
55+
}
56+
57+
private function generateResponse()
58+
{
59+
$pi = // ...
60+
echo 'Pi is '.$pi;
61+
}
62+
}
63+
64+
return new Handler();
65+
```

0 commit comments

Comments
 (0)