private/protocol/restjson/restjson: Use json.Decoder to decrease memory allocation
Update RESTJSON protocol unmarshaler to use json.Decoder instead of ioutil.ReadAll to reduce allocations.
Related to:
@@ -7,7 +7,7 @@ package restjson
import (
"encoding/json"
- "io/ioutil"
+ "io"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
@@ -54,26 +54,17 @@ func UnmarshalMeta(r *request.Request) {
// UnmarshalError unmarshals a response error for the REST JSON protocol.
func UnmarshalError(r *request.Request) {
defer r.HTTPResponse.Body.Close()
- code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
- bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body)
- if err != nil {
- r.Error = awserr.NewRequestFailure(
- awserr.New("SerializationError", "failed reading REST JSON error response", err),
- r.HTTPResponse.StatusCode,
- r.RequestID,
- )
- return
- }
- if len(bodyBytes) == 0 {
+
+ var jsonErr jsonErrorResponse
+ err := json.NewDecoder(r.HTTPResponse.Body).Decode(&jsonErr)
+ if err == io.EOF {
r.Error = awserr.NewRequestFailure(
awserr.New("SerializationError", r.HTTPResponse.Status, nil),
r.HTTPResponse.StatusCode,
r.RequestID,
)
return
- }
- var jsonErr jsonErrorResponse
- if err := json.Unmarshal(bodyBytes, &jsonErr); err != nil {
+ } else if err != nil {
r.Error = awserr.NewRequestFailure(
awserr.New("SerializationError", "failed decoding REST JSON error response", err),
r.HTTPResponse.StatusCode,
@@ -82,6 +73,7 @@ func UnmarshalError(r *request.Request) {
return
}
+ code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
if code == "" {
code = jsonErr.Code
}
