3131import org .apache .servicecomb .common .rest .filter .HttpServerFilter ;
3232import org .apache .servicecomb .core .Invocation ;
3333import org .apache .servicecomb .core .definition .OperationMeta ;
34+ import org .apache .servicecomb .foundation .common .concurrency .SuppressedRunnableWrapper ;
3435import org .apache .servicecomb .foundation .common .utils .PartUtils ;
3536import org .apache .servicecomb .foundation .vertx .http .HttpServletRequestEx ;
3637import org .apache .servicecomb .foundation .vertx .http .HttpServletResponseEx ;
@@ -126,19 +127,15 @@ public void onSubscribe(Subscription s) {
126127
127128 @ Override
128129 public void onNext (Object o ) {
129- try {
130- writeResponse (responseEx , produceProcessor , o , response ).whenComplete ((r , e ) -> {
131- if (e != null ) {
132- subscription .cancel ();
133- result .completeExceptionally (e );
134- return ;
135- }
130+ writeResponse (responseEx , produceProcessor , o , response ).thenApply (r -> {
136131 subscription .request (1 );
132+ return r ;
133+ })
134+ .exceptionally (e -> {
135+ new SuppressedRunnableWrapper (() -> subscription .cancel ()).run ();
136+ new SuppressedRunnableWrapper (() -> result .completeExceptionally (e )).run ();
137+ return response ;
137138 });
138- } catch (Throwable e ) {
139- LOGGER .warn ("Failed to subscribe event: {}" , o , e );
140- result .completeExceptionally (e );
141- }
142139 }
143140
144141 @ Override
@@ -158,22 +155,18 @@ private static CompletableFuture<Response> writeResponse(
158155 HttpServletResponseEx responseEx , ProduceProcessor produceProcessor , Object data , Response response ) {
159156 try (BufferOutputStream output = new BufferOutputStream (Buffer .buffer ())) {
160157 produceProcessor .encodeResponse (output , data );
161- CompletableFuture <Response > result = new CompletableFuture <>();
162- responseEx .sendBuffer (output .getBuffer ()).whenComplete ((v , e ) -> {
163- if (e != null ) {
164- result .completeExceptionally (e );
165- }
166- try {
167- responseEx .flushBuffer ();
168- } catch (IOException ex ) {
169- LOGGER .warn ("Failed to flush buffer for Server Send Events" , ex );
170- }
171- });
172- result .complete (response );
173- return result ;
158+ return responseEx .sendBuffer (output .getBuffer ())
159+ .thenApply (v -> {
160+ try {
161+ responseEx .flushBuffer ();
162+ return response ;
163+ } catch (IOException e ) {
164+ LOGGER .warn ("Failed to flush buffer for Server Send Events" , e );
165+ throw new IllegalStateException ("Failed to flush buffer for Server Send Events" , e );
166+ }
167+ });
174168 } catch (Throwable e ) {
175169 LOGGER .error ("internal service error must be fixed." , e );
176- responseEx .setStatus (500 );
177170 return CompletableFuture .failedFuture (e );
178171 }
179172 }
0 commit comments