4545import io .grpc .StatusOr ;
4646import io .grpc .SynchronizationContext ;
4747import io .grpc .SynchronizationContext .ScheduledHandle ;
48- import io .grpc .internal .GrpcUtil ;
48+ import io .grpc .internal .FixedObjectPool ;
4949import io .grpc .internal .ObjectPool ;
50- import io .grpc .internal .SharedResourceHolder ;
5150import io .grpc .xds .EnvoyServerProtoData .FilterChain ;
5251import io .grpc .xds .Filter .FilterConfig ;
5352import io .grpc .xds .Filter .NamedFilterConfig ;
@@ -100,7 +99,7 @@ public void uncaughtException(Thread t, Throwable e) {
10099 static final long RETRY_DELAY_NANOS = TimeUnit .MINUTES .toNanos (1 );
101100 private final String listenerAddress ;
102101 private final ServerBuilder <?> delegateBuilder ;
103- private boolean sharedTimeService ;
102+ private final ObjectPool < ScheduledExecutorService > timeServicePool ;
104103 private final ScheduledExecutorService timeService ;
105104 private final FilterRegistry filterRegistry ;
106105 private final ThreadSafeRandom random = ThreadSafeRandomImpl .instance ;
@@ -128,14 +127,16 @@ public void uncaughtException(Thread t, Throwable e) {
128127 // NamedFilterConfig.filterStateKey -> filter_instance.
129128 private final HashMap <String , Filter > activeFiltersDefaultChain = new HashMap <>();
130129
130+ @ VisibleForTesting
131131 XdsServerWrapper (
132- String listenerAddress ,
133- ServerBuilder <?> delegateBuilder ,
134- XdsServingStatusListener listener ,
135- FilterChainSelectorManager filterChainSelectorManager ,
136- XdsClientPoolFactory xdsClientPoolFactory ,
137- @ Nullable Map <String , ?> bootstrapOverride ,
138- FilterRegistry filterRegistry ) {
132+ String listenerAddress ,
133+ ServerBuilder <?> delegateBuilder ,
134+ XdsServingStatusListener listener ,
135+ FilterChainSelectorManager filterChainSelectorManager ,
136+ XdsClientPoolFactory xdsClientPoolFactory ,
137+ @ Nullable Map <String , ?> bootstrapOverride ,
138+ FilterRegistry filterRegistry ,
139+ ScheduledExecutorService timeService ) {
139140 this (
140141 listenerAddress ,
141142 delegateBuilder ,
@@ -144,11 +145,9 @@ public void uncaughtException(Thread t, Throwable e) {
144145 xdsClientPoolFactory ,
145146 bootstrapOverride ,
146147 filterRegistry ,
147- SharedResourceHolder .get (GrpcUtil .TIMER_SERVICE ));
148- sharedTimeService = true ;
148+ new FixedObjectPool <>(timeService ));
149149 }
150150
151- @ VisibleForTesting
152151 XdsServerWrapper (
153152 String listenerAddress ,
154153 ServerBuilder <?> delegateBuilder ,
@@ -157,7 +156,7 @@ public void uncaughtException(Thread t, Throwable e) {
157156 XdsClientPoolFactory xdsClientPoolFactory ,
158157 @ Nullable Map <String , ?> bootstrapOverride ,
159158 FilterRegistry filterRegistry ,
160- ScheduledExecutorService timeService ) {
159+ ObjectPool < ScheduledExecutorService > timeServicePool ) {
161160 this .listenerAddress = checkNotNull (listenerAddress , "listenerAddress" );
162161 this .delegateBuilder = checkNotNull (delegateBuilder , "delegateBuilder" );
163162 this .delegateBuilder .intercept (new ConfigApplyingInterceptor ());
@@ -166,7 +165,8 @@ public void uncaughtException(Thread t, Throwable e) {
166165 = checkNotNull (filterChainSelectorManager , "filterChainSelectorManager" );
167166 this .xdsClientPoolFactory = checkNotNull (xdsClientPoolFactory , "xdsClientPoolFactory" );
168167 this .bootstrapOverride = bootstrapOverride ;
169- this .timeService = checkNotNull (timeService , "timeService" );
168+ this .timeServicePool = checkNotNull (timeServicePool , "timeServicePool" );
169+ this .timeService = checkNotNull (timeServicePool .getObject (), "timeService" );
170170 this .filterRegistry = checkNotNull (filterRegistry ,"filterRegistry" );
171171 this .delegate = delegateBuilder .build ();
172172 }
@@ -275,9 +275,7 @@ private void internalShutdown() {
275275 if (restartTimer != null ) {
276276 restartTimer .cancel ();
277277 }
278- if (sharedTimeService ) {
279- SharedResourceHolder .release (GrpcUtil .TIMER_SERVICE , timeService );
280- }
278+ timeServicePool .returnObject (timeService );
281279 isServing = false ;
282280 internalTerminationLatch .countDown ();
283281 }
0 commit comments