[GitHub] [cxf] reta commented on a change in pull request #660: [CXF-8263] Support SSEs in MP Rest Client 2.0

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] [cxf] reta commented on a change in pull request #660: [CXF-8263] Support SSEs in MP Rest Client 2.0

GitBox

reta commented on a change in pull request #660:
URL: https://github.com/apache/cxf/pull/660#discussion_r424841054



##########
File path: rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/sse/SseSubscription.java
##########
@@ -0,0 +1,135 @@
+/**
+ * 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.cxf.microprofile.client.sse;
+
+import java.util.LinkedList;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.ws.rs.sse.InboundSseEvent;
+
+import org.apache.cxf.common.util.SystemPropertyAction;
+import org.reactivestreams.Subscriber;
+import org.reactivestreams.Subscription;
+
+public class SseSubscription implements Subscription {
+
+    private static final int DEFAULT_BUFFER_SIZE =
+        SystemPropertyAction.getInteger("org.apache.cxf.microprofile.client.sse.bufferSize", 256);
+    private final SsePublisher publisher;
+    private final Subscriber<? super InboundSseEvent> subscriber;
+    private final AtomicLong requested = new AtomicLong();
+    private final AtomicLong delivered = new AtomicLong();
+    private final AtomicBoolean completed = new AtomicBoolean();
+    private final AtomicBoolean canceled = new AtomicBoolean();
+    //CHECKSTYLE:OFF
+    private final LinkedList<InboundSseEvent> buffer = new LinkedList<>(); //NOPMD
+    //CHECKSTYLE:ON
+    private final AtomicInteger bufferSize = new AtomicInteger(DEFAULT_BUFFER_SIZE);
+
+    SseSubscription(SsePublisher publisher, Subscriber<? super InboundSseEvent> subscriber) {
+        this.publisher = publisher;
+        this.subscriber = subscriber;
+    }
+
+    @Override
+    public void request(long n) {
+        if (canceled.get()) {
+            return;
+        }
+        if (n < 1) {
+            fireError(new IllegalArgumentException("Only positive values may be requested - passed-in " + n));
+            return;
+        }
+        requested.addAndGet(n);
+        synchronized (buffer) {
+            InboundSseEvent bufferedEvent = null;
+            while (delivered.get() < requested.get()
+                   && (bufferedEvent = buffer.pollFirst()) != null) {
+                InboundSseEvent finalEvent = bufferedEvent;
+                delivered.updateAndGet(l -> {

Review comment:
       Sorry, the usage `updateAndGet` was "bothering" me and now I understand why ... I believe we should rethink the approach, here is the problem:
   ```
       /* ...
        * @param updateFunction a side-effect-free function
        * ...
        */
       public final long updateAndGet(LongUnaryOperator updateFunction) {
   ```
   The `updateFunction` must be `a side-effect-free function` (because it could be applied multiple times) but in current implementation is not. I have observed interesting races between `request` and `fireEvent`, which always are scheduled on different threads, when the value of `delivered` was changed and `updateFunction`  was applied multiple time, calling `onNext` several times.
   
   Does it make sense to you?
   




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

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