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

Introduce the CancellationScheduler interface #5220

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -15,6 +15,7 @@
*/
package com.linecorp.armeria.client;

import static com.linecorp.armeria.internal.common.CancellationScheduler.noopCancellationTask;
import static java.util.Objects.requireNonNull;

import java.net.InetSocketAddress;
Expand All @@ -35,12 +36,10 @@
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;
import com.linecorp.armeria.internal.common.CancellationScheduler;
import com.linecorp.armeria.internal.common.CancellationScheduler.CancellationTask;

import io.micrometer.core.instrument.MeterRegistry;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.ImmediateEventExecutor;

/**
* Builds a new {@link ClientRequestContext}. Note that it is not usually required to create a new context by
Expand All @@ -49,26 +48,11 @@
*/
public final class ClientRequestContextBuilder extends AbstractRequestContextBuilder {

private static final CancellationTask noopCancellationTask = new CancellationTask() {
@Override
public boolean canSchedule() {
return true;
}

@Override
public void run(Throwable cause) { /* no-op */ }
};

/**
* A cancellation scheduler that has been finished.
*/
static final CancellationScheduler noopResponseCancellationScheduler = new CancellationScheduler(0);

static {
noopResponseCancellationScheduler
.init(ImmediateEventExecutor.INSTANCE, noopCancellationTask, 0, /* server */ false);
noopResponseCancellationScheduler.finishNow();
}
private static final CancellationScheduler
finishedResponseCancellationScheduler = CancellationScheduler.finished(false);

@Nullable
private Endpoint endpoint;
Expand Down Expand Up @@ -136,9 +120,9 @@ public ClientRequestContext build() {

final CancellationScheduler responseCancellationScheduler;
if (timedOut()) {
responseCancellationScheduler = noopResponseCancellationScheduler;
responseCancellationScheduler = finishedResponseCancellationScheduler;
} else {
responseCancellationScheduler = new CancellationScheduler(0);
responseCancellationScheduler = CancellationScheduler.of(0);
final CountDownLatch latch = new CountDownLatch(1);
eventLoop().execute(() -> {
responseCancellationScheduler.init(eventLoop(), noopCancellationTask, 0, /* server */ false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.linecorp.armeria.client;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.linecorp.armeria.client.ClientRequestContextBuilder.noopResponseCancellationScheduler;
import static com.linecorp.armeria.common.SessionProtocol.H1;
import static com.linecorp.armeria.common.SessionProtocol.H1C;
import static com.linecorp.armeria.common.SessionProtocol.H2;
Expand Down Expand Up @@ -57,6 +56,7 @@
import com.linecorp.armeria.internal.client.UserAgentUtil;
import com.linecorp.armeria.internal.common.ArmeriaHttp2HeadersDecoder;
import com.linecorp.armeria.internal.common.ArmeriaHttpUtil;
import com.linecorp.armeria.internal.common.CancellationScheduler;
import com.linecorp.armeria.internal.common.ReadSuppressingHandler;
import com.linecorp.armeria.internal.common.TrafficLoggingHandler;
import com.linecorp.armeria.internal.common.util.ChannelUtil;
Expand Down Expand Up @@ -547,7 +547,7 @@ public void onComplete() {}
com.linecorp.armeria.common.HttpMethod.OPTIONS,
RequestTarget.forClient("*"), ClientOptions.of(),
HttpRequest.of(com.linecorp.armeria.common.HttpMethod.OPTIONS, "*"),
null, REQUEST_OPTIONS_FOR_UPGRADE_REQUEST, noopResponseCancellationScheduler,
null, REQUEST_OPTIONS_FOR_UPGRADE_REQUEST, CancellationScheduler.noop(),
System.nanoTime(), SystemInfo.currentTimeMicros());

// NB: No need to set the response timeout because we have session creation timeout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private DefaultClientRequestContext(
responseTimeoutMillis = options().responseTimeoutMillis();
}
this.responseCancellationScheduler =
new CancellationScheduler(TimeUnit.MILLISECONDS.toNanos(responseTimeoutMillis));
CancellationScheduler.of(TimeUnit.MILLISECONDS.toNanos(responseTimeoutMillis));
} else {
this.responseCancellationScheduler = responseCancellationScheduler;
}
Expand Down Expand Up @@ -504,7 +504,7 @@ private DefaultClientRequestContext(DefaultClientRequestContext ctx,
log = RequestLog.builder(this);
log.startRequest();
responseCancellationScheduler =
new CancellationScheduler(TimeUnit.MILLISECONDS.toNanos(ctx.responseTimeoutMillis()));
CancellationScheduler.of(TimeUnit.MILLISECONDS.toNanos(ctx.responseTimeoutMillis()));
writeTimeoutMillis = ctx.writeTimeoutMillis();
maxResponseLength = ctx.maxResponseLength();

Expand Down
Loading