Skip to content

Instantly share code, notes, and snippets.

@luv2code
Created June 18, 2013 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luv2code/5807567 to your computer and use it in GitHub Desktop.
Save luv2code/5807567 to your computer and use it in GitHub Desktop.
resp.Body.Read(buf) always reads in spurts of 4K characters. I need it to be much smaller. so that the chunks come through immediately
resp, err := http.Get(url)
log.Printf("stream started: %s", url)
defer func () {
log.Printf("stream closing: %s", url)
resp.Body.Close()
}()
if err != nil {
panic(err)
}
//capture only one letter at a time
buf := make([]byte, 1)
cnt, err := resp.Body.Read(buf)
for cnt > 0 {
//this only prints in bursts of 4K letters
//each chunk is a JSON object of about 200 characters. delineated by a newline
//There is approximately 4 seconds between each chunk.
//I need to write each chunk to a writer in realtime.
//This writes bursts of 20 chunks once every minute or so.
log.Printf("this is a single letter: %s", string(buf))
cnt, err = resp.Body.Read(buf)
}
@sam-falvo
Copy link

You could perhaps set up a custom Transport for net/http to use. One of the fields you get to control is Body, allowing you to set up your own reader. It's pretty low-level, but it might be the ticket you're looking for.

@luv2code
Copy link
Author

This is being commented on this g+ post as well:
https://plus.google.com/107663931165035909387/posts/5hmF4UAnAPy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment