-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: server times out when specified by CLOUD_RUN_TIMEOUT_SECONDS (#275)
* fix: server times out when specified by CLOUD_RUN_TIMEOUT_SECONDS
- Loading branch information
1 parent
dc9bc1f
commit 9e91f57
Showing
4 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
invoker/core/src/main/java/com/google/cloud/functions/invoker/http/TimeoutFilter.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 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.cloud.functions.invoker.http; | ||
|
||
import java.io.IOException; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.logging.Logger; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class TimeoutFilter implements Filter { | ||
|
||
private static final Logger logger = Logger.getLogger(TimeoutFilter.class.getName()); | ||
private final int timeoutMs; | ||
|
||
public TimeoutFilter(int timeoutSeconds) { | ||
this.timeoutMs = timeoutSeconds * 1000; // Convert seconds to milliseconds | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
Timer timer = new Timer(true); | ||
TimerTask timeoutTask = | ||
new TimerTask() { | ||
@Override | ||
public void run() { | ||
if (response instanceof HttpServletResponse) { | ||
try { | ||
((HttpServletResponse) response) | ||
.sendError(HttpServletResponse.SC_REQUEST_TIMEOUT, "Request timed out"); | ||
} catch (IOException e) { | ||
logger.warning("Error while sending HTTP response: " + e.toString()); | ||
} | ||
} else { | ||
try { | ||
response.getWriter().write("Request timed out"); | ||
} catch (IOException e) { | ||
logger.warning("Error while writing response: " + e.toString()); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
timer.schedule(timeoutTask, timeoutMs); | ||
|
||
try { | ||
chain.doFilter(request, response); | ||
timeoutTask.cancel(); | ||
} finally { | ||
timer.purge(); | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
invoker/core/src/test/java/com/google/cloud/functions/invoker/testfunctions/TimeoutHttp.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 @@ | ||
package com.google.cloud.functions.invoker.testfunctions; | ||
|
||
import com.google.cloud.functions.HttpFunction; | ||
import com.google.cloud.functions.HttpRequest; | ||
import com.google.cloud.functions.HttpResponse; | ||
|
||
public class TimeoutHttp implements HttpFunction { | ||
|
||
@Override | ||
public void service(HttpRequest request, HttpResponse response) throws Exception { | ||
try { | ||
Thread.sleep(2000); | ||
} catch (InterruptedException e) { | ||
response.getWriter().close(); | ||
} | ||
response.getWriter().write("finished\n"); | ||
} | ||
} |