Breaking a keep alive connection

Breaking a keep alive connection

am 29.09.2004 18:50:11 von moseley

I'm using keep alives and the form of $ua->get() that uses a callback
function to read the data as it arrives.

If the callback function dies will the connection always be broken?
That is, will the next request to that server be a new connection, not
an existing open connection from a previous keep alive request?

I assume if I've only read the first chunk out of a very large
response then the connection will be broken. But, I'm not clear what
happens if the fetched document is very small (like the first chunk is
the entire document).

Does size matter? Or would LWP drop the connection regardless?

Also, is there a way to ask LWP if a request would be to an open
connection before making the actual connection?

--
Bill Moseley
moseley@hank.org

Re: Breaking a keep alive connection

am 29.09.2004 19:47:23 von gisle

Bill Moseley writes:

> I'm using keep alives and the form of $ua->get() that uses a callback
> function to read the data as it arrives.
>
> If the callback function dies will the connection always be broken?

Yes, unless it dies after the last part of the response has actually
been read.

> That is, will the next request to that server be a new connection, not
> an existing open connection from a previous keep alive request?
>
> I assume if I've only read the first chunk out of a very large
> response then the connection will be broken. But, I'm not clear what
> happens if the fetched document is very small (like the first chunk is
> the entire document).
>
> Does size matter? Or would LWP drop the connection regardless?

If LWP has provided you with the complete content when your callback
dies, then the connection is kept up.

> Also, is there a way to ask LWP if a request would be to an open
> connection before making the actual connection?

You could roam around in the $ua->conn_cache object, but it is not
really documented how the connections are indexed, or what the
connection objects actually are.

If you want to make sure LWP has no more connections open, you can
call $au->conn_cache->drop;

Regards,
Gisle

Re: Breaking a keep alive connection

am 01.10.2004 18:27:27 von moseley

On Wed, Sep 29, 2004 at 09:50:11AM -0700, Bill Moseley wrote:
> I'm using keep alives and the form of $ua->get() that uses a callback
> function to read the data as it arrives.
>
> If the callback function dies will the connection always be broken?

Well, according to ethereal, it seems like the answer is yes. Even
when the content is very small and has all been received.

So, my next exciting question (I know you are excited due to the
response from my last post ;) is there a way in the callback function
to tell if the request has actually finished?

If there is a way then I can set a flag in my callback and instead of
aborting inside the callback (causing the connection to close) I could
deal with the flag outside the $ua->get call and not force the
connection closed.

I'm also still wondering if I can somehow ask LWP if a given URL would
be used with an open connection or not. I didn't see anything in the
ConnCache docs.



--
Bill Moseley
moseley@hank.org

Re: Breaking a keep alive connection

am 02.10.2004 01:06:22 von moseley

Hi Gisle,

On Wed, Sep 29, 2004 at 10:47:23AM -0700, Gisle Aas wrote:
> Bill Moseley writes:
>
> > I'm using keep alives and the form of $ua->get() that uses a callback
> > function to read the data as it arrives.
> >
> > If the callback function dies will the connection always be broken?
>
> Yes, unless it dies after the last part of the response has actually
> been read.

I'm not seeing that. If I fetch a small document and in my callback I
print out the content passed in I see all the content. Then if I die
the next request is a new connection.

LWP::UserAgent 2.32



#!perl
use warnings;
use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new( keep_alive => 1 );

my $request = HTTP::Request->new('GET', 'http://localhost/apache/1.html');

my $callback = sub {
my ( $content, $response ) = @_;
print "Content-Length: ",
$response->content_length,
". Passed content size: ",
length($content),
".\n";
die "dead\n";
};

my $response = $ua->simple_request( $request, $callback, 4096 );

print "Connection header from second request: ",
$ua->simple_request( $request )->header('Connection'),
"\n";

$ perl lwp.pl
Content-Length: 181. Passed content size: 181.
Connection header from second request: Keep-Alive

Remove the "die" and the second connection is on the same connection
(and no Keep-Alive header).

Thanks,


--
Bill Moseley
moseley@hank.org