RainYuY commented on code in PR #15532:
URL: https://github.com/apache/dubbo/pull/15532#discussion_r2196404939


##########
dubbo-plugin/dubbo-mutiny/src/main/java/org/apache/dubbo/mutiny/AbstractTripleMutinyPublisher.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.mutiny;
+
+import org.apache.dubbo.rpc.protocol.tri.CancelableStreamObserver;
+import org.apache.dubbo.rpc.protocol.tri.observer.CallStreamObserver;
+
+import java.util.concurrent.Flow;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+
+/**
+ * The middle layer between {@link 
org.apache.dubbo.rpc.protocol.tri.observer.CallStreamObserver} and Mutiny API. 
<p>
+ * 1. passing the data received by CallStreamObserver to Mutiny consumer <br>
+ * 2. passing the request of Mutiny API to CallStreamObserver
+ */
+public abstract class AbstractTripleMutinyPublisher<T> extends 
CancelableStreamObserver<T>
+        implements Flow.Publisher<T>, Flow.Subscription {
+
+    private boolean canRequest;
+
+    private long requested;
+
+    // weather publisher has been subscribed
+    private final AtomicBoolean subscribed = new AtomicBoolean();
+
+    private volatile Flow.Subscriber<? super T> downstream;
+
+    protected volatile CallStreamObserver<?> subscription;
+
+    private final AtomicBoolean hasSub = new AtomicBoolean();
+
+    // cancel status
+    private volatile boolean cancelled;
+
+    // complete status
+    private volatile boolean done;
+
+    // to help bind TripleSubscriber
+    private volatile Consumer<CallStreamObserver<?>> onSubscribe;
+
+    private volatile Runnable shutdownHook;
+
+    private final AtomicBoolean calledShutdown = new AtomicBoolean();
+
+    public AbstractTripleMutinyPublisher() {}
+
+    public AbstractTripleMutinyPublisher(Consumer<CallStreamObserver<?>> 
onSubscribe, Runnable shutdownHook) {
+        this.onSubscribe = onSubscribe;
+        this.shutdownHook = shutdownHook;
+    }
+
+    protected void onSubscribe(CallStreamObserver<?> subscription) {
+        if (subscription != null && this.subscription == null && 
hasSub.compareAndSet(false, true)) {
+            this.subscription = subscription;
+            subscription.disableAutoFlowControl();
+            if (onSubscribe != null) {
+                onSubscribe.accept(subscription);
+            }
+            return;
+        }
+        throw new IllegalStateException(getClass().getSimpleName() + " 
supports only a single subscription");
+    }
+
+    @Override
+    public void subscribe(Flow.Subscriber<? super T> s) {
+        if (s == null) {
+            throw new NullPointerException();
+        }
+        if (subscribed.compareAndSet(false, true)) {
+            this.downstream = s;
+            s.onSubscribe(this);
+            if (cancelled) this.downstream = null;
+        }
+    }
+
+    @Override
+    public void request(long n) {
+        synchronized (this) {
+            if (subscribed.get() && canRequest) {
+                subscription.request(n >= Integer.MAX_VALUE ? 
Integer.MAX_VALUE : (int) n);
+            } else {
+                requested += n;
+            }
+        }
+    }
+
+    public void startRequest() {

Review Comment:
   miss Override



-- 
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