Skip to content

Commit

Permalink
try moving DefaultClientRequestContext to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Jun 22, 2022
1 parent 0366c8e commit a4da669
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.logging.ClientConnectionTimings;
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.common.util.Unwrappable;
import com.linecorp.armeria.internal.client.ClientThreadLocalState;

/**
* Creates a new client that connects to a specified {@link URI}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.linecorp.armeria.common.stream.CancelledSubscriptionException;
import com.linecorp.armeria.common.stream.StreamWriter;
import com.linecorp.armeria.common.util.Exceptions;
import com.linecorp.armeria.internal.client.ClientRequestContextExtension;
import com.linecorp.armeria.internal.common.CancellationScheduler;
import com.linecorp.armeria.internal.common.CancellationScheduler.CancellationTask;
import com.linecorp.armeria.internal.common.InboundTrafficController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.AbstractUnwrappable;
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* under the License.
*/

package com.linecorp.armeria.client;
package com.linecorp.armeria.internal.client;

import java.util.concurrent.CompletableFuture;

import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.logging.RequestLog;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 LINE Corporation
* Copyright 2022 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.client;
package com.linecorp.armeria.internal.client;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,21 +22,23 @@

import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientRequestContextCaptor;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.SafeCloseable;

import io.netty.util.concurrent.FastThreadLocal;

final class ClientThreadLocalState {
public final class ClientThreadLocalState {

private static final FastThreadLocal<ClientThreadLocalState> threadLocalState = new FastThreadLocal<>();

@Nullable
static ClientThreadLocalState get() {
public static ClientThreadLocalState get() {
return threadLocalState.get();
}

static ClientThreadLocalState maybeCreate() {
public static ClientThreadLocalState maybeCreate() {
ClientThreadLocalState state = threadLocalState.get();
if (state == null) {
state = new ClientThreadLocalState();
Expand All @@ -51,14 +53,14 @@ static ClientThreadLocalState maybeCreate() {
@Nullable
private DefaultClientRequestContextCaptor pendingContextCaptor;

void add(Consumer<? super ClientRequestContext> customizer) {
public void add(Consumer<? super ClientRequestContext> customizer) {
if (customizers == null) {
customizers = new ArrayList<>();
}
customizers.add(customizer);
}

void remove(Consumer<? super ClientRequestContext> customizer) {
public void remove(Consumer<? super ClientRequestContext> customizer) {
if (customizers != null) {
// Iterate in reverse order since we add/remove in LIFO order.
for (int i = customizers.size() - 1; i >= 0; i--) {
Expand All @@ -74,7 +76,7 @@ void remove(Consumer<? super ClientRequestContext> customizer) {
reportThreadSafetyViolation();
}

ClientRequestContextCaptor newContextCaptor() {
public ClientRequestContextCaptor newContextCaptor() {
final DefaultClientRequestContextCaptor oldPendingContextCaptor = pendingContextCaptor;
return pendingContextCaptor = new DefaultClientRequestContextCaptor(oldPendingContextCaptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.linecorp.armeria.client.Client;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientRequestContextExtension;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.UnprocessedRequestException;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 LINE Corporation
* Copyright 2022 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -13,11 +13,10 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.client;
package com.linecorp.armeria.internal.client;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.linecorp.armeria.client.DefaultWebClient.pathWithQuery;
import static com.linecorp.armeria.internal.common.ArmeriaHttpUtil.isAbsoluteUri;
import static java.util.Objects.requireNonNull;

Expand All @@ -34,6 +33,14 @@

import javax.net.ssl.SSLSession;

import com.google.common.base.Strings;

import com.linecorp.armeria.client.Client;
import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.RequestOptions;
import com.linecorp.armeria.client.UnprocessedRequestException;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
import com.linecorp.armeria.common.ContextAwareEventLoop;
import com.linecorp.armeria.common.Flags;
Expand Down Expand Up @@ -145,7 +152,7 @@ public final class DefaultClientRequestContext
* @param requestStartTimeMicros the number of microseconds since the epoch,
* e.g. {@code System.currentTimeMillis() * 1000}.
*/
DefaultClientRequestContext(
public DefaultClientRequestContext(
EventLoop eventLoop, MeterRegistry meterRegistry, SessionProtocol sessionProtocol,
RequestId id, HttpMethod method, String path, @Nullable String query, @Nullable String fragment,
ClientOptions options, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq,
Expand Down Expand Up @@ -803,4 +810,14 @@ private String toStringSlow(@Nullable Channel ch, @Nullable RequestLogAccess par
return buf.toString();
}
}

static String pathWithQuery(URI uri, @Nullable String query) {
String path = uri.getRawPath();
if (Strings.isNullOrEmpty(path)) {
path = query == null ? "/" : "/?" + query;
} else if (query != null) {
path = path + '?' + query;
}
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.common.collect.MapMaker;
import com.google.errorprone.annotations.MustBeClosed;

import com.linecorp.armeria.client.DefaultClientRequestContext;
import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.RequestContext;
Expand All @@ -39,6 +38,7 @@
import com.linecorp.armeria.common.RequestContextStorageProvider;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;
import com.linecorp.armeria.server.DefaultServiceRequestContext;

import io.netty.channel.ChannelFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.internal.client.ClientThreadLocalState;

class ClientRequestContextInitFailureTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.google.errorprone.annotations.MustBeClosed;

import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientRequestContextExtension;
import com.linecorp.armeria.client.DefaultClientRequestContext;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.internal.client.ClientRequestContextExtension;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;
import com.linecorp.armeria.server.DefaultServiceRequestContext;
import com.linecorp.armeria.server.ServiceRequestContext;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* Copyright 2017 LINE Corporation
* Copyright 2022 LINE Corporation
*
* LINE Corporation licenses this file to you 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
* https://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 com.linecorp.armeria.client;
package com.linecorp.armeria.internal.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -31,6 +31,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientRequestContextCaptor;
import com.linecorp.armeria.client.Clients;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.RequestOptions;
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import com.linecorp.armeria.client.ClientBuilderParams;
import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.DefaultClientRequestContext;
import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.client.RequestOptions;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
Expand All @@ -40,6 +39,7 @@
import com.linecorp.armeria.common.logging.RequestLogProperty;
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.common.util.Unwrappable;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;

import io.grpc.CallCredentials;
import io.grpc.CallOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linecorp.armeria.client.DefaultClientRequestContext;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
Expand All @@ -60,6 +59,7 @@
import com.linecorp.armeria.common.stream.SubscriptionOption;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.common.util.TimeoutMode;
import com.linecorp.armeria.internal.client.DefaultClientRequestContext;
import com.linecorp.armeria.internal.client.endpoint.StaticEndpointGroup;
import com.linecorp.armeria.internal.client.grpc.protocol.InternalGrpcWebUtil;
import com.linecorp.armeria.internal.common.grpc.ForwardingCompressor;
Expand Down

0 comments on commit a4da669

Please sign in to comment.