-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Allow to limit the GraphQL queries execution by setting a time limit
- Loading branch information
Showing
5 changed files
with
114 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
graphql/src/main/java/org/restheart/graphql/instrumentation/MaxQueryTimeInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.restheart.graphql.instrumentation; | ||
|
||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import graphql.ExecutionResult; | ||
import graphql.execution.AbortExecutionException; | ||
import graphql.execution.instrumentation.InstrumentationContext; | ||
import graphql.execution.instrumentation.InstrumentationState; | ||
import static graphql.execution.instrumentation.SimpleInstrumentationContext.noOp; | ||
import graphql.execution.instrumentation.SimplePerformantInstrumentation; | ||
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters; | ||
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters; | ||
|
||
/** | ||
* Aborts the execution if the query time is greater than the specified queryTimeLimit. | ||
*/ | ||
public class MaxQueryTimeInstrumentation extends SimplePerformantInstrumentation { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MaxQueryTimeInstrumentation.class); | ||
|
||
private final int queryTimeLimit; // in ms | ||
|
||
/** | ||
* Creates a new instrumentation that aborts the execution if the query time is greater than the specified queryTimeLimit | ||
* | ||
* @param queryTimeLimit max allowed query time, otherwise execution will be aborted. Set queryTimeLimit <= 0 to disable it. | ||
*/ | ||
public MaxQueryTimeInstrumentation(int queryTimeLimit) { | ||
this.queryTimeLimit = queryTimeLimit; | ||
} | ||
|
||
@Override | ||
public InstrumentationState createState(InstrumentationCreateStateParameters parameters) { | ||
return new State(System.currentTimeMillis()); | ||
} | ||
|
||
@Override | ||
public InstrumentationContext<ExecutionResult> beginField(InstrumentationFieldParameters parameters, InstrumentationState rawState) { | ||
if (this.queryTimeLimit <= 0) { | ||
return noOp(); | ||
} | ||
|
||
State state = InstrumentationState.ofState(rawState); | ||
var elapsed = System.currentTimeMillis() - state.startTime; | ||
|
||
|
||
if (elapsed > this.queryTimeLimit) { | ||
LOGGER.debug("Exceeded query time limit of {} while attempting to fetch field '{}'. Current query elapsed time: {}.", this.queryTimeLimit, parameters.getExecutionStepInfo().getPath(), elapsed); | ||
throw mkAbortException(this.queryTimeLimit); | ||
} else { | ||
LOGGER.trace("Fetching data for field '{}' initiated. Current elapsed query time: {}.", parameters.getExecutionStepInfo().getPath(), elapsed); | ||
return noOp(); | ||
} | ||
} | ||
|
||
/** | ||
* Generate the exception with error message | ||
* | ||
* @param maxTime the maximum time allowed | ||
* | ||
* @return an instance of AbortExecutionException | ||
*/ | ||
protected AbortExecutionException mkAbortException(long maxTime) { | ||
return new QueryTimeoutException("Maximum query time limit of " + maxTime + "ms exceeded"); | ||
} | ||
|
||
private static record State(long startTime) implements InstrumentationState { | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
graphql/src/main/java/org/restheart/graphql/instrumentation/QueryTimeoutException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||
*/ | ||
|
||
package org.restheart.graphql.instrumentation; | ||
|
||
import graphql.execution.AbortExecutionException; | ||
|
||
/** | ||
* | ||
* @author uji | ||
*/ | ||
public class QueryTimeoutException extends AbortExecutionException { | ||
public QueryTimeoutException(String message) { | ||
super(message); | ||
} | ||
} |