1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.net.InetAddress;
22 import java.net.InetSocketAddress;
23 import java.net.SocketAddress;
24 import java.net.UnknownHostException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.Executor;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import org.eclipse.jetty.util.annotation.ManagedAttribute;
33 import org.eclipse.jetty.util.annotation.ManagedObject;
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.log.Logger;
36 import org.eclipse.jetty.util.thread.Scheduler;
37
38
39
40
41 public interface SocketAddressResolver
42 {
43
44
45
46
47
48
49
50 public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise);
51
52
53
54
55 @ManagedObject("The synchronous address resolver")
56 public static class Sync implements SocketAddressResolver
57 {
58 @Override
59 public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise)
60 {
61 try
62 {
63 InetAddress[] addresses = InetAddress.getAllByName(host);
64
65 List<InetSocketAddress> result = new ArrayList<>(addresses.length);
66 for (InetAddress address : addresses)
67 result.add(new InetSocketAddress(address, port));
68
69 if (result.isEmpty())
70 promise.failed(new UnknownHostException());
71 else
72 promise.succeeded(result);
73 }
74 catch (Throwable x)
75 {
76 promise.failed(x);
77 }
78 }
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 @ManagedObject("The asynchronous address resolver")
105 public static class Async implements SocketAddressResolver
106 {
107 private static final Logger LOG = Log.getLogger(SocketAddressResolver.class);
108
109 private final Executor executor;
110 private final Scheduler scheduler;
111 private final long timeout;
112
113
114
115
116
117
118
119
120
121 public Async(Executor executor, Scheduler scheduler, long timeout)
122 {
123 this.executor = executor;
124 this.scheduler = scheduler;
125 this.timeout = timeout;
126 }
127
128 public Executor getExecutor()
129 {
130 return executor;
131 }
132
133 public Scheduler getScheduler()
134 {
135 return scheduler;
136 }
137
138 @ManagedAttribute(value = "The timeout, in milliseconds, to resolve an address", readonly = true)
139 public long getTimeout()
140 {
141 return timeout;
142 }
143
144 @Override
145 public void resolve(final String host, final int port, final Promise<List<InetSocketAddress>> promise)
146 {
147 executor.execute(() ->
148 {
149 Scheduler.Task task = null;
150 final AtomicBoolean complete = new AtomicBoolean();
151 if (timeout > 0)
152 {
153 final Thread thread = Thread.currentThread();
154 task = scheduler.schedule(() ->
155 {
156 if (complete.compareAndSet(false, true))
157 {
158 promise.failed(new TimeoutException());
159 thread.interrupt();
160 }
161 }, timeout, TimeUnit.MILLISECONDS);
162 }
163
164 try
165 {
166 long start = System.nanoTime();
167 InetAddress[] addresses = InetAddress.getAllByName(host);
168 long elapsed = System.nanoTime() - start;
169 if (LOG.isDebugEnabled())
170 LOG.debug("Resolved {} in {} ms", host, TimeUnit.NANOSECONDS.toMillis(elapsed));
171
172 List<InetSocketAddress> result = new ArrayList<>(addresses.length);
173 for (InetAddress address : addresses)
174 result.add(new InetSocketAddress(address, port));
175
176 if (complete.compareAndSet(false, true))
177 {
178 if (result.isEmpty())
179 promise.failed(new UnknownHostException());
180 else
181 promise.succeeded(result);
182 }
183 }
184 catch (Throwable x)
185 {
186 if (complete.compareAndSet(false, true))
187 promise.failed(x);
188 }
189 finally
190 {
191 if (task != null)
192 task.cancel();
193 }
194 });
195 }
196 }
197 }