Small API changes

This commit is contained in:
Rene Pfeuffer
2019-12-12 16:13:36 +01:00
parent 6c8820719e
commit 4fd2a0dd23
5 changed files with 150 additions and 30 deletions

View File

@@ -12,31 +12,25 @@ import static sonia.scm.repository.spi.SyncAsyncExecutor.ExecutionType.SYNCHRONO
public class DefaultSyncAsyncExecutor implements SyncAsyncExecutor {
public static final long DEFAULT_MAX_ASYNC_RUNTIME = 60 * 1000L;
private final Executor executor;
private final Instant switchToAsyncTime;
private final long maxAsyncRuntime;
private AtomicLong asyncRuntime = new AtomicLong(0L);
private final long maxAsyncAbortMilliseconds;
private AtomicLong accumulatedAsyncRuntime = new AtomicLong(0L);
private boolean executedAllSynchronously = true;
DefaultSyncAsyncExecutor(Executor executor, Instant switchToAsyncTime) {
this(executor, switchToAsyncTime, DEFAULT_MAX_ASYNC_RUNTIME);
}
DefaultSyncAsyncExecutor(Executor executor, Instant switchToAsyncTime, long maxAsyncRuntime) {
DefaultSyncAsyncExecutor(Executor executor, Instant switchToAsyncTime, int maxAsyncAbortSeconds) {
this.executor = executor;
this.switchToAsyncTime = switchToAsyncTime;
this.maxAsyncRuntime = maxAsyncRuntime;
this.maxAsyncAbortMilliseconds = maxAsyncAbortSeconds * 1000L;
}
public ExecutionType execute(Consumer<ExecutionType> runnable, Runnable abortionFallback) {
if (Instant.now().isAfter(switchToAsyncTime)) {
public ExecutionType execute(Consumer<ExecutionType> task, Runnable abortionFallback) {
if (switchToAsyncTime.isBefore(Instant.now())) {
executor.execute(() -> {
if (asyncRuntime.get() < maxAsyncRuntime) {
if (accumulatedAsyncRuntime.get() < maxAsyncAbortMilliseconds) {
long chunkStartTime = System.currentTimeMillis();
runnable.accept(ASYNCHRONOUS);
asyncRuntime.addAndGet(System.currentTimeMillis() - chunkStartTime);
task.accept(ASYNCHRONOUS);
accumulatedAsyncRuntime.addAndGet(System.currentTimeMillis() - chunkStartTime);
} else {
abortionFallback.run();
}
@@ -44,7 +38,7 @@ public class DefaultSyncAsyncExecutor implements SyncAsyncExecutor {
executedAllSynchronously = false;
return ASYNCHRONOUS;
} else {
runnable.accept(SYNCHRONOUS);
task.accept(SYNCHRONOUS);
return SYNCHRONOUS;
}
}

View File

@@ -11,22 +11,41 @@ import java.util.concurrent.Executors;
public class DefaultSyncAsyncExecutorProvider implements SyncAsyncExecutorProvider, Closeable {
public static final int DEFAULT_MAX_ASYNC_ABORT_SECONDS = 60;
public static final String MAX_ASYNC_ABORT_SECONDS_PROPERTY = "scm.maxAsyncAbortSeconds";
public static final int DEFAULT_NUMBER_OF_THREADS = 4;
public static final String NUMBER_OF_THREADS_PROPERTY = "scm.asyncThreads";
private final ExecutorService executor;
private final int defaultMaxAsyncAbortSeconds;
public DefaultSyncAsyncExecutorProvider() {
this(Executors.newFixedThreadPool(4));
this(Executors.newFixedThreadPool(getProperty(NUMBER_OF_THREADS_PROPERTY, DEFAULT_NUMBER_OF_THREADS)));
}
public DefaultSyncAsyncExecutorProvider(ExecutorService executor) {
this.executor = executor;
this.defaultMaxAsyncAbortSeconds = getProperty(MAX_ASYNC_ABORT_SECONDS_PROPERTY, DEFAULT_MAX_ASYNC_ABORT_SECONDS);
}
public SyncAsyncExecutor createExecutorWithSecondsToTimeout(int seconds) {
return new DefaultSyncAsyncExecutor(executor, Instant.now().plus(seconds, ChronoUnit.SECONDS));
public SyncAsyncExecutor createExecutorWithSecondsToTimeout(int switchToAsyncInSeconds) {
return createExecutorWithSecondsToTimeout(switchToAsyncInSeconds, DEFAULT_MAX_ASYNC_ABORT_SECONDS);
}
public SyncAsyncExecutor createExecutorWithSecondsToTimeout(int switchToAsyncInSeconds, int maxAsyncAbortSeconds) {
return new DefaultSyncAsyncExecutor(
executor,
Instant.now().plus(switchToAsyncInSeconds, ChronoUnit.SECONDS),
maxAsyncAbortSeconds);
}
@Override
public void close() {
executor.shutdownNow();
}
private static int getProperty(String key, int defaultValue) {
return Integer.parseInt(System.getProperty(key, Integer.toString(defaultValue)));
}
}