Add back consumeAndClose functionality. (#7608)

* consume bodies for action button integrations, webrtc gateway, oauth
endpoint

* Fixing a couple more places, switching to io.Copy to ioutil.Discard, adding a comment to help prevent future performance regressions
This commit is contained in:
Christopher Speller
2017-10-12 08:00:53 -07:00
committed by GitHub
parent 521e27f4ac
commit 3461a7b207
7 changed files with 17 additions and 10 deletions

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"html"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
@@ -701,8 +700,7 @@ func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session
} else {
pushResponse := model.PushResponseFromJson(resp.Body)
if resp.Body != nil {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
consumeAndClose(resp)
}
if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_REMOVE {

View File

@@ -685,7 +685,7 @@ func (a *App) AuthorizeOAuthUser(w http.ResponseWriter, r *http.Request, service
return nil, "", stateProps, model.NewAppError("AuthorizeOAuthUser", "api.user.authorize_oauth_user.token_failed.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
ar = model.AccessResponseFromJson(resp.Body)
resp.Body.Close()
consumeAndClose(resp)
if ar == nil {
return nil, "", stateProps, model.NewAppError("AuthorizeOAuthUser", "api.user.authorize_oauth_user.bad_response.app_error", nil, "response_body="+string(bodyBytes), http.StatusInternalServerError)

View File

@@ -676,7 +676,7 @@ func GetOpenGraphMetadata(url string) *opengraph.OpenGraph {
l4g.Error("GetOpenGraphMetadata request failed for url=%v with err=%v", url, err.Error())
return og
}
defer res.Body.Close()
defer consumeAndClose(res)
if err := og.ProcessHTML(res.Body); err != nil {
l4g.Error("GetOpenGraphMetadata processing failed for url=%v with err=%v", url, err.Error())
@@ -712,7 +712,7 @@ func (a *App) DoPostAction(postId string, actionId string, userId string) *model
if err != nil {
return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest)
}
defer resp.Body.Close()
defer consumeAndClose(resp)
if resp.StatusCode != http.StatusOK {
return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, fmt.Sprintf("status=%v", resp.StatusCode), http.StatusBadRequest)

View File

@@ -80,8 +80,7 @@ func (a *App) DoSecurityUpdateCheck() {
}
bulletins := model.SecurityBulletinsFromJson(res.Body)
ioutil.ReadAll(res.Body)
res.Body.Close()
consumeAndClose(res)
for _, bulletin := range bulletins {
if bulletin.AppliesToVersion == model.CurrentVersion {

View File

@@ -5,6 +5,8 @@ package app
import (
"crypto/tls"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
@@ -208,3 +210,11 @@ func (a *App) StopServer() {
a.Srv.GracefulServer = nil
}
}
// This is required to re-use the underlying connection and not take up file descriptors
func consumeAndClose(r *http.Response) {
if r.Body != nil {
io.Copy(ioutil.Discard, r.Body)
r.Body.Close()
}
}

View File

@@ -109,7 +109,7 @@ func (a *App) TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.
if resp, err := utils.HttpClient(false).Do(req); err != nil {
l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.event_post.error"), err.Error())
} else {
defer resp.Body.Close()
defer consumeAndClose(resp)
webhookResp := model.OutgoingWebhookResponseFromJson(resp.Body)
if webhookResp != nil && webhookResp.Text != nil {

View File

@@ -62,7 +62,7 @@ func GetWebrtcToken(sessionId string) (string, *model.AppError) {
if rp, err := utils.HttpClient(true).Do(rq); err != nil {
return "", model.NewAppError("WebRTC.Token", "model.client.connecting.app_error", nil, err.Error(), http.StatusInternalServerError)
} else if rp.StatusCode >= 300 {
defer rp.Body.Close()
defer consumeAndClose(rp)
return "", model.AppErrorFromJson(rp.Body)
} else {
janusResponse := model.GatewayResponseFromJson(rp.Body)