-
Notifications
You must be signed in to change notification settings - Fork 0
Timeoutfilter #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Timeoutfilter #151
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ddc4805
Co-authored by: johan.jansson@iths.se
EmmaTravljanin 0f282ac
Co-authored by: johan.jansson@iths.se
EmmaTravljanin 0aa5ae5
Co-authored by: johan.jansson@iths.se
EmmaTravljanin b68c870
Revert "Co-authored by: johan.jansson@iths.se"
EmmaTravljanin 3cd7f6e
Add unit tests for TimeoutFilter
Johan-jans 34333de
Revert "Add unit tests for TimeoutFilter"
Johan-jans da28f3b
commit
EmmaTravljanin 53b5985
Add unit tests for TimeoutFilter
Johan-jans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,69 @@ | ||
| package org.juv25d.filter; | ||
|
|
||
| import org.juv25d.filter.annotation.Global; | ||
| import org.juv25d.http.HttpRequest; | ||
| import org.juv25d.http.HttpResponse; | ||
| import org.juv25d.logging.ServerLogging; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.*; | ||
| import java.util.logging.Logger; | ||
|
|
||
| @Global(order = 1) | ||
| public class TimeoutFilter implements Filter { | ||
|
|
||
| private static final long TIMEOUT_MS = 2000; | ||
|
|
||
| private static final ExecutorService executor = | ||
| Executors.newCachedThreadPool(); | ||
| static { | ||
| Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
| executor.shutdownNow(); | ||
| try { | ||
| executor.awaitTermination(5, TimeUnit.SECONDS); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| private static final Logger logger = ServerLogging.getLogger(); | ||
|
|
||
| @Override | ||
| public void doFilter(HttpRequest req, | ||
| HttpResponse res, | ||
| FilterChain chain) throws IOException { | ||
|
|
||
| logger.info("TimeoutFilter START for " + req.path()); | ||
|
|
||
| Future<?> future = executor.submit(() -> { | ||
| try { | ||
| chain.doFilter(req, res); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
| logger.info("TimeoutFilter COMPLETED normally for " + req.path()); | ||
|
|
||
| } catch (TimeoutException e) { | ||
|
|
||
| logger.warning("Timeout triggered for " + req.path()); | ||
|
|
||
| future.cancel(true); | ||
|
|
||
| res.setStatusCode(504); | ||
| res.setStatusText("Gateway Timeout"); | ||
| res.setBody("504 - Gateway Timeout".getBytes()); | ||
|
EmmaTravljanin marked this conversation as resolved.
|
||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| future.cancel(true); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
EmmaTravljanin marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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,23 @@ | ||
| package org.juv25d.plugin; | ||
|
|
||
| import org.juv25d.http.HttpRequest; | ||
| import org.juv25d.http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class SlowPlugin implements Plugin { | ||
|
|
||
| @Override | ||
| public void handle(HttpRequest req, HttpResponse res) throws IOException { | ||
|
|
||
| try { | ||
| Thread.sleep(5000); | ||
| } catch (InterruptedException e) { | ||
| return; | ||
| } | ||
|
|
||
| res.setStatusCode(200); | ||
| res.setStatusText("OK"); | ||
| res.setBody("Slow response finished".getBytes()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,73 @@ | ||
| package org.juv25d.filter; | ||
|
|
||
| import org.juv25d.http.HttpRequest; | ||
| import org.juv25d.http.HttpResponse; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class TimeoutFilterTest { | ||
|
|
||
| @Mock HttpRequest req; | ||
| @Mock FilterChain chain; | ||
|
|
||
| @Test | ||
| void fastRequest_keepsDefault200() throws IOException { | ||
| when(req.path()).thenReturn("/fast"); | ||
| HttpResponse res = new HttpResponse(); | ||
| TimeoutFilter filter = new TimeoutFilter(); | ||
|
|
||
| doNothing().when(chain).doFilter(req, res); | ||
|
|
||
| filter.doFilter(req, res, chain); | ||
|
|
||
| verify(chain).doFilter(req, res); | ||
| assertThat(res.statusCode()).isEqualTo(200); | ||
| } | ||
|
|
||
| @Test | ||
| @Timeout(value = 4, unit = TimeUnit.SECONDS) | ||
| void slowRequest_sets504() throws IOException { | ||
| when(req.path()).thenReturn("/slow"); | ||
| HttpResponse res = new HttpResponse(); | ||
| TimeoutFilter filter = new TimeoutFilter(); | ||
|
|
||
| doAnswer(inv -> { | ||
| Thread.sleep(3_000); // > 2000ms => timeout | ||
| return null; | ||
| }).when(chain).doFilter(req, res); | ||
|
|
||
| filter.doFilter(req, res, chain); | ||
|
|
||
| verify(chain).doFilter(req, res); | ||
| assertThat(res.statusCode()).isEqualTo(504); | ||
| assertThat(res.statusText()).isEqualTo("Gateway Timeout"); | ||
| assertThat(new String(res.body(), StandardCharsets.UTF_8)) | ||
| .isEqualTo("504 - Gateway Timeout"); | ||
| } | ||
|
|
||
| @Test | ||
| void downstreamIOException_throwsRuntimeException() throws IOException { | ||
| when(req.path()).thenReturn("/boom"); | ||
| HttpResponse res = new HttpResponse(); | ||
| TimeoutFilter filter = new TimeoutFilter(); | ||
|
|
||
| doThrow(new IOException("fail")).when(chain).doFilter(req, res); | ||
|
|
||
| assertThatThrownBy(() -> filter.doFilter(req, res, chain)) | ||
| .isInstanceOf(RuntimeException.class); | ||
|
|
||
| verify(chain).doFilter(req, res); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.