summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2017-10-12 08:00:53 -0700
committerGitHub <noreply@github.com>2017-10-12 08:00:53 -0700
commit3461a7b20704464ee3c19a3dd31805e4c5c1fc4f (patch)
treeb14ad4935a9453157024e638b5c468a1833e0eae
parent521e27f4ace125b47879b38edbad07d7c21a54e6 (diff)
downloadchat-3461a7b20704464ee3c19a3dd31805e4c5c1fc4f.tar.gz
chat-3461a7b20704464ee3c19a3dd31805e4c5c1fc4f.tar.bz2
chat-3461a7b20704464ee3c19a3dd31805e4c5c1fc4f.zip
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
-rw-r--r--app/notification.go4
-rw-r--r--app/oauth.go2
-rw-r--r--app/post.go4
-rw-r--r--app/security_update_check.go3
-rw-r--r--app/server.go10
-rw-r--r--app/webhook.go2
-rw-r--r--app/webrtc.go2
7 files changed, 17 insertions, 10 deletions
diff --git a/app/notification.go b/app/notification.go
index 3df4a789f..2a8f9ff2e 100644
--- a/app/notification.go
+++ b/app/notification.go
@@ -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 {
diff --git a/app/oauth.go b/app/oauth.go
index 909d16628..5a02f6238 100644
--- a/app/oauth.go
+++ b/app/oauth.go
@@ -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)
diff --git a/app/post.go b/app/post.go
index 94f4acbe7..d51ba7103 100644
--- a/app/post.go
+++ b/app/post.go
@@ -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)
diff --git a/app/security_update_check.go b/app/security_update_check.go
index 773556f5e..32d1f4d31 100644
--- a/app/security_update_check.go
+++ b/app/security_update_check.go
@@ -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 {
diff --git a/app/server.go b/app/server.go
index 3802c2eec..c509d0440 100644
--- a/app/server.go
+++ b/app/server.go
@@ -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()
+ }
+}
diff --git a/app/webhook.go b/app/webhook.go
index d3d9bbf8b..dbe444a25 100644
--- a/app/webhook.go
+++ b/app/webhook.go
@@ -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 {
diff --git a/app/webrtc.go b/app/webrtc.go
index 65bbac7cd..2d0bb0708 100644
--- a/app/webrtc.go
+++ b/app/webrtc.go
@@ -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)