Created
June 18, 2013 17:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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
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.