-
Notifications
You must be signed in to change notification settings - Fork 53
/
middleware_close_response_body.go
79 lines (66 loc) · 2.54 KB
/
middleware_close_response_body.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package http
import (
"context"
"github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
"io"
"io/ioutil"
)
// AddErrorCloseResponseBodyMiddleware adds the middleware to automatically
// close the response body of an operation request if the request response
// failed.
func AddErrorCloseResponseBodyMiddleware(stack *middleware.Stack) error {
return stack.Deserialize.Insert(&errorCloseResponseBodyMiddleware{}, "OperationDeserializer", middleware.Before)
}
type errorCloseResponseBodyMiddleware struct{}
func (*errorCloseResponseBodyMiddleware) ID() string {
return "ErrorCloseResponseBody"
}
func (m *errorCloseResponseBodyMiddleware) HandleDeserialize(
ctx context.Context, input middleware.DeserializeInput, next middleware.DeserializeHandler,
) (
output middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
out, metadata, err := next.HandleDeserialize(ctx, input)
if err != nil {
if resp, ok := out.RawResponse.(*Response); ok && resp != nil && resp.Body != nil {
// Consume the full body to prevent TCP connection resets on some platforms
_, _ = io.Copy(ioutil.Discard, resp.Body)
// Do not validate that the response closes successfully.
resp.Body.Close()
}
}
return out, metadata, err
}
// AddCloseResponseBodyMiddleware adds the middleware to automatically close
// the response body of an operation request, after the response had been
// deserialized.
func AddCloseResponseBodyMiddleware(stack *middleware.Stack) error {
return stack.Deserialize.Insert(&closeResponseBody{}, "OperationDeserializer", middleware.Before)
}
type closeResponseBody struct{}
func (*closeResponseBody) ID() string {
return "CloseResponseBody"
}
func (m *closeResponseBody) HandleDeserialize(
ctx context.Context, input middleware.DeserializeInput, next middleware.DeserializeHandler,
) (
output middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
out, metadata, err := next.HandleDeserialize(ctx, input)
if err != nil {
return out, metadata, err
}
if resp, ok := out.RawResponse.(*Response); ok {
// Consume the full body to prevent TCP connection resets on some platforms
_, copyErr := io.Copy(ioutil.Discard, resp.Body)
if copyErr != nil {
middleware.GetLogger(ctx).Logf(logging.Warn, "failed to discard remaining HTTP response body, this may affect connection reuse")
}
closeErr := resp.Body.Close()
if closeErr != nil {
middleware.GetLogger(ctx).Logf(logging.Warn, "failed to close HTTP response body, this may affect connection reuse")
}
}
return out, metadata, err
}