comparison src/org/eclipse/jetty/server/AsyncHttpConnection.java @ 963:4b6216fa9cec

replace SelectChannelEndPoint._state with isDispatched
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 14 Oct 2016 00:15:28 -0600
parents 1094975d013b
children 866f2e801618
comparison
equal deleted inserted replaced
962:94498d6daf5b 963:4b6216fa9cec
35 /** Asychronous Server HTTP connection 35 /** Asychronous Server HTTP connection
36 * 36 *
37 */ 37 */
38 public class AsyncHttpConnection extends AbstractHttpConnection implements AsyncConnection 38 public class AsyncHttpConnection extends AbstractHttpConnection implements AsyncConnection
39 { 39 {
40 private final static int NO_PROGRESS_INFO = Integer.getInteger("org.mortbay.jetty.NO_PROGRESS_INFO",100); 40 private final static int NO_PROGRESS_INFO = Integer.getInteger("org.mortbay.jetty.NO_PROGRESS_INFO",100);
41 private final static int NO_PROGRESS_CLOSE = Integer.getInteger("org.mortbay.jetty.NO_PROGRESS_CLOSE",200); 41 private final static int NO_PROGRESS_CLOSE = Integer.getInteger("org.mortbay.jetty.NO_PROGRESS_CLOSE",200);
42 42
43 private static final Logger LOG = LoggerFactory.getLogger(AsyncHttpConnection.class); 43 private static final Logger LOG = LoggerFactory.getLogger(AsyncHttpConnection.class);
44 private int _total_no_progress; 44 private int _total_no_progress;
45 private final AsyncEndPoint _asyncEndp; 45 private final AsyncEndPoint _asyncEndp;
46 private boolean _readInterested = true; 46 private boolean _readInterested = true;
47 47
48 public AsyncHttpConnection(Connector connector, EndPoint endpoint, Server server) 48 public AsyncHttpConnection(Connector connector, EndPoint endpoint, Server server)
49 { 49 {
50 super(connector,endpoint,server); 50 super(connector,endpoint,server);
51 _asyncEndp=(AsyncEndPoint)endpoint; 51 _asyncEndp=(AsyncEndPoint)endpoint;
52 } 52 }
53 53
54 @Override 54 @Override
55 public Connection handle() throws IOException 55 public Connection handle() throws IOException
56 { 56 {
57 Connection connection = this; 57 Connection connection = this;
58 boolean some_progress=false; 58 boolean some_progress = false;
59 boolean progress=true; 59 boolean progress = true;
60 60
61 try 61 try
62 { 62 {
63 setCurrentConnection(this); 63 setCurrentConnection(this);
64 64
65 // While progress and the connection has not changed 65 // While progress and the connection has not changed
66 while (progress && connection==this) 66 while (progress && connection==this)
67 { 67 {
68 progress=false; 68 progress=false;
69 try 69 try
70 { 70 {
71 // Parse more input 71 // Parse more input
72 if (!_parser.isComplete() && _parser.parseAvailable()) 72 if (!_parser.isComplete() && _parser.parseAvailable())
73 progress=true; 73 progress = true;
74 74
75 // Generate more output 75 // Generate more output
76 if (_generator.isCommitted() && !_generator.isComplete() && !_endp.isOutputShutdown()) 76 if (_generator.isCommitted() && !_generator.isComplete() && !_endp.isOutputShutdown())
77 if (_generator.flushBuffer()>0) 77 if (_generator.flushBuffer()>0)
78 progress=true; 78 progress = true;
79 79
80 // Flush output 80 // Flush output
81 _endp.flush(); 81 _endp.flush();
82 82
83 // Has any IO been done by the endpoint itself since last loop 83 // Has any IO been done by the endpoint itself since last loop
84 if (_asyncEndp.hasProgressed()) 84 if (_asyncEndp.hasProgressed())
85 progress=true; 85 progress = true;
86 } 86 }
87 catch (HttpException e) 87 catch (HttpException e)
88 { 88 {
89 if (LOG.isDebugEnabled()) 89 if (LOG.isDebugEnabled())
90 { 90 {
91 LOG.debug("uri="+_uri); 91 LOG.debug("uri="+_uri);
92 LOG.debug("fields="+_requestFields); 92 LOG.debug("fields="+_requestFields);
93 LOG.debug("",e); 93 LOG.debug("",e);
94 } 94 }
95 progress=true; 95 progress = true;
96 _generator.sendError(e.getStatus(), e.getReason(), null, true); 96 _generator.sendError(e.getStatus(), e.getReason(), null, true);
97 } 97 }
98 finally 98 finally
99 { 99 {
100 some_progress|=progress; 100 some_progress |= progress;
101 // Is this request/response round complete and are fully flushed? 101 // Is this request/response round complete and are fully flushed?
102 boolean parserComplete = _parser.isComplete(); 102 boolean parserComplete = _parser.isComplete();
103 boolean generatorComplete = _generator.isComplete(); 103 boolean generatorComplete = _generator.isComplete();
104 boolean complete = parserComplete && generatorComplete; 104 boolean complete = parserComplete && generatorComplete;
105 if (parserComplete) 105 if (parserComplete)
106 { 106 {
107 if (generatorComplete) 107 if (generatorComplete)
108 { 108 {
109 // Reset the parser/generator 109 // Reset the parser/generator
110 progress=true; 110 progress=true;
111 111
112 // look for a switched connection instance? 112 // look for a switched connection instance?
113 if (_response.getStatus()==HttpStatus.SWITCHING_PROTOCOLS_101) 113 if (_response.getStatus()==HttpStatus.SWITCHING_PROTOCOLS_101)
114 { 114 {
115 Connection switched=(Connection)_request.getAttribute("org.eclipse.jetty.io.Connection"); 115 Connection switched=(Connection)_request.getAttribute("org.eclipse.jetty.io.Connection");
116 if (switched!=null) 116 if (switched!=null)
117 connection=switched; 117 connection=switched;
118 } 118 }
119 119
120 reset(); 120 reset();
121 121
122 // TODO Is this still required? 122 // TODO Is this still required?
123 if (!_generator.isPersistent() && !_endp.isOutputShutdown()) 123 if (!_generator.isPersistent() && !_endp.isOutputShutdown())
124 { 124 {
125 LOG.warn("Safety net oshut!!! IF YOU SEE THIS, PLEASE RAISE BUGZILLA"); 125 LOG.warn("Safety net oshut!!! IF YOU SEE THIS, PLEASE RAISE BUGZILLA");
126 _endp.shutdownOutput(); 126 _endp.shutdownOutput();
127 } 127 }
128 } 128 }
129 else 129 else
130 { 130 {
131 // We have finished parsing, but not generating so 131 // We have finished parsing, but not generating so
132 // we must not be interested in reading until we 132 // we must not be interested in reading until we
133 // have finished generating and we reset the generator 133 // have finished generating and we reset the generator
134 _readInterested = false; 134 _readInterested = false;
135 LOG.debug("Disabled read interest while writing response {}", _endp); 135 LOG.debug("Disabled read interest while writing response {}", _endp);
136 } 136 }
137 } 137 }
138 } 138 }
139 } 139 }
140 } 140 }
141 finally 141 finally
142 { 142 {
143 setCurrentConnection(null); 143 setCurrentConnection(null);
144 144
145 // return buffers 145 // return buffers
146 _parser.returnBuffers(); 146 _parser.returnBuffers();
147 _generator.returnBuffers(); 147 _generator.returnBuffers();
148 148
149 // Safety net to catch spinning 149 // Safety net to catch spinning
150 if (some_progress) 150 if (some_progress)
151 _total_no_progress=0; 151 _total_no_progress = 0;
152 else 152 else
153 { 153 {
154 _total_no_progress++; 154 _total_no_progress++;
155 if (NO_PROGRESS_INFO>0 && _total_no_progress%NO_PROGRESS_INFO==0 && (NO_PROGRESS_CLOSE<=0 || _total_no_progress< NO_PROGRESS_CLOSE)) 155 if (NO_PROGRESS_INFO>0 && _total_no_progress%NO_PROGRESS_INFO==0 && (NO_PROGRESS_CLOSE<=0 || _total_no_progress< NO_PROGRESS_CLOSE))
156 LOG.info("EndPoint making no progress: "+_total_no_progress+" "+_endp+" "+this); 156 LOG.info("EndPoint making no progress: "+_total_no_progress+" "+_endp+" "+this);
157 if (NO_PROGRESS_CLOSE>0 && _total_no_progress==NO_PROGRESS_CLOSE) 157 if (NO_PROGRESS_CLOSE>0 && _total_no_progress==NO_PROGRESS_CLOSE)
158 { 158 {
159 LOG.warn("Closing EndPoint making no progress: "+_total_no_progress+" "+_endp+" "+this); 159 LOG.warn("Closing EndPoint making no progress: "+_total_no_progress+" "+_endp+" "+this);
160 if (_endp instanceof SelectChannelEndPoint) 160 if (_endp instanceof SelectChannelEndPoint)
161 ((SelectChannelEndPoint)_endp).getChannel().close(); 161 ((SelectChannelEndPoint)_endp).getChannel().close();
162 } 162 }
163 } 163 }
164 } 164 }
165 return connection; 165 return connection;
166 } 166 }
167 167
168 public void onInputShutdown() throws IOException 168 public void onInputShutdown() throws IOException
169 { 169 {
170 // If we don't have a committed response and we are not suspended 170 // If we don't have a committed response and we are not suspended
171 if (_generator.isIdle()) 171 if (_generator.isIdle())
172 { 172 {
173 // then no more can happen, so close. 173 // then no more can happen, so close.
174 _endp.close(); 174 _endp.close();
175 } 175 }
176 176
177 // Make idle parser seek EOF 177 // Make idle parser seek EOF
178 if (_parser.isIdle()) 178 if (_parser.isIdle())
179 _parser.setPersistent(false); 179 _parser.setPersistent(false);
180 } 180 }
181 181
182 @Override 182 @Override
183 public void reset() 183 public void reset()
184 { 184 {
185 _readInterested = true; 185 _readInterested = true;
186 LOG.debug("Enabled read interest {}", _endp); 186 LOG.debug("Enabled read interest {}", _endp);
187 super.reset(); 187 super.reset();
188 } 188 }
189 189
190 @Override 190 @Override
191 public boolean isSuspended() 191 public boolean isSuspended()
192 { 192 {
193 return !_readInterested || super.isSuspended(); 193 return !_readInterested || super.isSuspended();
194 } 194 }
195 } 195 }