guohao commented on a change in pull request #8871:
URL: https://github.com/apache/dubbo/pull/8871#discussion_r716153024



##########
File path: 
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ServerTransportObserver.java
##########
@@ -48,19 +59,38 @@ public void onMetadata(Metadata metadata, boolean 
endStream) {
             headers.set(TripleHeaderEnum.CONTENT_TYPE_KEY.getHeader(), 
TripleConstant.CONTENT_PROTO);
         }
         ctx.writeAndFlush(new DefaultHttp2HeadersFrame(headers, endStream))
-            .addListener(future -> {
-                if (!future.isSuccess()) {
-                    promise.tryFailure(future.cause());
-                }
-            });
+                .addListener(future -> {

Review comment:
       why discard `promise` here ?

##########
File path: 
dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/CancellationContext.java
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executor;
+
+public class CancellationContext implements Closeable {
+
+    private ArrayList<ExecutableListener> listeners;
+    private Throwable cancellationCause;
+    private boolean cancelled;
+
+    public void addListener(
+            final CancellationListener cancellationListener, final Executor 
executor) {
+        addListener(cancellationListener, executor, null);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener) {
+        addListener(cancellationListener, Runnable::run, null);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener,
+            final RpcServiceContext context) {
+        addListener(cancellationListener, Runnable::run, context);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener,
+            final Executor executor,
+            final RpcServiceContext context) {
+        addListenerInternal(new ExecutableListener(executor, 
cancellationListener, context));
+    }
+
+    public void addListenerInternal(ExecutableListener executableListener) {
+        synchronized (this) {
+            if (isCancelled()) {
+                executableListener.deliver();
+            } else {
+                if (listeners == null) {
+                    listeners = new ArrayList<>();
+                }
+                listeners.add(executableListener);
+            }
+        }
+    }
+
+    public boolean cancel(Throwable cause) {
+        boolean triggeredCancel = false;
+        synchronized (this) {
+            if (!cancelled) {
+                cancelled = true;
+                this.cancellationCause = cause;
+                triggeredCancel = true;
+            }
+        }
+        if (triggeredCancel) {
+            notifyAndClearListeners();
+        }
+        return triggeredCancel;
+    }
+
+    private void notifyAndClearListeners() {
+        ArrayList<ExecutableListener> tmpListeners;
+        synchronized (this) {
+            if (listeners == null) {
+                return;
+            }
+            tmpListeners = listeners;

Review comment:
       This reference may cause `ConcurrentModification`

##########
File path: 
dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/CancellationContext.java
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executor;
+
+public class CancellationContext implements Closeable {
+
+    private ArrayList<ExecutableListener> listeners;
+    private Throwable cancellationCause;
+    private boolean cancelled;
+
+    public void addListener(
+            final CancellationListener cancellationListener, final Executor 
executor) {
+        addListener(cancellationListener, executor, null);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener) {
+        addListener(cancellationListener, Runnable::run, null);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener,
+            final RpcServiceContext context) {
+        addListener(cancellationListener, Runnable::run, context);
+    }
+
+    public void addListener(
+            final CancellationListener cancellationListener,
+            final Executor executor,
+            final RpcServiceContext context) {
+        addListenerInternal(new ExecutableListener(executor, 
cancellationListener, context));
+    }
+
+    public void addListenerInternal(ExecutableListener executableListener) {
+        synchronized (this) {
+            if (isCancelled()) {
+                executableListener.deliver();
+            } else {
+                if (listeners == null) {
+                    listeners = new ArrayList<>();
+                }
+                listeners.add(executableListener);
+            }
+        }
+    }
+
+    public boolean cancel(Throwable cause) {
+        boolean triggeredCancel = false;
+        synchronized (this) {
+            if (!cancelled) {
+                cancelled = true;
+                this.cancellationCause = cause;
+                triggeredCancel = true;
+            }
+        }
+        if (triggeredCancel) {
+            notifyAndClearListeners();
+        }
+        return triggeredCancel;
+    }
+
+    private void notifyAndClearListeners() {
+        ArrayList<ExecutableListener> tmpListeners;
+        synchronized (this) {
+            if (listeners == null) {
+                return;
+            }
+            tmpListeners = listeners;
+            listeners = null;
+        }
+        for (ExecutableListener tmpListener : tmpListeners) {
+            tmpListener.deliver();
+        }
+    }
+
+    public boolean isCancelled() {

Review comment:
       ```suggestion
       public synchronized boolean isCancelled() {
          return cancelled;
       }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to