View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http2;
20  
21  import org.eclipse.jetty.http2.frames.Frame;
22  import org.eclipse.jetty.http2.frames.WindowUpdateFrame;
23  import org.eclipse.jetty.util.Callback;
24  
25  public class SimpleFlowControlStrategy extends AbstractFlowControlStrategy
26  {
27      public SimpleFlowControlStrategy()
28      {
29          this(DEFAULT_WINDOW_SIZE);
30      }
31  
32      public SimpleFlowControlStrategy(int initialStreamSendWindow)
33      {
34          super(initialStreamSendWindow);
35      }
36  
37      @Override
38      public void onDataConsumed(ISession session, IStream stream, int length)
39      {
40          if (length <= 0)
41              return;
42  
43          // This is the simple algorithm for flow control.
44          // This method is called when a whole flow controlled frame has been consumed.
45          // We send a WindowUpdate every time, even if the frame was very small.
46  
47          WindowUpdateFrame sessionFrame = new WindowUpdateFrame(0, length);
48          session.updateRecvWindow(length);
49          if (LOG.isDebugEnabled())
50              LOG.debug("Data consumed, increased session recv window by {} for {}", length, session);
51  
52          Frame[] streamFrame = Frame.EMPTY_ARRAY;
53          if (stream != null)
54          {
55              if (stream.isClosed())
56              {
57                  if (LOG.isDebugEnabled())
58                      LOG.debug("Data consumed, ignoring update stream recv window by {} for closed {}", length, stream);
59              }
60              else
61              {
62                  streamFrame = new Frame[1];
63                  streamFrame[0] = new WindowUpdateFrame(stream.getId(), length);
64                  stream.updateRecvWindow(length);
65                  if (LOG.isDebugEnabled())
66                      LOG.debug("Data consumed, increased stream recv window by {} for {}", length, stream);
67              }
68          }
69  
70          session.frames(stream, Callback.NOOP, sessionFrame, streamFrame);
71      }
72  }