|
25 | 25 | import io.grpc.netty.NettyChannelBuilder; |
26 | 26 | import io.netty.handler.ssl.SslContextBuilder; |
27 | 27 | import io.netty.util.internal.PlatformDependent; |
28 | | -import io.netty.util.internal.SocketUtils; |
29 | 28 | import lombok.RequiredArgsConstructor; |
30 | 29 | import lombok.extern.slf4j.Slf4j; |
31 | 30 | import org.apache.skywalking.banyandb.v1.client.Options; |
32 | 31 | import org.apache.skywalking.banyandb.v1.client.util.PrivateKeyUtil; |
33 | | - |
34 | 32 | import java.io.File; |
35 | 33 | import java.io.FileInputStream; |
36 | 34 | import java.io.IOException; |
37 | 35 | import java.io.InputStream; |
38 | | -import java.net.InetAddress; |
39 | 36 | import java.net.InetSocketAddress; |
40 | 37 | import java.net.SocketAddress; |
41 | 38 | import java.net.URI; |
42 | 39 | import java.net.UnknownHostException; |
43 | | -import java.util.Arrays; |
44 | | -import java.util.Comparator; |
45 | | -import java.util.stream.Stream; |
46 | 40 |
|
47 | 41 | @Slf4j |
48 | 42 | @RequiredArgsConstructor |
49 | 43 | public class DefaultChannelFactory implements ChannelFactory { |
50 | 44 | private final URI[] targets; |
51 | 45 | private final Options options; |
52 | | - private SocketAddress[] addresses; |
53 | | - private long lastTargetsResolvedTime; |
54 | 46 |
|
55 | 47 | @Override |
56 | 48 | public ManagedChannel create() throws IOException { |
57 | | - if (this.addresses == null || |
58 | | - System.currentTimeMillis() - this.lastTargetsResolvedTime > this.options.getResolveDNSInterval()) { |
59 | | - resolveTargets(); |
60 | | - } |
61 | 49 | NettyChannelBuilder managedChannelBuilder = NettyChannelBuilder.forAddress(resolveAddress()) |
62 | 50 | .maxInboundMessageSize(options.getMaxInboundMessageSize()) |
63 | 51 | .usePlaintext(); |
@@ -91,30 +79,12 @@ public ManagedChannel create() throws IOException { |
91 | 79 | return managedChannelBuilder.build(); |
92 | 80 | } |
93 | 81 |
|
94 | | - private void resolveTargets() { |
95 | | - this.addresses = Arrays.stream(this.targets) |
96 | | - .flatMap(target -> { |
97 | | - try { |
98 | | - return Arrays.stream(SocketUtils.allAddressesByName(target.getHost())) |
99 | | - .map(InetAddress::getHostAddress) |
100 | | - .map(ip -> new InetSocketAddress(ip, target.getPort())); |
101 | | - } catch (Throwable t) { |
102 | | - log.error("Failed to resolve the BanyanDB server's address ", t); |
103 | | - } |
104 | | - return Stream.empty(); |
105 | | - }) |
106 | | - .sorted(Comparator.comparing(InetSocketAddress::toString)) |
107 | | - .distinct() |
108 | | - .toArray(InetSocketAddress[]::new); |
109 | | - this.lastTargetsResolvedTime = System.currentTimeMillis(); |
110 | | - } |
111 | | - |
112 | 82 | private SocketAddress resolveAddress() throws UnknownHostException { |
113 | | - int numAddresses = this.addresses.length; |
| 83 | + int numAddresses = this.targets.length; |
114 | 84 | if (numAddresses < 1) { |
115 | 85 | throw new UnknownHostException(); |
116 | 86 | } |
117 | 87 | int offset = numAddresses == 1 ? 0 : PlatformDependent.threadLocalRandom().nextInt(numAddresses); |
118 | | - return this.addresses[offset]; |
| 88 | + return new InetSocketAddress(this.targets[offset].getHost(), this.targets[offset].getPort()); |
119 | 89 | } |
120 | 90 | } |
0 commit comments