@@ -145,7 +145,22 @@ public Watch createWatch(
145145 }
146146
147147 // Otherwise, the watch may be responded immediately
148- respond (watch , snapshot , group );
148+ boolean responded = respond (watch , snapshot , group );
149+
150+ if (!responded ) {
151+ watchCount ++;
152+
153+ LOGGER .info ("did not respond immediately, leaving open watch {} for {}[{}] from node {} for version {}" ,
154+ watchCount ,
155+ request .getTypeUrl (),
156+ String .join (", " , request .getResourceNamesList ()),
157+ group ,
158+ request .getVersionInfo ());
159+
160+ status .setWatch (watchCount , watch );
161+
162+ watch .setStop (() -> status .removeWatch (watchCount ));
163+ }
149164
150165 return watch ;
151166
@@ -245,7 +260,7 @@ private Response createResponse(DiscoveryRequest request, Map<String, ? extends
245260 return Response .create (request , filtered , version );
246261 }
247262
248- private void respond (Watch watch , Snapshot snapshot , T group ) {
263+ private boolean respond (Watch watch , Snapshot snapshot , T group ) {
249264 Map <String , ? extends Message > snapshotResources = snapshot .resources (watch .request ().getTypeUrl ());
250265
251266 if (!watch .request ().getResourceNamesList ().isEmpty () && watch .ads ()) {
@@ -262,7 +277,7 @@ private void respond(Watch watch, Snapshot snapshot, T group) {
262277 String .join (", " , watch .request ().getResourceNamesList ()),
263278 String .join (", " , missingNames ));
264279
265- return ;
280+ return false ;
266281 }
267282 }
268283
@@ -281,6 +296,7 @@ private void respond(Watch watch, Snapshot snapshot, T group) {
281296
282297 try {
283298 watch .respond (response );
299+ return true ;
284300 } catch (WatchCancelledException e ) {
285301 LOGGER .error (
286302 "failed to respond for {} from node {} at version {} with version {} because watch was already cancelled" ,
@@ -289,5 +305,7 @@ private void respond(Watch watch, Snapshot snapshot, T group) {
289305 watch .request ().getVersionInfo (),
290306 version );
291307 }
308+
309+ return false ;
292310 }
293311}
0 commit comments