Skip to content

Commit

Permalink
Add maven modules with okhttp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Feb 13, 2022
1 parent 3db3c6a commit dc6b459
Show file tree
Hide file tree
Showing 206 changed files with 802 additions and 149 deletions.
30 changes: 30 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>dev.failsafe</groupId>
<artifactId>failsafe-parent</artifactId>
<version>3.2.2-SNAPSHOT</version>
</parent>

<artifactId>failsafe</artifactId>
<name>Failsafe</name>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
File renamed without changes.
47 changes: 47 additions & 0 deletions core/src/main/java/dev/failsafe/Call.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 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.CheckedRunnable;

/**
* A call that can perform Failsafe executions and can be cancelled. Cancellations are propagated to any {@link
* ExecutionContext#onCancel(CheckedRunnable) cancelCallback} that is registered. Useful for integrating with libraries
* that support cancellation.
* <p>
* To perform cancellable async executions, use the {@link FailsafeExecutor} async methods.
* </p>
*
* @param <R> result type
* @author Jonathan Halterman
*/
public interface Call<R> {
/**
* Executes the call until a successful result is returned or the configured policies are exceeded.
*
* @throws FailsafeException if the execution fails with a checked Exception. {@link FailsafeException#getCause()} can
* be used to learn the underlying checked exception.
*/
R execute();

/**
* Cancels a synchronous execution by calling the most recent {@link ExecutionContext#onCancel(CheckedRunnable)
* cancelCallback} that was registered during the execution and marking the execution as cancelled. The execution is
* still allowed to complete and return a result. In addition to using a {@link ExecutionContext#onCancel(CheckedRunnable)
* cancelCallback}, executions can cooperate with cancellation by checking {@link ExecutionContext#isCancelled()}.
*/
void cancel();
}
40 changes: 40 additions & 0 deletions core/src/main/java/dev/failsafe/CallImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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;

/**
* A call implementation that delegates to an execution.
*
* @param <R> result type
* @author Jonathan Halterman
*/
class CallImpl<R> implements Call<R> {
private volatile SyncExecutionImpl<R> execution;

void setExecution(SyncExecutionImpl<R> execution) {
this.execution = execution;
}

@Override
public R execute() {
return execution.executeSync();
}

@Override
public void cancel() {
execution.cancel();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package dev.failsafe;

import dev.failsafe.function.CheckedRunnable;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

/**
* Contextual execution information.
Expand All @@ -24,6 +27,12 @@
* @author Jonathan Halterman
*/
public interface ExecutionContext<R> {
/**
* Sets the {@code cancelCallback} to be called if the execution is cancelled, such as by the resulting {@link Call}
* or {@link CompletableFuture}, or a {@link Timeout}. Any exception thrown by the {@code cancelCallback} is ignored.
*/
void onCancel(CheckedRunnable cancelCallback);

/**
* Returns the elapsed time since initial execution began.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package dev.failsafe;

import dev.failsafe.function.CheckedRunnable;
import dev.failsafe.internal.util.Assert;
import dev.failsafe.spi.ExecutionInternal;
import dev.failsafe.spi.ExecutionResult;
Expand Down Expand Up @@ -53,6 +54,8 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
volatile Duration attemptStartTime;
// The index of a PolicyExecutor that cancelled the execution. Integer.MIN_VALUE represents non-cancelled.
volatile int cancelledIndex = Integer.MIN_VALUE;
// The user-provided callback to be called when an execution is cancelled
volatile CheckedRunnable cancelCallback;
// Whether the execution has pre-executed indicating it has started
private volatile boolean preExecuted;
// Whether the execution attempt has been recorded
Expand Down Expand Up @@ -102,6 +105,11 @@ public ExecutionResult<R> getResult() {
return result;
}

@Override
public void onCancel(CheckedRunnable cancelCallback) {
this.cancelCallback = cancelCallback;
}

@Override
public synchronized void preExecute() {
if (!preExecuted) {
Expand Down Expand Up @@ -153,14 +161,30 @@ synchronized ExecutionResult<R> postExecute(ExecutionResult<R> result) {
return result;
}

/** Called by users. */
@Override
public void cancel() {
cancelledIndex = Integer.MAX_VALUE;
if (!isCancelled()) {
cancelledIndex = Integer.MAX_VALUE;
if (cancelCallback != null) {
try {
cancelCallback.run();
} catch (Throwable ignore) {
}
}
}
}

/** Called by policies. */
@Override
public void cancel(PolicyExecutor<R> policyExecutor) {
cancelledIndex = policyExecutor.getPolicyIndex();
if (cancelCallback != null) {
try {
cancelCallback.run();
} catch (Throwable ignore) {
}
}
}

@Override
Expand Down
File renamed without changes.
Loading

0 comments on commit dc6b459

Please sign in to comment.