@@ -68,27 +68,10 @@ public String get(String hostname) {
6868 * @return The IP address for the given hostname, or an empty string if the hostname cannot be resolved.
6969 */
7070 private String resolveHostname (String hostname ) {
71- String cachedIp = cache .get (hostname );
7271 try {
73- String ip = NetUtils .getIpByHost (hostname , 0 );
74- if (ip == null || ip .isEmpty ()) {
75- if (cachedIp != null && !cachedIp .isEmpty ()) {
76- LOG .warn ("Failed to resolve hostname {}, use cached ip: {}" , hostname , cachedIp );
77- return cachedIp ;
78- } else {
79- LOG .warn ("Failed to resolve hostname {}, no cached ip available" , hostname );
80- return "" ;
81- }
82- }
83- return ip ;
72+ return NetUtils .getIpByHost (hostname , 0 );
8473 } catch (UnknownHostException e ) {
85- if (cachedIp != null && !cachedIp .isEmpty ()) {
86- LOG .warn ("Failed to resolve hostname {}, use cached ip: {}" , hostname , cachedIp );
87- return cachedIp ;
88- } else {
89- LOG .warn ("Failed to resolve hostname {}, no cached ip available" , hostname );
90- return "" ;
91- }
74+ return "" ;
9275 }
9376 }
9477
@@ -100,12 +83,15 @@ private String resolveHostname(String hostname) {
10083 */
10184 private void refresh () {
10285 for (String hostname : cache .keySet ()) {
103- String resolvedHostname = resolveHostname (hostname );
104- String currentHostname = cache .get (hostname );
105- if (!resolvedHostname .equals (currentHostname )) {
106- cache .put (hostname , resolvedHostname );
107- LOG .info ("IP for hostname {} has changed from {} to {}" , hostname , currentHostname ,
108- resolvedHostname );
86+ String resolvedIp = resolveHostname (hostname );
87+ String currentIp = cache .get (hostname );
88+ if (resolvedIp .isEmpty ()) {
89+ LOG .warn ("Failed to resolve hostname {}, using cached IP: {}" , hostname , currentIp );
90+ continue ;
91+ }
92+ if (!resolvedIp .equals (currentIp )) {
93+ cache .put (hostname , resolvedIp );
94+ LOG .info ("IP for hostname {} has changed from {} to {}" , hostname , currentIp , resolvedIp );
10995 }
11096 }
11197 }
0 commit comments