[erlang-questions] making http client close connections

Oscar Hellström oscar@REDACTED
Wed Jun 10 00:47:50 CEST 2009


Ok... you essentially want to stop waiting for a response, not closing the socket after a response, right?

If you look at the interface for lhttpc though, you should be able to implement a asynchronous HTTP request by spawning a lhttpc_client. You can look at what the lhttpc module does, and kinda copy this, bit in a specific way.

Essentially you would be doing something like:
Client = spawn_link(lhttpc_client, request, [self(), URL, Method, Hdrs, Body]),
...
Here you would need to expect to get a {response, Child, Response} message back. When you don't feel like waiting for the poll, just do:
unlink(Client),
kill(Client, timeout), % or kill or whatever...
You could also use the way it's done in lhttpc:kill_client/1 if you want to make sure that there are no messages send to you between you gave up and the Client process was killed.

Since the socket is owned by the Client process, it would take it with it. If you do get a response and server sends Connection: close, you won't pass this socket to the manager to be re-used either. I don't see why you wouldn't want to re-use it though.

I imagine that limiting this to one socket / system should be something you'd want to implement in your logic though.

Maybe if you explained what you're trying to do from the beginning, you wouldn't get all the useless replies...

Good luck :)

----- "Joel Reymont" <joelr1@REDACTED> wrote:

> The connection I'm trying to close on the client side is a long-poll 
> 
> connection. I cannot use Connection: close in the headers of the  
> request that establishes it since that will cause the connection to  
> close, i.e. stop being long-poll.
> 
> On Jun 9, 2009, at 9:12 PM, Oscar Hellström wrote:
> 
> > Hi Joel,
> >
> > What I meant was to send the Connection: close in the *headers* of 
> 
> > the *request*... That signals that the *client* isn't prepared to  
> > continue to use the connection after the request/response.
> >
> > To make it a bit more clear, you could read this:
> > http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10
> >
> > IIRC inets will notice this, and will close the connection.  
> > Otherwise, have you tried to use {version, "HTTP/1.1"} as on  
> > HTTPOption?
> 
> ---
> Mac hacker with a performance bent
> http://www.linkedin.com/in/joelreymont

-- 
Oscar Hellström, oscar@REDACTED
Phone:  +44 (0)798 45 44 773
Mobile: +44 (0)207 65 50 337
Web:    http://www.erlang-consulting.com



More information about the erlang-questions mailing list