cch1 / http.async.client

Async Http Client - Clojure
http://cch1.github.com/http.async.client
267 stars 40 forks source link

how do I terminate a stream-seq #7

Closed sandys closed 12 years ago

sandys commented 13 years ago

I have the foll code: (:require [http.async.client :as c] [org.danlarkin.json :as j])) (defn track [s](println %28first %28c/string %28c/stream-seq :post "http://stream.twitter.com/1/statuses/filter.json" :body {"track" %28str s)} :auth {:user u :password p})))))

However the code seems to hang - I thought that "first" would extract and eval the first closure from a lazy-seq and drop the rest of it.

I need this because in case of certain errors, I need to shut down the client - but keep a stream running otherwise. How should I approach this - by constructing my own lazy-seq or something ?

thanks!

neotyk commented 13 years ago

Hi sandys,

first just takes first element from seq. code doesn't terminate because client is still running request.

You probably want to process entire response seq: (doall [a (c/string (c/stream-seq ..))](println a))

What do you mean by certain errors?

You basically have to get hold of response returned by (c/stream-seq ..) to be able to work on response, not only reading it's contents, so you can (c/cancel ..) it.

HTH, Hubert.

sandys commented 13 years ago

thanks for answering.

I cannot process entire response seq - I am actually reading a twitter stream. Let me explain what I mean by errors: Twitter rate-limits requests to its stream. So, if I start getting 404 responses, I need to sleep for 60 seconds or so and then resume reading the stream.

So the problem is this - I should read the Twitter stream until I get an error. Then I should sleep for 6 seconds and then resume reading of the stream. If I still get an error, then I shut down the client.

neotyk commented 13 years ago

Looks like you need a reconnection strategy, this can only be implemented at callbacks level for now. http://j.mp/b57MRP Look for error handing, your error handler should start new request, or schedule it. I'll try to add reconnection strategies for seq interface for next release.

neotyk commented 12 years ago

To close request you should use cancel: http://neotyk.github.com/http.async.client/doc/http.async.client.html#var-cancel