removed lock from HttpParser. Altered way that returnBuffers called to avoid nulling buffer for other thread

This commit is contained in:
Greg Wilkins 2011-11-14 12:08:21 +11:00
parent ca8873593a
commit 26bf17f9c1
3 changed files with 63 additions and 72 deletions

View File

@ -83,8 +83,6 @@ public class HttpParser implements Parser
protected int _chunkPosition; protected int _chunkPosition;
private boolean _headResponse; private boolean _headResponse;
private Lock _lock = new ReentrantLock(); // Ensure only a single parsing/resetting thread
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
/** /**
* Constructor. * Constructor.
@ -252,7 +250,6 @@ public class HttpParser implements Parser
*/ */
public int parseNext() throws IOException public int parseNext() throws IOException
{ {
_lock.lock();
try try
{ {
int progress=0; int progress=0;
@ -278,6 +275,7 @@ public class HttpParser implements Parser
{ {
_state=STATE_END; _state=STATE_END;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
@ -340,6 +338,7 @@ public class HttpParser implements Parser
if (!isComplete() && !isIdle()) if (!isComplete() && !isIdle())
throw new EofException(); throw new EofException();
returnBuffers();
return -1; return -1;
} }
length=_buffer.length(); length=_buffer.length();
@ -453,6 +452,7 @@ public class HttpParser implements Parser
_state=STATE_SEEKING_EOF; _state=STATE_SEEKING_EOF;
_handler.headerComplete(); _handler.headerComplete();
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
break; break;
@ -482,6 +482,7 @@ public class HttpParser implements Parser
_state=STATE_SEEKING_EOF; _state=STATE_SEEKING_EOF;
_handler.headerComplete(); _handler.headerComplete();
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
} }
@ -645,7 +646,8 @@ public class HttpParser implements Parser
_handler.headerComplete(); _handler.headerComplete();
_state=_persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF; _state=_persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
break; returnBuffers();
return 1;
default: default:
_state=STATE_CONTENT; _state=STATE_CONTENT;
@ -850,6 +852,7 @@ public class HttpParser implements Parser
{ {
_state=_persistent?STATE_END:STATE_SEEKING_EOF; _state=_persistent?STATE_END:STATE_SEEKING_EOF;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
@ -869,6 +872,7 @@ public class HttpParser implements Parser
{ {
_state=_persistent?STATE_END:STATE_SEEKING_EOF; _state=_persistent?STATE_END:STATE_SEEKING_EOF;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
} }
// TODO adjust the _buffer to keep unconsumed content // TODO adjust the _buffer to keep unconsumed content
return 1; return 1;
@ -903,6 +907,7 @@ public class HttpParser implements Parser
_eol=_buffer.get(); _eol=_buffer.get();
_state=_persistent?STATE_END:STATE_SEEKING_EOF; _state=_persistent?STATE_END:STATE_SEEKING_EOF;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
else else
@ -933,6 +938,7 @@ public class HttpParser implements Parser
_eol=_buffer.get(); _eol=_buffer.get();
_state=_persistent?STATE_END:STATE_SEEKING_EOF; _state=_persistent?STATE_END:STATE_SEEKING_EOF;
_handler.messageComplete(_contentPosition); _handler.messageComplete(_contentPosition);
returnBuffers();
return 1; return 1;
} }
else else
@ -979,10 +985,6 @@ public class HttpParser implements Parser
_state=STATE_SEEKING_EOF; _state=STATE_SEEKING_EOF;
throw e; throw e;
} }
finally
{
_lock.unlock();
}
} }
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
@ -1047,9 +1049,6 @@ public class HttpParser implements Parser
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
public void reset() public void reset()
{
_lock.lock();
try
{ {
// reset state // reset state
_contentView.setGetIndex(_contentView.putIndex()); _contentView.setGetIndex(_contentView.putIndex());
@ -1093,19 +1092,12 @@ public class HttpParser implements Parser
_body.setMarkIndex(-1); _body.setMarkIndex(-1);
_buffer=_header; _buffer=_header;
} returnBuffers();
finally
{
_lock.unlock();
}
} }
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
public void returnBuffers() public void returnBuffers()
{
_lock.lock();
try
{ {
if (_body!=null && !_body.hasContent() && _body.markIndex()==-1 && _buffers!=null) if (_body!=null && !_body.hasContent() && _body.markIndex()==-1 && _buffers!=null)
{ {
@ -1124,11 +1116,6 @@ public class HttpParser implements Parser
_header=null; _header=null;
} }
} }
finally
{
_lock.unlock();
}
}
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
public void setState(int state) public void setState(int state)

View File

@ -125,8 +125,11 @@ public class AsyncHttpConnection extends AbstractHttpConnection implements Async
finally finally
{ {
setCurrentConnection(null); setCurrentConnection(null);
if (!_request.isAsyncStarted())
{
_parser.returnBuffers(); _parser.returnBuffers();
_generator.returnBuffers(); _generator.returnBuffers();
}
// Safety net to catch spinning // Safety net to catch spinning
if (some_progress) if (some_progress)

View File

@ -66,6 +66,7 @@ public class AsyncRequestReadTest
@Test @Test
public void test() throws Exception public void test() throws Exception
{ {
total=0;
final Socket socket = new Socket("localhost",connector.getLocalPort()); final Socket socket = new Socket("localhost",connector.getLocalPort());
byte[] content = new byte[16*4096]; byte[] content = new byte[16*4096];