/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
go/libkb/httputil.go
37 строк
769 B
Joshua Blum
further CI fixes (#25292)
30 ноя 2022, 23:03
Не верифицирован
30 ноя 2022, 23:03
1be3518
Код
Авторство
О чём код?
package libkb import ( "fmt" "io" "net/http" ) func discardAndClose(rc io.ReadCloser) error { _, _ = io.Copy(io.Discard, rc) return rc.Close() } // DiscardAndCloseBody reads as much as possible from the body of the // given response, and then closes it. // // This is because, in order to free up the current connection for // re-use, a response body must be read from before being closed; see // http://stackoverflow.com/a/17953506 . // // Instead of doing: // // res, _ := ... // defer res.Body.Close() // // do // // res, _ := ... // defer DiscardAndCloseBody(res) // // instead. func DiscardAndCloseBody(resp *http.Response) error { if resp == nil { return fmt.Errorf("Nothing to discard (http.Response was nil)") } return discardAndClose(resp.Body) }