Use standard Java thread creation in Concurrent (#1880)

The AppEngine thread factory is only useful if we can't create our own
(this is no longer the case) or if we need access to AppEngine APIs
(this is no longer the case).

The Concurrent class is only used by the DNS writer and the
CreateGroupsAction.
This commit is contained in:
gbrodman 2022-12-12 15:42:02 -05:00 committed by GitHub
parent f7b7461891
commit d2da91d456

View file

@ -14,7 +14,6 @@
package google.registry.util;
import static com.google.appengine.api.ThreadManager.currentRequestThreadFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.Math.max;
@ -30,13 +29,12 @@ import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.function.Function;
/** Utilities for multithreaded operations in App Engine requests. */
/** Utilities for multithreaded operations. */
public final class Concurrent {
/** Maximum number of threads per pool. The actual GAE per-request limit is 50. */
/** Maximum number of threads per pool. */
private static final int MAX_THREADS = 10;
/**
@ -67,16 +65,10 @@ public final class Concurrent {
checkNotNull(funk);
checkNotNull(items);
int threadCount = max(1, min(items.size(), maxThreadCount));
ThreadFactory threadFactory = threadCount > 1 ? currentRequestThreadFactory() : null;
if (threadFactory == null) {
// Fall back to non-concurrent transform if we only want 1 thread, or if we can't get an App
// Engine thread factory (most likely caused by hitting this code from a command-line tool).
// Default Java system threads are not compatible with code that needs to interact with App
// Engine (such as Objectify), which we often have in funk when calling
// Concurrent.transform(). For more info see: http://stackoverflow.com/questions/15976406
if (threadCount == 1) {
return items.stream().map(funk).collect(toImmutableList());
}
ExecutorService executor = newFixedThreadPool(threadCount, threadFactory);
ExecutorService executor = newFixedThreadPool(threadCount);
try {
List<Future<B>> futures = new ArrayList<>();
for (final A item : items) {