You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by violetagg <gi...@git.apache.org> on 2017/02/07 09:29:34 UTC

[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

GitHub user violetagg opened a pull request:

    https://github.com/apache/tomcat/pull/42

    Read events suspend/resume logic in websocket impl to achieve backpressure

    Hi,
    
    Currently JSR356 provides possibility to add message handlers in order to receive web socket
    messages but there is no way to instruct the web socket implementation to suspend for a while
    the incoming messages (backpressure) so that the application is able to process the already delivered messages.
    The other web containers (Jetty, Undertow) supports such functionality so I would like to introduce it in Tomcat.
    I prepared one possible implementation.
    
    What do you think about this feature and the proposed implementation?
     
    Regards,
    Violeta

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/violetagg/tomcat ws-suspend-resume

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/tomcat/pull/42.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #42
    
----
commit c9c9619ec33fd6d8971752a91fb9bc7c81fd0c87
Author: Violeta Georgieva <vg...@pivotal.io>
Date:   2017-01-17T17:47:02Z

    Format the code

commit 9efda586f01fd4986a296ee1a9adb9fd9c498c6f
Author: Violeta Georgieva <vg...@pivotal.io>
Date:   2017-01-18T07:01:56Z

    Add WsSession suspend/resume functionality

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99924840
  
    --- Diff: java/org/apache/tomcat/websocket/server/WsFrameServer.java ---
    @@ -56,21 +61,27 @@ public void onDataAvailable() throws IOException {
             if (log.isDebugEnabled()) {
                 log.debug("wsFrameServer.onDataAvailable");
             }
    -        while (isOpen()) {
    -            // Fill up the input buffer with as much data as we can
    -            inputBuffer.mark();
    -            inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    -            int read = socketWrapper.read(false, inputBuffer);
    -            inputBuffer.limit(inputBuffer.position()).reset();
    -            if (read < 0) {
    -                throw new EOFException();
    -            } else if (read == 0) {
    -                return;
    -            }
    -            if (log.isDebugEnabled()) {
    -                log.debug(sm.getString("wsFrameServer.bytesRead", Integer.toString(read)));
    +        if (reading.compareAndSet(false, true)) {
    +            try {
    +                while (isOpen() && !isSuspended()) {
    +                        // Fill up the input buffer with as much data as we can
    +                        inputBuffer.mark();
    +                        inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    +                        int read = socketWrapper.read(false, inputBuffer);
    +                        inputBuffer.limit(inputBuffer.position()).reset();
    +                        if (read < 0) {
    +                            throw new EOFException();
    +                        } else if (read == 0) {
    +                            return;
    +                        }
    +                        if (log.isDebugEnabled()) {
    +                            log.debug(sm.getString("wsFrameServer.bytesRead", Integer.toString(read)));
    +                        }
    +                        processInputBuffer();
    +                }
    +            } finally {
    +                reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99924137
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -58,31 +59,44 @@ void startInputProcessing() {
     
     
         private void processSocketRead() throws IOException {
    +        if (reading.compareAndSet(false, true)) {
    +            while (response.hasRemaining()) {
    +                if (isSuspended()) {
    +                    reading.getAndSet(false);
    +                    return;
    +                }
     
    -        while (response.hasRemaining()) {
    -            inputBuffer.mark();
    -            inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    +                inputBuffer.mark();
    +                inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
     
    -            int toCopy = Math.min(response.remaining(), inputBuffer.remaining());
    +                int toCopy = Math.min(response.remaining(), inputBuffer.remaining());
     
    -            // Copy remaining bytes read in HTTP phase to input buffer used by
    -            // frame processing
    +                // Copy remaining bytes read in HTTP phase to input buffer used by
    +                // frame processing
     
    -            int orgLimit = response.limit();
    -            response.limit(response.position() + toCopy);
    -            inputBuffer.put(response);
    -            response.limit(orgLimit);
    +                int orgLimit = response.limit();
    +                response.limit(response.position() + toCopy);
    +                inputBuffer.put(response);
    +                response.limit(orgLimit);
     
    -            inputBuffer.limit(inputBuffer.position()).reset();
    +                inputBuffer.limit(inputBuffer.position()).reset();
     
    -            // Process the data we have
    -            processInputBuffer();
    -        }
    -        response.clear();
    +                // Process the data we have
    +                try {
    +                    processInputBuffer();
    +                } catch (IOException e) {
    +                    reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99923774
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -58,31 +59,44 @@ void startInputProcessing() {
     
     
         private void processSocketRead() throws IOException {
    +        if (reading.compareAndSet(false, true)) {
    +            while (response.hasRemaining()) {
    +                if (isSuspended()) {
    +                    reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/tomcat/pull/42


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100015643
  
    --- Diff: test/org/apache/tomcat/websocket/TestWsSessionSuspendResume.java ---
    @@ -0,0 +1,144 @@
    +/*
    + * 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.tomcat.websocket;
    +
    +import java.io.IOException;
    +import java.net.URI;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.concurrent.CountDownLatch;
    +import java.util.concurrent.TimeUnit;
    +
    +import javax.websocket.ClientEndpointConfig;
    +import javax.websocket.CloseReason;
    +import javax.websocket.ContainerProvider;
    +import javax.websocket.Endpoint;
    +import javax.websocket.EndpointConfig;
    +import javax.websocket.Session;
    +import javax.websocket.WebSocketContainer;
    +import javax.websocket.server.ServerEndpointConfig;
    +
    +import org.junit.Assert;
    +import org.junit.Test;
    +
    +import org.apache.catalina.Context;
    +import org.apache.catalina.servlets.DefaultServlet;
    +import org.apache.catalina.startup.Tomcat;
    +import org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint;
    +import org.apache.tomcat.websocket.server.TesterEndpointConfig;
    +
    +public class TestWsSessionSuspendResume extends WebSocketBaseTest {
    +
    +    @Test
    +    public void test() throws Exception {
    +        Tomcat tomcat = getTomcatInstance();
    +
    +        Context ctx = tomcat.addContext("", null);
    +        ctx.addApplicationListener(Config.class.getName());
    +
    +        Tomcat.addServlet(ctx, "default", new DefaultServlet());
    +        ctx.addServletMappingDecoded("/", "default");
    +
    +        tomcat.start();
    +
    +        WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    +
    +        ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    +        Session wsSession = wsContainer.connectToServer(
    +                TesterProgrammaticEndpoint.class,
    +                clientEndpointConfig,
    +                new URI("ws://localhost:" + getPort() + Config.PATH));
    +
    +        CountDownLatch latch = new CountDownLatch(2);
    +        wsSession.addMessageHandler(String.class, message -> {
    +            Assert.assertTrue("[echo, echo, echo]".equals(message));
    +            latch.countDown();
    +        });
    +        for (int i = 0; i < 8; i++) {
    +            wsSession.getAsyncRemote().sendText("echo");
    +        }
    +
    +        boolean latchResult = latch.await(30, TimeUnit.SECONDS);
    +        Assert.assertTrue(latchResult);
    +
    +        wsSession.close();
    +    }
    +
    +
    +    public static final class Config extends TesterEndpointConfig {
    +        private static final String PATH = "/echo";
    +
    +        @Override
    +        protected Class<?> getEndpointClass() {
    +            return SuspendResumeEndpoint.class;
    +        }
    +
    +        @Override
    +        protected ServerEndpointConfig getServerEndpointConfig() {
    +            return ServerEndpointConfig.Builder.create(getEndpointClass(), PATH).build();
    +        }
    +    }
    +
    +
    +    public static final class SuspendResumeEndpoint extends Endpoint {
    +
    +        @Override
    +        public void onOpen(Session session, EndpointConfig  epc) {
    +            MessageProcessor processor = new MessageProcessor(session, 3);
    +            session.addMessageHandler(String.class, message -> processor.addMessage(message));
    +        }
    +
    +        @Override
    +        public void onClose(Session session, CloseReason closeReason) {
    +            try {
    +                session.close();
    +            } catch (IOException e) {
    +                e.printStackTrace();
    +            }
    +        }
    +
    +        @Override
    +        public void onError(Session session, Throwable t) {
    +            t.printStackTrace();
    +        }
    +    }
    +
    +
    +    private static final class MessageProcessor {
    +        private final Session session;
    +        private final int count;
    +        private final List<String> messages = new ArrayList<>();
    +
    +        MessageProcessor(Session session, int count) {
    +            this.session = session;
    +            this.count = count;
    +        }
    +
    +        void addMessage(String message) {
    +            if (messages.size() == count) {
    +                ((SuspendableMessageReceiver) session).suspend();
    +                session.getAsyncRemote().sendText(messages.toString(), result -> {
    --- End diff --
    
    I'll try to extend the test


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100015479
  
    --- Diff: java/org/apache/tomcat/websocket/SuspendableMessageReceiver.java ---
    @@ -0,0 +1,24 @@
    +/*
    + * 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.tomcat.websocket;
    +
    +public interface SuspendableMessageReceiver {
    +
    +    public void suspend();
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100015018
  
    --- Diff: java/org/apache/tomcat/websocket/server/WsFrameServer.java ---
    @@ -56,21 +61,27 @@ public void onDataAvailable() throws IOException {
             if (log.isDebugEnabled()) {
                 log.debug("wsFrameServer.onDataAvailable");
             }
    -        while (isOpen()) {
    -            // Fill up the input buffer with as much data as we can
    -            inputBuffer.mark();
    -            inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    -            int read = socketWrapper.read(false, inputBuffer);
    -            inputBuffer.limit(inputBuffer.position()).reset();
    -            if (read < 0) {
    -                throw new EOFException();
    -            } else if (read == 0) {
    -                return;
    -            }
    -            if (log.isDebugEnabled()) {
    -                log.debug(sm.getString("wsFrameServer.bytesRead", Integer.toString(read)));
    +        if (reading.compareAndSet(false, true)) {
    +            try {
    +                while (isOpen() && !isSuspended()) {
    +                        // Fill up the input buffer with as much data as we can
    +                        inputBuffer.mark();
    +                        inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    +                        int read = socketWrapper.read(false, inputBuffer);
    +                        inputBuffer.limit(inputBuffer.position()).reset();
    +                        if (read < 0) {
    +                            throw new EOFException();
    +                        } else if (read == 0) {
    +                            return;
    +                        }
    +                        if (log.isDebugEnabled()) {
    +                            log.debug(sm.getString("wsFrameServer.bytesRead", Integer.toString(read)));
    +                        }
    +                        processInputBuffer();
    +                }
    +            } finally {
    +                reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100014965
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -58,31 +59,44 @@ void startInputProcessing() {
     
     
         private void processSocketRead() throws IOException {
    +        if (reading.compareAndSet(false, true)) {
    +            while (response.hasRemaining()) {
    +                if (isSuspended()) {
    +                    reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99922783
  
    --- Diff: java/org/apache/tomcat/websocket/SuspendableMessageReceiver.java ---
    @@ -0,0 +1,24 @@
    +/*
    + * 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.tomcat.websocket;
    +
    +public interface SuspendableMessageReceiver {
    +
    +    public void suspend();
    --- End diff --
    
    No need of `public`, it is implicit.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99924267
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -116,12 +129,11 @@ protected Log getLog() {
             return log;
         }
     
    -
    -    private class WsFrameClientCompletionHandler
    -            implements CompletionHandler<Integer,Void> {
    +    private class WsFrameClientCompletionHandler implements CompletionHandler<Integer, Void> {
     
             @Override
             public void completed(Integer result, Void attachment) {
    +            reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99924904
  
    --- Diff: java/org/apache/tomcat/websocket/server/WsFrameServer.java ---
    @@ -124,4 +135,56 @@ protected void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOExcept
                 Thread.currentThread().setContextClassLoader(cl);
             }
         }
    +
    +
    +    @Override
    +    protected void resumeProcessing() {
    +        if (!reading.get()) {
    +            try {
    +                if (reading.compareAndSet(false, true)) {
    +                    try {
    +                        if (isOpen() && inputBuffer.remaining() > 0) {
    +                            processInputBuffer();
    +                        }
    +                    } finally {
    +                        reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100015036
  
    --- Diff: java/org/apache/tomcat/websocket/server/WsFrameServer.java ---
    @@ -124,4 +135,56 @@ protected void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOExcept
                 Thread.currentThread().setContextClassLoader(cl);
             }
         }
    +
    +
    +    @Override
    +    protected void resumeProcessing() {
    +        if (!reading.get()) {
    +            try {
    +                if (reading.compareAndSet(false, true)) {
    +                    try {
    +                        if (isOpen() && inputBuffer.remaining() > 0) {
    +                            processInputBuffer();
    +                        }
    +                    } finally {
    +                        reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100014999
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -116,12 +129,11 @@ protected Log getLog() {
             return log;
         }
     
    -
    -    private class WsFrameClientCompletionHandler
    -            implements CompletionHandler<Integer,Void> {
    +    private class WsFrameClientCompletionHandler implements CompletionHandler<Integer, Void> {
     
             @Override
             public void completed(Integer result, Void attachment) {
    +            reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100424012
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameBase.java ---
    @@ -687,6 +691,29 @@ protected Transformation getTransformation() {
         }
     
     
    +    @Override
    +    public void suspend() {
    +        if (!this.suspended.compareAndSet(false, true)) {
    +            getLog().warn("Message receiving has already been suspended.");
    --- End diff --
    
    fixed


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100014979
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -58,31 +59,44 @@ void startInputProcessing() {
     
     
         private void processSocketRead() throws IOException {
    +        if (reading.compareAndSet(false, true)) {
    +            while (response.hasRemaining()) {
    +                if (isSuspended()) {
    +                    reading.getAndSet(false);
    +                    return;
    +                }
     
    -        while (response.hasRemaining()) {
    -            inputBuffer.mark();
    -            inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
    +                inputBuffer.mark();
    +                inputBuffer.position(inputBuffer.limit()).limit(inputBuffer.capacity());
     
    -            int toCopy = Math.min(response.remaining(), inputBuffer.remaining());
    +                int toCopy = Math.min(response.remaining(), inputBuffer.remaining());
     
    -            // Copy remaining bytes read in HTTP phase to input buffer used by
    -            // frame processing
    +                // Copy remaining bytes read in HTTP phase to input buffer used by
    +                // frame processing
     
    -            int orgLimit = response.limit();
    -            response.limit(response.position() + toCopy);
    -            inputBuffer.put(response);
    -            response.limit(orgLimit);
    +                int orgLimit = response.limit();
    +                response.limit(response.position() + toCopy);
    +                inputBuffer.put(response);
    +                response.limit(orgLimit);
     
    -            inputBuffer.limit(inputBuffer.position()).reset();
    +                inputBuffer.limit(inputBuffer.position()).reset();
     
    -            // Process the data we have
    -            processInputBuffer();
    -        }
    -        response.clear();
    +                // Process the data we have
    +                try {
    +                    processInputBuffer();
    +                } catch (IOException e) {
    +                    reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99924344
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -150,10 +162,11 @@ public void completed(Integer result, Void attachment) {
     
             @Override
             public void failed(Throwable exc, Void attachment) {
    +            reading.getAndSet(false);
    --- End diff --
    
    `reading.set(false);`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by violetagg <gi...@git.apache.org>.
Github user violetagg commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100015008
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameClient.java ---
    @@ -150,10 +162,11 @@ public void completed(Integer result, Void attachment) {
     
             @Override
             public void failed(Throwable exc, Void attachment) {
    +            reading.getAndSet(false);
    --- End diff --
    
    applied


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r100054233
  
    --- Diff: java/org/apache/tomcat/websocket/WsFrameBase.java ---
    @@ -687,6 +691,29 @@ protected Transformation getTransformation() {
         }
     
     
    +    @Override
    +    public void suspend() {
    +        if (!this.suspended.compareAndSet(false, true)) {
    +            getLog().warn("Message receiving has already been suspended.");
    --- End diff --
    
    Should this use StringManager ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[GitHub] tomcat pull request #42: Read events suspend/resume logic in websocket impl ...

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/tomcat/pull/42#discussion_r99928093
  
    --- Diff: test/org/apache/tomcat/websocket/TestWsSessionSuspendResume.java ---
    @@ -0,0 +1,144 @@
    +/*
    + * 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.tomcat.websocket;
    +
    +import java.io.IOException;
    +import java.net.URI;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.concurrent.CountDownLatch;
    +import java.util.concurrent.TimeUnit;
    +
    +import javax.websocket.ClientEndpointConfig;
    +import javax.websocket.CloseReason;
    +import javax.websocket.ContainerProvider;
    +import javax.websocket.Endpoint;
    +import javax.websocket.EndpointConfig;
    +import javax.websocket.Session;
    +import javax.websocket.WebSocketContainer;
    +import javax.websocket.server.ServerEndpointConfig;
    +
    +import org.junit.Assert;
    +import org.junit.Test;
    +
    +import org.apache.catalina.Context;
    +import org.apache.catalina.servlets.DefaultServlet;
    +import org.apache.catalina.startup.Tomcat;
    +import org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint;
    +import org.apache.tomcat.websocket.server.TesterEndpointConfig;
    +
    +public class TestWsSessionSuspendResume extends WebSocketBaseTest {
    +
    +    @Test
    +    public void test() throws Exception {
    +        Tomcat tomcat = getTomcatInstance();
    +
    +        Context ctx = tomcat.addContext("", null);
    +        ctx.addApplicationListener(Config.class.getName());
    +
    +        Tomcat.addServlet(ctx, "default", new DefaultServlet());
    +        ctx.addServletMappingDecoded("/", "default");
    +
    +        tomcat.start();
    +
    +        WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    +
    +        ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    +        Session wsSession = wsContainer.connectToServer(
    +                TesterProgrammaticEndpoint.class,
    +                clientEndpointConfig,
    +                new URI("ws://localhost:" + getPort() + Config.PATH));
    +
    +        CountDownLatch latch = new CountDownLatch(2);
    +        wsSession.addMessageHandler(String.class, message -> {
    +            Assert.assertTrue("[echo, echo, echo]".equals(message));
    +            latch.countDown();
    +        });
    +        for (int i = 0; i < 8; i++) {
    +            wsSession.getAsyncRemote().sendText("echo");
    +        }
    +
    +        boolean latchResult = latch.await(30, TimeUnit.SECONDS);
    +        Assert.assertTrue(latchResult);
    +
    +        wsSession.close();
    +    }
    +
    +
    +    public static final class Config extends TesterEndpointConfig {
    +        private static final String PATH = "/echo";
    +
    +        @Override
    +        protected Class<?> getEndpointClass() {
    +            return SuspendResumeEndpoint.class;
    +        }
    +
    +        @Override
    +        protected ServerEndpointConfig getServerEndpointConfig() {
    +            return ServerEndpointConfig.Builder.create(getEndpointClass(), PATH).build();
    +        }
    +    }
    +
    +
    +    public static final class SuspendResumeEndpoint extends Endpoint {
    +
    +        @Override
    +        public void onOpen(Session session, EndpointConfig  epc) {
    +            MessageProcessor processor = new MessageProcessor(session, 3);
    +            session.addMessageHandler(String.class, message -> processor.addMessage(message));
    +        }
    +
    +        @Override
    +        public void onClose(Session session, CloseReason closeReason) {
    +            try {
    +                session.close();
    +            } catch (IOException e) {
    +                e.printStackTrace();
    +            }
    +        }
    +
    +        @Override
    +        public void onError(Session session, Throwable t) {
    +            t.printStackTrace();
    +        }
    +    }
    +
    +
    +    private static final class MessageProcessor {
    +        private final Session session;
    +        private final int count;
    +        private final List<String> messages = new ArrayList<>();
    +
    +        MessageProcessor(Session session, int count) {
    +            this.session = session;
    +            this.count = count;
    +        }
    +
    +        void addMessage(String message) {
    +            if (messages.size() == count) {
    +                ((SuspendableMessageReceiver) session).suspend();
    +                session.getAsyncRemote().sendText(messages.toString(), result -> {
    --- End diff --
    
    IMO `sendText(...)` should be executed in a different thread with some delay.
    Otherwise now even without calling `suspend()` the behavior would be same. `messages` will be serialized to its String form and the fourth message would have a chance to break the test.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org