http包的Respose,不全部读取就不能关闭吗?

198 views
Skip to first unread message

Wendal Chen

unread,
Mar 15, 2013, 8:46:02 PM3/15/13
to golang...@googlegroups.com, golang-nuts
golang 1.0.3
平台: windows, opensuse 11.4, F14, 均可重现
演示访问openSUSE的iso镜像,但不读取Body的数据

package main

import (
"fmt"
"net/http"
)

func main() {
if err != nil {
panic(err)
}
fmt.Println("Resp code", resp.StatusCode)
        // 只读取少量或根本就不读取,然后关闭Body
resp.Body.Close() // 卡住, 即使断网也不继续运行了
}

不完全读取数据之前,不可关闭??
注: 与HEAD的场景无关.

--
Wendal Chen
GuangDong China

David DENG

unread,
Mar 15, 2013, 8:47:43 PM3/15/13
to golan...@googlegroups.com, golang...@googlegroups.com
Use English to post your questions, please.

David

Wendal Chen

unread,
Mar 15, 2013, 8:52:06 PM3/15/13
to golang...@googlegroups.com, golang-nuts
So sorry, I send it in Chinese.

///////////////////////////////////////////////////////////////////////
golang 1.0.3
windows 7
opensuse 11.4

///////////////////////////////////////////////////////////////////////

package main

import (
"fmt"
"net/http"
)

func main() {
if err != nil {
panic(err)
}
fmt.Println("Resp code", resp.StatusCode)
        // read some data but not all
resp.Body.Close() //  stop here, even shutdown network
}

BTW: Don't motion HEAD

////////////////////////////////////////////////////////////////////////////////////////////////////////////

Dave Cheney

unread,
Mar 15, 2013, 8:54:05 PM3/15/13
to Wendal Chen, golang...@googlegroups.com, golang-nuts
Calling resp.Body.Close() must read the _entire_ response body (some
700mb). I don't know what you meant about HEAD, but that would sound
like the HTTP verb you are looking for.
> --
> 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/groups/opt_out.
>
>

Wendal Chen

unread,
Mar 15, 2013, 9:06:30 PM3/15/13
to Dave Cheney, golang...@googlegroups.com, golang-nuts
So, I can't close it before read ALL data?

2013/3/16 Dave Cheney <da...@cheney.net>

Dave Cheney

unread,
Mar 15, 2013, 9:10:32 PM3/15/13
to Wendal Chen, golang...@googlegroups.com, golang-nuts
That is correct. You _must_ close the response body, and closing the
response body _must_ read all the data on the connection so it can
return the connection to the connection pool.

Tong Sun

unread,
Mar 16, 2013, 12:24:30 AM3/16/13
to golan...@googlegroups.com, Wendal Chen, golang...@googlegroups.com
Not to question the decision, but that seem really counter-intuitive, as I think people stop web requests in the middle all the times. 

Why go insists on reading the _entire_ response body before resp.Body.Close(), even some 700mb has to be read and discarded? Again, just curious to know. Also, any other method that allow people  stop in the middle of web requests? 

Thanks
Message has been deleted

dio...@gmail.com

unread,
May 21, 2020, 2:38:02 PM5/21/20
to golang-nuts
You ever get an answer to this? 

Robert Engels

unread,
May 21, 2020, 3:59:37 PM5/21/20
to dio...@gmail.com, golang-nuts
You can stop on the middle of the request - but then you cannot reuse the http connection. 

If you want more advanced reuse you need to transfer in chunks and have a higher level protocol

Also, I think this is addressed in HTTP2 since all connections are multiplexed anyway. 

On May 21, 2020, at 1:37 PM, dio...@gmail.com wrote:


--
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.
Reply all
Reply to author
Forward
0 new messages