-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4.x: Adds support to iterate over URIs when connecting to a gRPC serv…
…ice (#9285) (#9300) Adds support to iterate over URIs when connecting to a gRPC service. Defines a new interface ClientUriSupplier that can be used to dynamically provide a list of URIs to connect to. Each gRPC call will retrieve the next URI from the supplier. Co-authored-by: Santiago Pericas-Geertsen <[email protected]>
- Loading branch information
Showing
12 changed files
with
523 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2024 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. | ||
--> | ||
|
||
<FindBugsFilter | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://github.com/spotbugs/filter/3.0.0" | ||
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd"> | ||
<Match> | ||
<Class name="io.helidon.webclient.grpc.ClientUriSuppliers$RoundRobinSupplier" /> | ||
<Bug pattern="IT_NO_SUCH_ELEMENT" /> | ||
</Match> | ||
<Match> | ||
<Class name="io.helidon.webclient.grpc.ClientUriSuppliers$SingleSupplier" /> | ||
<Bug pattern="IT_NO_SUCH_ELEMENT" /> | ||
</Match> | ||
<Match> | ||
<Class name="io.helidon.webclient.grpc.ClientUriSuppliers$RandomSupplier" /> | ||
<Bug pattern="IT_NO_SUCH_ELEMENT" /> | ||
</Match> | ||
</FindBugsFilter> |
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
31 changes: 31 additions & 0 deletions
31
webclient/grpc/src/main/java/io/helidon/webclient/grpc/ClientUriSupplier.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,31 @@ | ||
/* | ||
* Copyright (c) 2024 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.grpc; | ||
|
||
import java.util.Iterator; | ||
|
||
import io.helidon.webclient.api.ClientUri; | ||
|
||
/** | ||
* Interface implemented by all client URI suppliers. | ||
*/ | ||
public interface ClientUriSupplier extends Iterator<ClientUri>, Iterable<ClientUri> { | ||
|
||
@Override | ||
default Iterator<ClientUri> iterator() { | ||
return this; | ||
} | ||
} |
203 changes: 203 additions & 0 deletions
203
webclient/grpc/src/main/java/io/helidon/webclient/grpc/ClientUriSuppliers.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,203 @@ | ||
/* | ||
* Copyright (c) 2024 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.grpc; | ||
|
||
import java.net.URI; | ||
import java.security.SecureRandom; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import io.helidon.webclient.api.ClientUri; | ||
|
||
/** | ||
* Some popular implementations of the {@link io.helidon.webclient.grpc.ClientUriSupplier} | ||
* interface. | ||
*/ | ||
public class ClientUriSuppliers { | ||
|
||
/** | ||
* Supplies an iterator that returns URIs chosen in order from | ||
* first to last. | ||
*/ | ||
public static class OrderedSupplier implements ClientUriSupplier { | ||
|
||
private final Iterator<ClientUri> clientUris; | ||
|
||
/** | ||
* Creates an ordered supplier. | ||
* | ||
* @param clientUris array of client URIs | ||
* @return new supplier | ||
*/ | ||
public static OrderedSupplier create(ClientUri... clientUris) { | ||
return new OrderedSupplier(List.of(clientUris)); | ||
} | ||
|
||
/** | ||
* Creates an ordered supplier. | ||
* | ||
* @param clientUris collection of client URIs | ||
* @return new supplier | ||
*/ | ||
public static OrderedSupplier create(Collection<ClientUri> clientUris) { | ||
return new OrderedSupplier(clientUris); | ||
} | ||
|
||
protected OrderedSupplier(Collection<ClientUri> clientUris) { | ||
this.clientUris = List.copyOf(clientUris).iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return clientUris.hasNext(); | ||
} | ||
|
||
@Override | ||
public ClientUri next() { | ||
return clientUris.next(); | ||
} | ||
} | ||
|
||
/** | ||
* Supplies a neven-ending iterator that returns URIs chosen using | ||
* a round-robin strategy. | ||
*/ | ||
public static class RoundRobinSupplier implements ClientUriSupplier { | ||
|
||
private int next; | ||
private final ClientUri[] clientUris; | ||
|
||
/** | ||
* Creates a round-robin supplier. | ||
* | ||
* @param clientUris array of client URIs | ||
* @return new supplier | ||
*/ | ||
public static RoundRobinSupplier create(ClientUri... clientUris) { | ||
return new RoundRobinSupplier(clientUris); | ||
} | ||
|
||
/** | ||
* Creates a round-robin supplier. | ||
* | ||
* @param clientUris collection of client URIs | ||
* @return new supplier | ||
*/ | ||
public static RoundRobinSupplier create(Collection<ClientUri> clientUris) { | ||
return new RoundRobinSupplier(clientUris.toArray(new ClientUri[]{})); | ||
} | ||
|
||
protected RoundRobinSupplier(ClientUri[] clientUris) { | ||
this.clientUris = clientUris; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public ClientUri next() { | ||
return clientUris[next++ % clientUris.length]; | ||
} | ||
} | ||
|
||
/** | ||
* Supplies the same client URI over and over, never ends. | ||
*/ | ||
public static class SingleSupplier implements ClientUriSupplier { | ||
|
||
private final ClientUri clientUri; | ||
|
||
/** | ||
* Creates a single supplier. | ||
* | ||
* @param clientUri the client URI as a string | ||
* @return new supplier | ||
*/ | ||
public static SingleSupplier create(String clientUri) { | ||
return new SingleSupplier(ClientUri.create(URI.create(clientUri))); | ||
} | ||
|
||
/** | ||
* Creates a single supplier. | ||
* | ||
* @param clientUri the client URI | ||
* @return new supplier | ||
*/ | ||
public static SingleSupplier create(ClientUri clientUri) { | ||
return new SingleSupplier(clientUri); | ||
} | ||
|
||
protected SingleSupplier(ClientUri clientUri) { | ||
this.clientUri = clientUri; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public ClientUri next() { | ||
return clientUri; | ||
} | ||
} | ||
|
||
/** | ||
* Supplies an iterator that returns a URI chosen at random, never ends. | ||
*/ | ||
public static class RandomSupplier implements ClientUriSupplier { | ||
|
||
private final ClientUri[] clientUris; | ||
private final SecureRandom random = new SecureRandom(); | ||
|
||
/** | ||
* Creates a random supplier. | ||
* | ||
* @param clientUris array of client URIs | ||
* @return new supplier | ||
*/ | ||
public static RandomSupplier create(ClientUri... clientUris) { | ||
return new RandomSupplier(clientUris); | ||
} | ||
|
||
/** | ||
* Creates a random supplier. | ||
* | ||
* @param clientUris collection of client URIs | ||
* @return new supplier | ||
*/ | ||
public static RandomSupplier create(Collection<ClientUri> clientUris) { | ||
return new RandomSupplier(clientUris.toArray(new ClientUri[]{})); | ||
} | ||
|
||
protected RandomSupplier(ClientUri[] clientUris) { | ||
this.clientUris = clientUris; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public ClientUri next() { | ||
return clientUris[random.nextInt(clientUris.length)]; | ||
} | ||
} | ||
} |
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.