Limiting size of HTTP response body

1,109 views
Skip to first unread message

jo...@mobify.me

unread,
Jan 7, 2015, 1:48:01 AM1/7/15
to golan...@googlegroups.com
Hey Gophers,

I'm writing an HTTP Client that loads chunked responses bodies with arbitrary sizes into memory.  

To prevent large responses from consuming too much memory, I'd like to be able to limit the number of bytes that I read from any particular response.

My thinking is to use a struct similar to `io.LimitedReader` to wrap the response's Body and implementing my own Read method which explodes if the caller reads more than a limited number of bytes.

Here is my initial implementation: https://play.golang.org/p/aJHdROxce8

I'm a complete #golang noob, so any comments on whether I'm coming at this from the right angle would be very helpful!

Thanks,

John

Brad Fitzpatrick

unread,
Jan 7, 2015, 1:06:17 PM1/7/15
to jo...@mobify.me, golang-nuts
Seems good. I suppose the caller is still responsible for calling Request.Body.Close after the error to ultimately close the TCP connection with all the unwanted data arriving?




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Boxall

unread,
Jan 7, 2015, 1:51:42 PM1/7/15
to golan...@googlegroups.com, jo...@mobify.me
Good point thank you!

jb
Reply all
Reply to author
Forward
0 new messages