hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitry Potapov <potapo...@gmail.com>
Subject Re: Stale connection check in NHttpConnectionBase and DefaultListeningIOReactor
Date Fri, 06 Jul 2012 07:11:10 GMT
On Fri, Jul 6, 2012 at 1:30 AM, Oleg Kalnichevski <olegk@apache.org> wrote:
> On Thu, 2012-07-05 at 22:25 +0400, Dmitry Potapov wrote:
>> Hello everyone,
>>
>> I develop streaming HTTP server for my project and used HttpCore NIO
>> for this purpose.
>> Interruptible heavy-weight task takes place before streaming start,
>> so, I'm interesting in any mechanism that provides connection status
>> check, because I have tons of requests which terminates connection
>> before I start streaming.
>> APIs I've already tried:
>>
>> 1. HttpAsyncExchange.setCallback() — doesn't work, cancel() function
>> of the registered callback was never called. Doesn't matter how
>> connection was terminated, with RST or FIN.
>
> Dmitry
>
> There is a test case specifically intended to test Cancellable callback
> for HTTP exchanges
>
> https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java
This was a first file I've read before implementing my own Cancellable.

>
> If this functionality is broken I would like to know how the problem can
> be reproduced.

I've prepared complete test for this.
Source code and output are placed on github: https://gist.github.com/3058489
It can be compiler and run using:
javac -cp lib/httpcore-4.2.1.jar:lib/httpcore-nio-4.2.1.jar HttpServer.java
java -cp lib/httpcore-4.2.1.jar:lib/httpcore-nio-4.2.1.jar:. HttpServer

Test structure:
1. Create async server with DefaultListeningIOReactor and start it in
separate thread
2. Sleep for 1 second
3. Send request as it done in TestHttpAsyncHandlerCancellable.java
4. Close request socket

What is done in request handler:
1. Set chunked input stream entity in response object.
1.a. InputStreamEntiry is created on top of PipedInputStream, which
corresponing PipedOutputStream will be passed to worker thread in step
4.
2. Set callback for the HttpAsyncExchange. Callback will be passed to
worker thread in step 4.
3. Submit basic async response producer
4. Start separate thread which will do some work and then produce output
4.a. (Heavy-weight task emulation) Three times check if connection is
still alive and wait 1 second
4.b. (Heave-weight output emulation) Three times check if connection
is still alive and write response chunk
4.c. Close output stream, which causes InputStreamEntity to send
null-length chunk

I hope this will help to locate problem.

>
>> 2. NHttpConnectionBase.isStale() — doesn't work, as it does nothing
>> but session.isClosed(), which significantly differs from
>> HttpConnection.isStale() behaviour.
>
> The stale connection check is needed for blocking connections only as
> the blocking I/O model provides no way of finding out whether a
> connection is half-closed unless a attempt is made to read from the
> underlying socket. Non-blocking connections can immediately react to I/O
> events due to their asynchronous nature and can either be open or
> closed, but never stale. The behavior of #isStale method is different
> because blocking and non-blocking connections work entirely differently.

Ok, this makes sense. Thanks for the explanation.

>
>> 3. NHttpConnectionBase.getSocket().sendUrgentData() — doesn't work, as
>> IOReactors configures socket with non-blocking mode, and I haven't
>> found way to change this (most probably this shouldn't be changed).
>>
>> Is there any other ways I missed? Is there any mechanism (even
>> expensive one) to check connection status?
>>
>> P.S. I'm using version 4.2.1.
>> P.P.S I'm using InputStreamEntity over PipedInputStream for streaming.
>>
>
> I am not entirely sure InputStream interface is the best fit for
> streaming large amount of data asynchronously. You should consider using
> HttpAsyncContentProducer instead (probably along with SharedOutputBuffer
> if the content is generated by a different thread).
>
> https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/HttpAsyncContentProducer.java
> https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/util/SharedOutputBuffer.java

I'll take a look at this, thanks. However, I doubt this can have
impact on Cancellable callback behaviour.

>
> Hope this helps somewhat.
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
> For additional commands, e-mail: dev-help@hc.apache.org
>

Please, let me know if I'm doing something wrong.

-- 
Thanks in advance,
Dmitry

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


Mime
View raw message