Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
netty: implement UdsNameResolver and UdsNettyChannelProvider (#9113)
* netty: implement UdsNameResolver and UdsNettyChannelProvider When the scheme is "unix:" we get the UdsNettyChannelProvider to create a NettyChannelBuilder with DomainSocketAddress type and other related params needed for UDS sockets
- Loading branch information
1 parent
cb61a5e
commit 41c027c
Showing
13 changed files
with
662 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2022 The gRPC Authors | ||
* | ||
* 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.grpc.netty; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.grpc.EquivalentAddressGroup; | ||
import io.grpc.NameResolver; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
final class UdsNameResolver extends NameResolver { | ||
private NameResolver.Listener2 listener; | ||
private final String authority; | ||
|
||
UdsNameResolver(String authority, String targetPath) { | ||
checkArgument(authority == null, "non-null authority not supported"); | ||
this.authority = targetPath; | ||
} | ||
|
||
@Override | ||
public String getServiceAuthority() { | ||
return this.authority; | ||
} | ||
|
||
@Override | ||
public void start(Listener2 listener) { | ||
Preconditions.checkState(this.listener == null, "already started"); | ||
this.listener = checkNotNull(listener, "listener"); | ||
resolve(); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
resolve(); | ||
} | ||
|
||
private void resolve() { | ||
ResolutionResult.Builder resolutionResultBuilder = ResolutionResult.newBuilder(); | ||
List<EquivalentAddressGroup> servers = new ArrayList<>(1); | ||
servers.add(new EquivalentAddressGroup(new DomainSocketAddress(authority))); | ||
resolutionResultBuilder.setAddresses(Collections.unmodifiableList(servers)); | ||
listener.onResult(resolutionResultBuilder.build()); | ||
} | ||
|
||
@Override | ||
public void shutdown() {} | ||
} |
71 changes: 71 additions & 0 deletions
71
netty/src/main/java/io/grpc/netty/UdsNameResolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2022 The gRPC Authors | ||
* | ||
* 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.grpc.netty; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.grpc.Internal; | ||
import io.grpc.NameResolver; | ||
import io.grpc.NameResolverProvider; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
@Internal | ||
public final class UdsNameResolverProvider extends NameResolverProvider { | ||
|
||
private static final String SCHEME = "unix"; | ||
|
||
@Override | ||
public UdsNameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
if (SCHEME.equals(targetUri.getScheme())) { | ||
return new UdsNameResolver(targetUri.getAuthority(), getTargetPathFromUri(targetUri)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
static String getTargetPathFromUri(URI targetUri) { | ||
Preconditions.checkArgument(SCHEME.equals(targetUri.getScheme()), "scheme must be " + SCHEME); | ||
String targetPath = targetUri.getPath(); | ||
if (targetPath == null) { | ||
targetPath = Preconditions.checkNotNull(targetUri.getSchemeSpecificPart(), "targetPath"); | ||
} | ||
return targetPath; | ||
} | ||
|
||
@Override | ||
public String getDefaultScheme() { | ||
return SCHEME; | ||
} | ||
|
||
@Override | ||
protected boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected int priority() { | ||
return 3; | ||
} | ||
|
||
@Override | ||
protected Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes() { | ||
return Collections.singleton(DomainSocketAddress.class); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
netty/src/main/java/io/grpc/netty/UdsNettyChannelProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2015 The gRPC Authors | ||
* | ||
* 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.grpc.netty; | ||
|
||
import io.grpc.CallCredentials; | ||
import io.grpc.ChannelCredentials; | ||
import io.grpc.InsecureChannelCredentials; | ||
import io.grpc.Internal; | ||
import io.grpc.ManagedChannelProvider; | ||
import io.grpc.internal.SharedResourcePool; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** Provider for {@link NettyChannelBuilder} instances for UDS channels. */ | ||
@Internal | ||
public final class UdsNettyChannelProvider extends ManagedChannelProvider { | ||
|
||
@Override | ||
public boolean isAvailable() { | ||
return (Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE != null); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 3; | ||
} | ||
|
||
@Override | ||
public NettyChannelBuilder builderForAddress(String name, int port) { | ||
throw new UnsupportedOperationException("host:port not supported"); | ||
} | ||
|
||
@Override | ||
public NettyChannelBuilder builderForTarget(String target) { | ||
ChannelCredentials creds = InsecureChannelCredentials.create(); | ||
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds); | ||
if (result.error != null) { | ||
throw new RuntimeException(result.error); | ||
} | ||
return getNettyChannelBuilder(target, creds, null, result.negotiator); | ||
} | ||
|
||
@Override | ||
public NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds) { | ||
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds); | ||
if (result.error != null) { | ||
return NewChannelBuilderResult.error(result.error); | ||
} | ||
return NewChannelBuilderResult.channelBuilder( | ||
getNettyChannelBuilder(target, creds, result.callCredentials, result.negotiator)); | ||
} | ||
|
||
private static NettyChannelBuilder getNettyChannelBuilder( | ||
String target, | ||
ChannelCredentials creds, | ||
CallCredentials callCredentials, | ||
ProtocolNegotiator.ClientFactory negotiator) { | ||
if (Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE == null) { | ||
throw new IllegalStateException("Epoll is not available"); | ||
} | ||
String targetPath = UdsNameResolverProvider.getTargetPathFromUri(URI.create(target)); | ||
NettyChannelBuilder builder = | ||
new NettyChannelBuilder( | ||
new DomainSocketAddress(targetPath), creds, callCredentials, negotiator); | ||
builder = | ||
builder | ||
.eventLoopGroupPool( | ||
SharedResourcePool.forResource(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP)) | ||
.channelType(Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE); | ||
return builder; | ||
} | ||
|
||
@Override | ||
protected Collection<Class<? extends SocketAddress>> getSupportedSocketAddressTypes() { | ||
return Collections.singleton(DomainSocketAddress.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
netty/src/main/resources/META-INF/services/io.grpc.ManagedChannelProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
io.grpc.netty.NettyChannelProvider | ||
io.grpc.netty.UdsNettyChannelProvider |
1 change: 1 addition & 0 deletions
1
netty/src/main/resources/META-INF/services/io.grpc.NameResolverProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.grpc.netty.UdsNameResolverProvider |
Oops, something went wrong.