Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete DefaultClientRequestContext.whenIntialized() after fully initializing a context #3636

Merged
merged 2 commits into from
Jun 18, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,32 +281,13 @@ private CompletableFuture<Boolean> initEndpointGroup(EndpointGroup endpointGroup
}).thenCompose(Function.identity());
}

private CompletableFuture<Boolean> initFuture(boolean success, @Nullable EventLoop acquiredEventLoop) {
CompletableFuture<Boolean> whenInitialized = this.whenInitialized;
if (whenInitialized == null) {
final CompletableFuture<Boolean> future;
if (acquiredEventLoop == null) {
future = UnmodifiableFuture.completedFuture(success);
} else {
future = CompletableFuture.supplyAsync(() -> success, acquiredEventLoop);
}
if (whenInitializedUpdater.compareAndSet(this, null, future)) {
return future;
}
whenInitialized = this.whenInitialized;
}

final CompletableFuture<Boolean> finalWhenInitialized = whenInitialized;
if (finalWhenInitialized.isDone()) {
return finalWhenInitialized;
}

private static CompletableFuture<Boolean> initFuture(boolean success,
@Nullable EventLoop acquiredEventLoop) {
if (acquiredEventLoop == null) {
finalWhenInitialized.complete(success);
return UnmodifiableFuture.completedFuture(success);
} else {
acquiredEventLoop.execute(() -> finalWhenInitialized.complete(success));
return CompletableFuture.supplyAsync(() -> success, acquiredEventLoop);
}
return finalWhenInitialized;
}

/**
Expand All @@ -329,6 +310,21 @@ public CompletableFuture<Boolean> whenInitialized() {
}
}

/**
* Completes the {@link #whenInitialized()} with the specified value.
*/
public void finishInitialization(boolean success) {
final CompletableFuture<Boolean> whenInitialized = this.whenInitialized;
if (whenInitialized != null) {
whenInitialized.complete(success);
} else {
if (!whenInitializedUpdater.compareAndSet(this, null,
UnmodifiableFuture.completedFuture(success))) {
this.whenInitialized.complete(success);
}
}
}

private void updateEndpoint(@Nullable Endpoint endpoint) {
this.endpoint = endpoint;
autoFillSchemeAndAuthority();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ O initContextAndExecuteWithFallback(
requireNonNull(futureConverter, "futureConverter");
requireNonNull(errorResponseFactory, "errorResponseFactory");

boolean initialized = false;
boolean success = false;
try {
endpointGroup = mapEndpoint(ctx, endpointGroup);
final CompletableFuture<Boolean> initFuture = ctx.init(endpointGroup);
if (initFuture.isDone()) {
initialized = initFuture.isDone();
if (initialized) {
// Initialization has been done immediately.
final boolean success;
try {
success = initFuture.get();
} catch (Exception e) {
Expand All @@ -66,22 +68,28 @@ O initContextAndExecuteWithFallback(

return initContextAndExecuteWithFallback(delegate, ctx, errorResponseFactory, success);
} else {
return futureConverter.apply(initFuture.handle((success, cause) -> {
return futureConverter.apply(initFuture.handle((success0, cause) -> {
try {
if (cause != null) {
throw UnprocessedRequestException.of(Exceptions.peel(cause));
}

return initContextAndExecuteWithFallback(delegate, ctx, errorResponseFactory, success);
return initContextAndExecuteWithFallback(delegate, ctx, errorResponseFactory, success0);
} catch (Throwable t) {
fail(ctx, t);
return errorResponseFactory.apply(ctx, t);
} finally {
ctx.finishInitialization(success0);
}
}));
}
} catch (Throwable cause) {
fail(ctx, cause);
return errorResponseFactory.apply(ctx, cause);
} finally {
if (initialized) {
ctx.finishInitialization(success);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.common.JacksonModuleProvider;

public final class CustomJacksonModuleProvider implements JacksonModuleProvider {
@Override
public List<Module> modules() {
Expand Down