Skip to content

Commit

Permalink
Fix NPE when address is null
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Dec 2, 2024
1 parent 1ac934b commit 3d9dfd4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected URL setThreadNameIfAbsent(URL url, String executorCacheKey) {

@Override
protected String getProviderKey(URL url) {
if (url.hasAttribute(SERVICE_EXECUTOR)) {
if (url.getAttributes().containsKey(SERVICE_EXECUTOR)) {
return url.getServiceKey();
} else {
return super.getProviderKey(url);
Expand All @@ -55,7 +55,7 @@ protected String getProviderKey(URL url) {

@Override
protected String getProviderKey(ProviderModel providerModel, URL url) {
if (url.hasAttribute(SERVICE_EXECUTOR)) {
if (url.getAttributes().containsKey(SERVICE_EXECUTOR)) {
return providerModel.getServiceKey();
} else {
return super.getProviderKey(url);
Expand All @@ -64,7 +64,7 @@ protected String getProviderKey(ProviderModel providerModel, URL url) {

@Override
protected ExecutorService createExecutor(URL url) {
Object executor = url.getAttribute(SERVICE_EXECUTOR);
Object executor = url.getAttributes().get(SERVICE_EXECUTOR);
if (executor instanceof ExecutorService) {
return (ExecutorService) executor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ private void processServiceExecutor(URL url) {
* and obtained it in IsolationExecutorRepository#createExecutor method
*/
providerModel.getServiceMetadata().addAttribute(SERVICE_EXECUTOR, getExecutor());
url.putAttribute(SERVICE_EXECUTOR, getExecutor());
url.getAttributes().put(SERVICE_EXECUTOR, getExecutor());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.http3.Http3SslContexts;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

Expand All @@ -44,20 +45,21 @@

public final class NettyHttp3ConnectionClient extends AbstractNettyConnectionClient {

private final Consumer<ChannelPipeline> pipelineConfigurator;
private AtomicReference<io.netty.channel.Channel> datagramChannel;
private QuicChannelBootstrap bootstrap;
private Consumer<ChannelPipeline> pipelineConfigurator;

@SuppressWarnings("unchecked")
public NettyHttp3ConnectionClient(URL url, ChannelHandler handler) throws RemotingException {
super(url, handler);
pipelineConfigurator = (Consumer<ChannelPipeline>) getUrl().getAttribute(PIPELINE_CONFIGURATOR_KEY);
Objects.requireNonNull(pipelineConfigurator, "pipelineConfigurator should be set");
}

@Override
@SuppressWarnings("unchecked")
protected void initConnectionClient() {
super.initConnectionClient();
datagramChannel = new AtomicReference<>();
pipelineConfigurator = (Consumer<ChannelPipeline>) getUrl().getAttribute(PIPELINE_CONFIGURATOR_KEY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

import io.netty.bootstrap.Bootstrap;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class NettyHttp3Server extends AbstractServer {
public NettyHttp3Server(URL url, ChannelHandler handler) throws RemotingException {
super(url, ChannelHandlers.wrap(handler, url));
pipelineConfigurator = (Consumer<ChannelPipeline>) getUrl().getAttribute(PIPELINE_CONFIGURATOR_KEY);
Objects.requireNonNull(pipelineConfigurator, "pipelineConfigurator should be set");
serverShutdownTimeoutMills = ConfigurationUtils.getServerShutdownTimeout(getUrl().getOrDefaultModuleModel());
}

Expand All @@ -83,8 +85,8 @@ protected void doOpen() throws Throwable {
@Override
protected void initChannel(QuicChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(nettyServerHandler);
pipelineConfigurator.accept(pipeline);
pipeline.addLast(nettyServerHandler);
}
})
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,6 @@ public static InetSocketAddress getLocalAddress(Channel channel) {
return (InetSocketAddress) channel.localAddress();
}

public static String getRemoteAddressKey(Channel channel) {
InetSocketAddress address;
for (int i = 0, size = ACCESSORS.size(); i < size; i++) {
ChannelAddressAccessor accessor = ACCESSORS.get(i);
address = accessor.getRemoteAddress(channel);
if (address != null) {
return accessor.getProtocol() + ' ' + toAddressString(address);
}
}
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
if (remoteAddress == null) {
return "UNKNOWN";
}
return toAddressString(remoteAddress);
}

public static String getLocalAddressKey(Channel channel) {
InetSocketAddress address;
for (int i = 0, size = ACCESSORS.size(); i < size; i++) {
ChannelAddressAccessor accessor = ACCESSORS.get(i);
address = accessor.getLocalAddress(channel);
if (address != null) {
return accessor.getProtocol() + ' ' + toAddressString(address);
}
}
SocketAddress localAddress = channel.localAddress();
if (localAddress == null) {
return "UNKNOWN";
}
return toAddressString((InetSocketAddress) localAddress);
}

static void initAddressIfNecessary(NettyChannel nettyChannel) {
Channel channel = nettyChannel.getNioChannel();
SocketAddress address = channel.localAddress();
Expand Down Expand Up @@ -122,12 +90,18 @@ static InetSocketAddress getRemoteAddress(NettyChannel channel) {

static String getLocalAddressKey(NettyChannel channel) {
InetSocketAddress address = getLocalAddress(channel);
if (address == null) {
return "UNKNOWN";
}
String protocol = (String) channel.getAttribute(PROTOCOL_KEY);
return protocol == null ? toAddressString(address) : protocol + ' ' + toAddressString(address);
}

static String getRemoteAddressKey(NettyChannel channel) {
InetSocketAddress address = getRemoteAddress(channel);
if (address == null) {
return "UNKNOWN";
}
String protocol = (String) channel.getAttribute(PROTOCOL_KEY);
return protocol == null ? toAddressString(address) : protocol + ' ' + toAddressString(address);
}
Expand Down

0 comments on commit 3d9dfd4

Please sign in to comment.