54
54
import java .util .concurrent .ScheduledExecutorService ;
55
55
import java .util .concurrent .ThreadFactory ;
56
56
import java .util .concurrent .TimeUnit ;
57
+ import java .util .concurrent .atomic .AtomicReference ;
57
58
import java .util .logging .Level ;
58
59
import java .util .logging .Logger ;
59
60
import javax .annotation .Nullable ;
@@ -70,7 +71,7 @@ public final class XdsClientWrapperForServerSds {
70
71
private static final TimeServiceResource timeServiceResource =
71
72
new TimeServiceResource ("GrpcServerXdsClient" );
72
73
73
- private EnvoyServerProtoData .Listener curListener ;
74
+ private AtomicReference < EnvoyServerProtoData .Listener > curListener = new AtomicReference <>() ;
74
75
@ SuppressWarnings ("unused" )
75
76
@ Nullable private XdsClient xdsClient ;
76
77
private final int port ;
@@ -137,14 +138,14 @@ public void start(XdsClient xdsClient, String grpcServerResourceId) {
137
138
new XdsClient .LdsResourceWatcher () {
138
139
@ Override
139
140
public void onChanged (XdsClient .LdsUpdate update ) {
140
- curListener = update .listener ;
141
+ curListener . set ( update .listener ) ;
141
142
reportSuccess ();
142
143
}
143
144
144
145
@ Override
145
146
public void onResourceDoesNotExist (String resourceName ) {
146
147
logger .log (Level .WARNING , "Resource {0} is unavailable" , resourceName );
147
- curListener = null ;
148
+ curListener . set ( null ) ;
148
149
reportError (Status .NOT_FOUND .asException (), true );
149
150
}
150
151
@@ -180,7 +181,8 @@ private static boolean isResourceAbsent(Status status) {
180
181
*/
181
182
@ Nullable
182
183
public DownstreamTlsContext getDownstreamTlsContext (Channel channel ) {
183
- if (curListener != null && channel != null ) {
184
+ EnvoyServerProtoData .Listener copyListener = curListener .get ();
185
+ if (copyListener != null && channel != null ) {
184
186
SocketAddress localAddress = channel .localAddress ();
185
187
SocketAddress remoteAddress = channel .remoteAddress ();
186
188
if (localAddress instanceof InetSocketAddress && remoteAddress instanceof InetSocketAddress ) {
@@ -189,7 +191,7 @@ public DownstreamTlsContext getDownstreamTlsContext(Channel channel) {
189
191
checkState (
190
192
port == localInetAddr .getPort (),
191
193
"Channel localAddress port does not match requested listener port" );
192
- return getDownstreamTlsContext (localInetAddr , remoteInetAddr );
194
+ return getDownstreamTlsContext (localInetAddr , remoteInetAddr , copyListener );
193
195
}
194
196
}
195
197
return null ;
@@ -204,9 +206,10 @@ public DownstreamTlsContext getDownstreamTlsContext(Channel channel) {
204
206
* @param localInetAddr dest address of the inbound connection
205
207
* @param remoteInetAddr source address of the inbound connection
206
208
*/
207
- private DownstreamTlsContext getDownstreamTlsContext (
208
- InetSocketAddress localInetAddr , InetSocketAddress remoteInetAddr ) {
209
- List <FilterChain > filterChains = curListener .getFilterChains ();
209
+ private static DownstreamTlsContext getDownstreamTlsContext (
210
+ InetSocketAddress localInetAddr , InetSocketAddress remoteInetAddr ,
211
+ EnvoyServerProtoData .Listener listener ) {
212
+ List <FilterChain > filterChains = listener .getFilterChains ();
210
213
211
214
filterChains = filterOnDestinationPort (filterChains );
212
215
filterChains = filterOnIpAddress (filterChains , localInetAddr .getAddress (), true );
@@ -221,7 +224,7 @@ private DownstreamTlsContext getDownstreamTlsContext(
221
224
} else if (filterChains .size () == 1 ) {
222
225
return filterChains .get (0 ).getDownstreamTlsContext ();
223
226
}
224
- return curListener .getDefaultFilterChain ().getDownstreamTlsContext ();
227
+ return listener .getDefaultFilterChain ().getDownstreamTlsContext ();
225
228
}
226
229
227
230
// destination_port present => Always fail match
@@ -255,7 +258,7 @@ private static List<FilterChain> filterOnSourcePort(
255
258
return filteredOnMatch .isEmpty () ? filteredOnEmpty : filteredOnMatch ;
256
259
}
257
260
258
- private List <FilterChain > filterOnSourceType (
261
+ private static List <FilterChain > filterOnSourceType (
259
262
List <FilterChain > filterChains , InetAddress sourceAddress , InetAddress destAddress ) {
260
263
ArrayList <FilterChain > filtered = new ArrayList <>(filterChains .size ());
261
264
for (FilterChain filterChain : filterChains ) {
@@ -350,7 +353,7 @@ public int hashCode() {
350
353
}
351
354
352
355
// use prefix_ranges (CIDR) and get the most specific matches
353
- private List <FilterChain > filterOnIpAddress (
356
+ private static List <FilterChain > filterOnIpAddress (
354
357
List <FilterChain > filterChains , InetAddress address , boolean forDestination ) {
355
358
PriorityQueue <QueueElement > heap = new PriorityQueue <>(10 , new QueueElementComparator ());
356
359
@@ -384,7 +387,8 @@ public void addServerWatcher(ServerWatcher serverWatcher) {
384
387
synchronized (serverWatchers ) {
385
388
serverWatchers .add (serverWatcher );
386
389
}
387
- if (curListener != null ) {
390
+ EnvoyServerProtoData .Listener copyListener = curListener .get ();
391
+ if (copyListener != null ) {
388
392
serverWatcher .onListenerUpdate ();
389
393
}
390
394
}
0 commit comments