hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sam Berlin" <sber...@limepeer.com>
Subject Blocking & Non-Blocking I/O
Date Mon, 06 Jun 2005 00:13:36 GMT
Hi Folks,

I recall earlier posts mentioning that HttpClient 4.0 was going to offer
blocking & non-blocking variations, so that multiple requests (or responses,
in the case of the server side) could use a single thread.  Since I did
offer LimeWire's support with some of HttpClient 4.0's development, I figure
I should follow up on that. :)

We've recently completed converting LimeWire's messaging code to be
non-blocking while still allowing blocking use.  The package has two
external depencies (used in a single file, for easier error reporting) which
can easily be removed (other than that, it uses commons logging & java.net,
java.util, java.io, and java.nio classes).

The architecture may work well for HttpClient.  The basic concept is that
reading & writing use the decorator pattern.  So, for instance, you could
have a HttpHeaderWriter, HttpBodyWriter, and a DeflaterWriter.  When writing
headers, you would set the 'writer' to be an HttpHeaderWriter.  When writing
the body, you would set the writer to be the HttpBodyWriter, potentially
settings its writer to be the DeflaterWriter.  With reading, it'd be very
similar, an HttpHeaderReader, HttpBodyReader, and InflaterReader.  There's
also the equivilent of BufferedOutputStream, a DelayedBufferWriter, which
one-ups BufferedOutputStream in the sense that it will try to buffer all
output (writing only when the buffer is full, to reduce the nmber TCP
packets sent), but if the buffer hasn't filled in X milliseconds, it will
force the data to be written.  It's also possible to add a ThrottleWriter
(and share a Throttle among multiple ThrottleWriters) which will limit the
outgoing throughput (allowing a fair share for each ThrottleWriter).

It is possible to use blocking i/o for a portion of the transfer, and then
convert it to non-blocking, although I'm not sure how useful it is for
HttpClient.  Internally, the transfer is actually non-blocking, but it
emulates the blocking portion (somewhat better than Sun's blocking emulation
with configureBlocking(true)).  To use it, all you have to do is change new
Socket(...) to new NIOSocket(...), and new ServerSocket(...) to new
NIOServerSocket(...).  I do not know how convenient this is with the
SocketFactories that HttpClient has.  I imagine there could be new
NIOFactories which would subclass the existing ones.

Non-blocking connecting currently isn't supported (neither is non-blocking
accepting), but they're trivial to add and we're likely going to add support

As an example DeflaterWriter & InflaterReader, see
ection/DeflaterWriter.java?r=1.2 (Deflater) and
ection/InflaterReader.java?r=1.2 (Inflater).  Right now they're in a
separate package, but they'd fit really well in the io package.

As an example 'top level writer/reader', see our MessageReader &
ection/MessageReader.java?r=1.4 and
ection/MessageWriter.java?r=1.2 .


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

View raw message