Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added core/.DS_Store
Binary file not shown.
Binary file added core/src/.DS_Store
Binary file not shown.
Binary file added core/src/main/.DS_Store
Binary file not shown.
Binary file added core/src/main/java/.DS_Store
Binary file not shown.
Binary file added core/src/main/java/dev/.DS_Store
Binary file not shown.
Binary file added core/src/main/java/dev/failsafe/.DS_Store
Binary file not shown.
66 changes: 66 additions & 0 deletions core/src/main/java/dev/failsafe/ExecutorDelegation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 dev.failsafe;

import dev.failsafe.function.AsyncRunnable;
import dev.failsafe.function.ContextualSupplier;

import java.util.concurrent.Executor;

/**
* Wraps a {@link ContextualSupplier} or {@link AsyncRunnable} so that it runs on a user-supplied {@link Executor}
* rather than the calling thread, and propagates any resulting throwable back out of the executor thread.
* <p>
* Extracted from {@link Functions}, which previously combined this executor-delegation concern with
* execution-pipeline wiring and generic function adaptation in a single class with no shared state across the
* three concerns.
*
* @author Jonathan Halterman
*/
final class ExecutorDelegation {
static <R, T> ContextualSupplier<R, T> withExecutor(ContextualSupplier<R, T> supplier, Executor executor) {
return executor == null ? supplier : ctx -> {
executor.execute(() -> {
try {
supplier.get(ctx);
} catch (Throwable e) {
handleExecutorThrowable(e);
}
});
return null;
};
}

static <R> AsyncRunnable<R> withExecutor(AsyncRunnable<R> runnable, Executor executor) {
return executor == null ? runnable : exec -> {
executor.execute(() -> {
try {
runnable.run(exec);
} catch (Throwable e) {
handleExecutorThrowable(e);
}
});
};
}

private static void handleExecutorThrowable(Throwable e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
if (e instanceof Error)
throw (Error) e;
throw new FailsafeException(e);
}
}
1 change: 1 addition & 0 deletions core/src/main/java/dev/failsafe/FailsafeExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Function;

import static dev.failsafe.Functions.*;
import static dev.failsafe.FunctionAdapters.*;

/**
* <p>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/dev/failsafe/Fallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.concurrent.CompletionStage;

import static dev.failsafe.Functions.toFn;
import static dev.failsafe.FunctionAdapters.toFn;

/**
* A Policy that handles failures using a fallback function or result.
Expand Down Expand Up @@ -224,4 +224,4 @@ static Fallback<Void> none() {
*/
@Override
FallbackConfig<R> getConfig();
}
}
74 changes: 74 additions & 0 deletions core/src/main/java/dev/failsafe/FunctionAdapters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 dev.failsafe;

import dev.failsafe.function.*;
import dev.failsafe.internal.util.Assert;

/**
* Adapts various user-supplied functional interface shapes (runnables, suppliers, consumers, results) into the
* common {@link ContextualSupplier} or {@link CheckedFunction} shape that the rest of Failsafe operates on.
* <p>
* Extracted from {@link Functions}, which previously combined this purely-functional adaptation logic with
* execution-pipeline wiring and executor-thread delegation in a single class with no shared state across the three
* concerns.
*
* @author Jonathan Halterman
*/
final class FunctionAdapters {
static ContextualSupplier<Void, Void> toCtxSupplier(CheckedRunnable runnable) {
Assert.notNull(runnable, "runnable");
return ctx -> {
runnable.run();
return null;
};
}

static ContextualSupplier<Void, Void> toCtxSupplier(ContextualRunnable<Void> runnable) {
Assert.notNull(runnable, "runnable");
return ctx -> {
runnable.run(ctx);
return null;
};
}

static <R, T> ContextualSupplier<R, T> toCtxSupplier(CheckedSupplier<T> supplier) {
Assert.notNull(supplier, "supplier");
return ctx -> supplier.get();
}

static <T, R> CheckedFunction<T, R> toFn(CheckedConsumer<T> consumer) {
return t -> {
consumer.accept(t);
return null;
};
}

static <T, R> CheckedFunction<T, R> toFn(CheckedRunnable runnable) {
return t -> {
runnable.run();
return null;
};
}

static <T, R> CheckedFunction<T, R> toFn(CheckedSupplier<? extends R> supplier) {
return t -> supplier.get();
}

static <T, R> CheckedFunction<T, R> toFn(R result) {
return t -> result;
}
}
102 changes: 13 additions & 89 deletions core/src/main/java/dev/failsafe/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ final class Functions {
* @param <R> result type
*/
static <R> Function<SyncExecutionImpl<R>, ExecutionResult<R>> get(ContextualSupplier<R, R> supplier,
Executor executor) {
Executor executor) {

return execution -> {
ExecutionResult<R> result;
Throwable throwable = null;
try {
execution.preExecute();
result = ExecutionResult.success(withExecutor(supplier, executor).get(execution));
result = ExecutionResult.success(ExecutorDelegation.withExecutor(supplier, executor).get(execution));
} catch (Throwable t) {
throwable = t;
result = ExecutionResult.exception(t);
Expand Down Expand Up @@ -73,14 +73,14 @@ static <R> Function<SyncExecutionImpl<R>, ExecutionResult<R>> get(ContextualSupp
* @param <R> result type
*/
static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> getPromise(
ContextualSupplier<R, R> supplier, Executor executor) {
ContextualSupplier<R, R> supplier, Executor executor) {

Assert.notNull(supplier, "supplier");
return execution -> {
ExecutionResult<R> result;
try {
execution.preExecute();
result = ExecutionResult.success(withExecutor(supplier, executor).get(execution));
result = ExecutionResult.success(ExecutorDelegation.withExecutor(supplier, executor).get(execution));
} catch (Throwable t) {
result = ExecutionResult.exception(t);
}
Expand All @@ -97,15 +97,15 @@ static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult
* @param <R> result type
*/
static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> getPromiseExecution(
AsyncRunnable<R> runnable, Executor executor) {
AsyncRunnable<R> runnable, Executor executor) {

Assert.notNull(runnable, "runnable");
return new Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>>() {
@Override
public synchronized CompletableFuture<ExecutionResult<R>> apply(AsyncExecutionInternal<R> execution) {
try {
execution.preExecute();
withExecutor(runnable, executor).run(execution);
ExecutorDelegation.withExecutor(runnable, executor).run(execution);
} catch (Throwable e) {
execution.record(null, e);
}
Expand All @@ -125,15 +125,15 @@ public synchronized CompletableFuture<ExecutionResult<R>> apply(AsyncExecutionIn
*/
@SuppressWarnings("unchecked")
static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> getPromiseOfStage(
ContextualSupplier<R, ? extends CompletionStage<? extends R>> supplier, FailsafeFuture<R> future,
Executor executor) {
ContextualSupplier<R, ? extends CompletionStage<? extends R>> supplier, FailsafeFuture<R> future,
Executor executor) {

Assert.notNull(supplier, "supplier");
return execution -> {
CompletableFuture<ExecutionResult<R>> promise = new CompletableFuture<>();
try {
execution.preExecute();
CompletionStage<? extends R> stage = withExecutor(supplier, executor).get(execution);
CompletionStage<? extends R> stage = ExecutorDelegation.withExecutor(supplier, executor).get(execution);

if (stage == null) {
ExecutionResult<R> r = ExecutionResult.success(null);
Expand Down Expand Up @@ -168,7 +168,7 @@ static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult
* @param <R> result type
*/
static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> toExecutionAware(
Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn) {
Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn) {
return execution -> {
ExecutionResult<R> result = execution.getResult();
if (result == null) {
Expand All @@ -186,8 +186,8 @@ static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult
* @param <R> result type
*/
static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> toAsync(
Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn, Scheduler scheduler,
FailsafeFuture<R> future) {
Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn, Scheduler scheduler,
FailsafeFuture<R> future) {

AtomicBoolean scheduled = new AtomicBoolean();
return execution -> {
Expand Down Expand Up @@ -221,80 +221,4 @@ static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult
}
};
}

static ContextualSupplier<Void, Void> toCtxSupplier(CheckedRunnable runnable) {
Assert.notNull(runnable, "runnable");
return ctx -> {
runnable.run();
return null;
};
}

static ContextualSupplier<Void, Void> toCtxSupplier(ContextualRunnable<Void> runnable) {
Assert.notNull(runnable, "runnable");
return ctx -> {
runnable.run(ctx);
return null;
};
}

static <R, T> ContextualSupplier<R, T> toCtxSupplier(CheckedSupplier<T> supplier) {
Assert.notNull(supplier, "supplier");
return ctx -> supplier.get();
}

static <R, T> ContextualSupplier<R, T> withExecutor(ContextualSupplier<R, T> supplier, Executor executor) {
return executor == null ? supplier : ctx -> {
executor.execute(() -> {
try {
supplier.get(ctx);
} catch (Throwable e) {
handleExecutorThrowable(e);
}
});
return null;
};
}

static <R> AsyncRunnable<R> withExecutor(AsyncRunnable<R> runnable, Executor executor) {
return executor == null ? runnable : exec -> {
executor.execute(() -> {
try {
runnable.run(exec);
} catch (Throwable e) {
handleExecutorThrowable(e);
}
});
};
}

private static void handleExecutorThrowable(Throwable e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
if (e instanceof Error)
throw (Error) e;
throw new FailsafeException(e);
}

static <T, R> CheckedFunction<T, R> toFn(CheckedConsumer<T> consumer) {
return t -> {
consumer.accept(t);
return null;
};
}

static <T, R> CheckedFunction<T, R> toFn(CheckedRunnable runnable) {
return t -> {
runnable.run();
return null;
};
}

static <T, R> CheckedFunction<T, R> toFn(CheckedSupplier<? extends R> supplier) {
return t -> supplier.get();
}

static <T, R> CheckedFunction<T, R> toFn(R result) {
return t -> result;
}
}
}
Binary file not shown.
Loading