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

Properly removes a channel from the cache #7952

Merged
merged 1 commit into from
Nov 7, 2023
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 @@ -89,10 +89,10 @@ class WebClientRequestBuilderImpl implements WebClientRequestBuilder {

private static final Logger LOGGER = Logger.getLogger(WebClientRequestBuilderImpl.class.getName());

private static final Map<ConnectionIdent, Set<ChannelRecord>> CHANNEL_CACHE = new ConcurrentHashMap<>();
private static final List<DataPropagationProvider> PROPAGATION_PROVIDERS = HelidonServiceLoader
.builder(ServiceLoader.load(DataPropagationProvider.class)).build().asList();

static final Map<ConnectionIdent, Set<ChannelRecord>> CHANNEL_CACHE = new ConcurrentHashMap<>();
static final AttributeKey<WebClientRequestImpl> REQUEST = AttributeKey.valueOf("request");
static final AttributeKey<CompletableFuture<WebClientServiceResponse>> RECEIVED = AttributeKey.valueOf("received");
static final AttributeKey<CompletableFuture<WebClientServiceResponse>> COMPLETED = AttributeKey.valueOf("completed");
Expand Down Expand Up @@ -242,7 +242,16 @@ static void removeChannelFromCache(ConnectionIdent key, Channel channel) {
LOGGER.finest(() -> "Removing from channel cache. Connection ident -> " + key
+ ", channel -> " + channel.hashCode());
}
CHANNEL_CACHE.get(key).remove(new ChannelRecord(channel));
Set<ChannelRecord> channelSet = CHANNEL_CACHE.get(key);
if (channelSet != null) {
// remove entry from set
channelSet.remove(new ChannelRecord(channel));

// remove set from map if empty
if (channelSet.isEmpty()) {
CHANNEL_CACHE.remove(key);
}
}
}

@Override
Expand Down Expand Up @@ -958,17 +967,17 @@ HttpRequest.Path createSubpath(String path, String rawPath,
}
}

private static class ChannelRecord {
static class ChannelRecord {

private final ChannelFuture channelFuture;
private final Channel channel;

private ChannelRecord(ChannelFuture channelFuture) {
ChannelRecord(ChannelFuture channelFuture) {
this.channelFuture = channelFuture;
this.channel = channelFuture.channel();
}

private ChannelRecord(Channel channel) {
ChannelRecord(Channel channel) {
this.channelFuture = null;
this.channel = channel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.webclient;

import java.util.HashSet;

import io.netty.channel.Channel;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;

class WebClientRequestBuilderImplTest {

@Test
void testChannelRemovalFromCache() {
var conn = Mockito.mock(WebClientRequestBuilderImpl.ConnectionIdent.class);
var channel = Mockito.mock(Channel.class);
HashSet<WebClientRequestBuilderImpl.ChannelRecord> set = new HashSet<>();
set.add(new WebClientRequestBuilderImpl.ChannelRecord(channel));
WebClientRequestBuilderImpl.CHANNEL_CACHE.put(conn, set);
WebClientRequestBuilderImpl.removeChannelFromCache(conn, channel);
assertThat(WebClientRequestBuilderImpl.CHANNEL_CACHE.get(conn), nullValue());
}
}
Loading