[GitHub] [cxf] andymc12 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] andymc12 commented on a change in pull request #660: [CXF-8263] Support SSEs in MP Rest Client 2.0

GitBox

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



##########
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:
       It does (and sorry for the slow turnaround) - the main thing I want to ensure is that the call to `onNext` results in the `delivered` count being incremented.  I think I can do that with a sync block.

##########
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()

Review comment:
       I think I had something similar coded up a while back - but that broke the reactive streams TCK.  IIUC, the subscriber can close or cancel the subscription while the subscription is still sending it requested items.




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