Skip to content

Commit e00e5f4

Browse files
committed
Add APPENGINE_API_MAX_THREADS env variable for better api calls.
1 parent b10177d commit e00e5f4

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

runtime/runtime_impl_jetty121/src/main/java/com/google/apphosting/runtime/http/HttpApiHostClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ Config config() {
129129
return config;
130130
}
131131

132+
static int getMaxThreads(Config config) {
133+
int maxThreads = config.maxConnectionsPerDestination().orElse(100);
134+
String maxThreadsEnv = System.getenv("APPENGINE_API_MAX_THREADS");
135+
if (maxThreadsEnv != null) {
136+
try {
137+
int envMaxThreads = Integer.parseInt(maxThreadsEnv);
138+
logger.atInfo().log(
139+
"Overriding API max threads to %d from environment variable.", envMaxThreads);
140+
return envMaxThreads;
141+
} catch (NumberFormatException e) {
142+
logger.atWarning().withCause(e).log(
143+
"Invalid value for APPENGINE_API_MAX_THREADS: %s, using default %d",
144+
maxThreadsEnv, maxThreads);
145+
}
146+
}
147+
return maxThreads;
148+
}
149+
132150
static HttpApiHostClient create(String url, Config config) {
133151
if (System.getenv("APPENGINE_API_CALLS_USING_JDK_CLIENT") != null) {
134152
logger.atInfo().log("Using JDK HTTP client for API calls");

runtime/runtime_impl_jetty121/src/main/java/com/google/apphosting/runtime/http/JdkHttpApiHostClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static JdkHttpApiHostClient create(String url, Config config) {
114114
* of threads under retry, which overwhelms the JVM and the internal Datastore
115115
* Appserver connection, forcing it to respond with masking INTERNAL_ERROR fallbacks.
116116
*/
117-
int maxThreads = config.maxConnectionsPerDestination().orElse(100);
117+
int maxThreads = getMaxThreads(config);
118118
ThreadPoolExecutor tpe =
119119
new ThreadPoolExecutor(
120120
maxThreads, maxThreads, 60L, SECONDS, new LinkedBlockingQueue<>(), factory);

runtime/runtime_impl_jetty121/src/main/java/com/google/apphosting/runtime/http/JettyHttpApiHostClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static JettyHttpApiHostClient create(String url, Config config) {
120120
* If the system experiences a spike, Jetty will safely queue the outgoing RPCs, preventing the
121121
* JVM and the Appserver from being overwhelmed and eliminating the INTERNAL_ERROR fallback loop.
122122
*/
123-
int maxThreads = config.maxConnectionsPerDestination().orElse(100);
123+
int maxThreads = getMaxThreads(config);
124124
QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, 10, 60000, null, myThreadGroup);
125125
threadPool.setName("JettyHttpApiHostClient");
126126
threadPool.setDaemon(true);

0 commit comments

Comments
 (0)