|
20 | 20 | import static lombok.AccessLevel.PRIVATE; |
21 | 21 |
|
22 | 22 | import java.net.InetAddress; |
23 | | -import java.util.Map; |
24 | 23 | import java.util.Optional; |
25 | | -import java.util.concurrent.ConcurrentHashMap; |
26 | | -import java.util.function.BiFunction; |
27 | 24 |
|
28 | 25 | import io.appulse.epmd.java.core.model.request.GetNodeInfo; |
29 | 26 | import io.appulse.epmd.java.core.model.response.NodeInfo; |
30 | 27 |
|
31 | 28 | import lombok.NonNull; |
32 | 29 | import lombok.RequiredArgsConstructor; |
33 | 30 | import lombok.SneakyThrows; |
34 | | -import lombok.Value; |
35 | 31 | import lombok.experimental.FieldDefaults; |
36 | 32 | import lombok.extern.slf4j.Slf4j; |
37 | 33 | import lombok.val; |
|
46 | 42 | @FieldDefaults(level = PRIVATE, makeFinal = true) |
47 | 43 | class LookupService { |
48 | 44 |
|
49 | | - private static final Map<NodeDescriptor, NodeInfo> CACHE; |
50 | | - |
51 | | - private static final BiFunction<NodeDescriptor, NodeInfo, NodeInfo> COMPUTE; |
52 | | - |
53 | | - static { |
54 | | - CACHE = new ConcurrentHashMap<>(); |
55 | | - |
56 | | - COMPUTE = (key, value) -> { |
57 | | - if (value != null) { |
58 | | - log.debug("Used cached value {}", value); |
59 | | - return value; |
60 | | - } |
61 | | - |
62 | | - val request = new GetNodeInfo(key.getNode()); |
63 | | - try (val connection = new Connection(key.getAddress(), key.getPort())) { |
64 | | - val response = connection.send(request, NodeInfo.class); |
65 | | - log.debug("Lookup result is {}", response); |
66 | | - return response.isOk() |
67 | | - ? response |
68 | | - : null; |
69 | | - } |
70 | | - }; |
71 | | - } |
72 | | - |
73 | 45 | @NonNull |
74 | 46 | InetAddress defaultAddress; |
75 | 47 |
|
@@ -100,25 +72,12 @@ public Optional<NodeInfo> lookup (@NonNull String node, @NonNull InetAddress add |
100 | 72 |
|
101 | 73 | log.debug("Looking up node '{}' at '{}:{}'", shortName, address, port); |
102 | 74 |
|
103 | | - val descriptor = new NodeDescriptor(shortName, address, port); |
104 | | - val nodeInfo = CACHE.compute(descriptor, COMPUTE); |
105 | | - return ofNullable(nodeInfo); |
106 | | - } |
107 | | - |
108 | | - void clearCache () { |
109 | | - CACHE.clear(); |
110 | | - } |
111 | | - |
112 | | - @Value |
113 | | - private static class NodeDescriptor { |
114 | | - |
115 | | - @NonNull |
116 | | - String node; |
117 | | - |
118 | | - @NonNull |
119 | | - InetAddress address; |
120 | | - |
121 | | - @NonNull |
122 | | - Integer port; |
| 75 | + val request = new GetNodeInfo(shortName); |
| 76 | + try (val connection = new Connection(address, port)) { |
| 77 | + val response = connection.send(request, NodeInfo.class); |
| 78 | + log.debug("Lookup result is {}", response); |
| 79 | + return ofNullable(response) |
| 80 | + .filter(NodeInfo::isOk); |
| 81 | + } |
123 | 82 | } |
124 | 83 | } |
0 commit comments