Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for anonymous in-process servers. (#8589)
Support anonymous in-process servers, and InProcessChannelBuilder.forTarget. Anonymous servers aren't registered statically, meaning they can't be looked up by name. Only the AnonymousInProcessSocketAddress passed to InProcessServerBuilder.forAddress(), (or subsequently fetched from Server.getListenSockets()) can be used to connect to the server. Supporting InProcessChannelBuilder.forTarget is particularly useful for production Android usage of in-process servers, where process startup latency is crucial. A custom name resolver can be used to create the server instance on demand without directly impacting the startup latency of in-process gRPC clients. Together, these features support a more-standard approach to "OnDeviceServer" referenced in gRFC L73. https://github.com/grpc/proposal/blob/master/L73-java-binderchannel.md#ondeviceserver
- Loading branch information
Showing
8 changed files
with
316 additions
and
46 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
core/src/main/java/io/grpc/inprocess/AnonymousInProcessSocketAddress.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,58 @@ | ||
/* | ||
* Copyright 2021 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.inprocess; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import io.grpc.ExperimentalApi; | ||
import java.io.IOException; | ||
import java.net.SocketAddress; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.GuardedBy; | ||
|
||
/** | ||
* Custom SocketAddress class for {@link InProcessTransport}, for | ||
* a server which can only be referenced via this address instance. | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8626") | ||
public final class AnonymousInProcessSocketAddress extends SocketAddress { | ||
private static final long serialVersionUID = -8567592561863414695L; | ||
|
||
@Nullable | ||
@GuardedBy("this") | ||
private InProcessServer server; | ||
|
||
/** Creates a new AnonymousInProcessSocketAddress. */ | ||
public AnonymousInProcessSocketAddress() { } | ||
|
||
@Nullable | ||
synchronized InProcessServer getServer() { | ||
return server; | ||
} | ||
|
||
synchronized void setServer(InProcessServer server) throws IOException { | ||
if (this.server != null) { | ||
throw new IOException("Server instance already registered"); | ||
} | ||
this.server = server; | ||
} | ||
|
||
synchronized void clearServer(InProcessServer server) { | ||
checkState(this.server == server); | ||
this.server = null; | ||
} | ||
} |
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
Oops, something went wrong.