Skip to content

Commit 17a24db

Browse files
fix: update the async property in the async-timeout example (#320)
- Fixed async properties in the application.yml - Added logs in the Query example to show that the execution is not interrupted but it continues after the `cancel(true)` invocation. This is due by the CompletableFuture object that cannot be really stopped by design: (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#cancel-boolean-) - Updated README Refs: #317
1 parent 909a296 commit 17a24db

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

async-timeout/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Async timeout
22

3-
The `graphql.servlet.async-timeout` property enables you to define a request level timeout to be enforced. This can help
3+
The `graphql.servlet.async.timeout` property enables you to define a request level timeout to be enforced. This can help
44
in preventing DDoS attacks. This is only available when running the application in `servlet` mode. The value is the
55
timeout in milliseconds that should be enforced. If the GraphQL request has not finished within that timeout a GraphQL
66
error will be returned:
@@ -30,12 +30,9 @@ In this example the timeout has been configured at 500 milliseconds in
3030
[application.yml](src/main/resources/application.yml).
3131

3232
```yaml
33-
graphql.servlet.async-timeout: 500
33+
graphql.servlet.async.timeout: 500
3434
```
3535
36-
Note that the property `graphql.servlet.async-mode-enabled` has not been set to `true` here, because that's it's enabled
37-
by default already.
38-
3936
## GraphQL execution cancellation
4037
4138
The [Query](src/main/java/com/graphql/sample/boot/Query.java) class shows two examples of queries that will take 3000
@@ -59,11 +56,11 @@ query {
5956

6057
Either one will stop processing after 500ms because of the configured timeout.
6158

62-
The main difference between the two methods in the `Query` class however is the ability to also cancel the execution of
59+
~~The main difference between the two methods in the `Query` class however is the ability to also cancel the execution of
6360
the GraphQL request. Ideally the GraphQL execution is cancelled as well, because otherwise that request is still taking
6461
up resources and Thread(s) even though the HTTP request has stopped. An attacker could repeatedly call that particular
6562
GraphQL query which now returns fast, and that would spawn a new Thread with long running execution within the
66-
application itself. That could ultimately lead to an application that crashes because it runs out of memory for example.
63+
application itself. That could ultimately lead to an application that crashes because it runs out of memory for example.~~
6764

6865
To make the GraphQL requests themselves cancellable you only have to remember to always return a `CompletableFuture`
6966
instead of the value directly. The `Query.getHello()` method is a simple example of a cancellable GraphQL request.

async-timeout/src/main/java/com/graphql/sample/boot/Query.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ CompletableFuture<String> getHello() {
2020
return CompletableFuture.supplyAsync(
2121
() -> {
2222
try {
23+
log.info("Start Hello query execution");
2324
Thread.sleep(3000);
25+
log.info("Execute Hello query after the sleep");
2426
} catch (InterruptedException e) {
2527
Thread.currentThread().interrupt();
2628
}
29+
log.info("End Hello query execution");
2730
return "world";
2831
});
2932
}
@@ -36,10 +39,13 @@ CompletableFuture<String> getHello() {
3639
*/
3740
String getWorld() {
3841
try {
42+
log.info("Start World query execution");
3943
Thread.sleep(3000);
44+
log.info("Execute World query after the sleep");
4045
} catch (InterruptedException e) {
4146
Thread.currentThread().interrupt();
4247
}
48+
log.info("End World query execution");
4349
return "hello";
4450
}
4551
}

async-timeout/src/main/resources/application.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ server:
66
graphql:
77
servlet:
88
exception-handlers-enabled: true
9-
async-timeout: 500
9+
async:
10+
timeout: 500
11+
enabled: true
1012
graphiql:
1113
enabled: true

0 commit comments

Comments
 (0)