-
Notifications
You must be signed in to change notification settings - Fork 14
/
unclosedresponsebodymonitor.go
112 lines (95 loc) · 2.61 KB
/
unclosedresponsebodymonitor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package internal
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"sync"
"github.com/anz-bank/sysl-go/log"
)
type unclosedResponseBodyMonitorContextKey struct{}
type unclosedResponseBodyMonitor struct {
mtx *sync.Mutex
openResponses []*readCloserClosedWrapper
}
func AddResponseBodyMonitorToContext(ctx context.Context) context.Context {
return context.WithValue(ctx, unclosedResponseBodyMonitorContextKey{}, newUnclosedResponseBodyMonitor())
}
func AddResponseToMonitor(ctx context.Context, resp *http.Response) {
if val, ok := ctx.Value(unclosedResponseBodyMonitorContextKey{}).(*unclosedResponseBodyMonitor); ok {
val.addResponse(resp)
}
}
func CheckForUnclosedResponses(ctx context.Context) {
if val, ok := ctx.Value(unclosedResponseBodyMonitorContextKey{}).(*unclosedResponseBodyMonitor); ok {
openBodyErrors := OpenResponseBodyErrors{}
for _, body := range val.getResponsesWithOpenBodies() {
err := openBodyError{
cause: body.parentReq.URL.String(),
err: "response body not closed",
}
openBodyErrors.errors = append(openBodyErrors.errors, err)
}
openBodyCount := len(openBodyErrors.errors)
if openBodyCount > 0 {
err := errors.New(openBodyErrors.Error())
log.Error(ctx, err, "unclosed response")
panic(err)
}
}
}
func newUnclosedResponseBodyMonitor() *unclosedResponseBodyMonitor {
return &unclosedResponseBodyMonitor{
mtx: &sync.Mutex{},
openResponses: []*readCloserClosedWrapper{},
}
}
func (r *unclosedResponseBodyMonitor) addResponse(rsp *http.Response) {
wrapper := &readCloserClosedWrapper{
rsp.Request,
rsp.Body,
false,
}
rsp.Body = wrapper
r.mtx.Lock()
defer r.mtx.Unlock()
r.openResponses = append(r.openResponses, wrapper)
}
func (r *unclosedResponseBodyMonitor) getResponsesWithOpenBodies() []*readCloserClosedWrapper {
r.mtx.Lock()
defer r.mtx.Unlock()
openResponses := make([]*readCloserClosedWrapper, 0)
for _, val := range r.openResponses {
if !val.closed {
openResponses = append(openResponses, val)
}
}
return openResponses
}
type readCloserClosedWrapper struct {
parentReq *http.Request
parent io.ReadCloser
closed bool
}
type openBodyError struct {
cause string
err string
}
type OpenResponseBodyErrors struct {
errors []openBodyError
}
func (e *OpenResponseBodyErrors) Error() string {
var errors string
for _, err := range e.errors {
errors += fmt.Sprintf("%#v \n", err)
}
return fmt.Sprintf("%#v", errors)
}
func (r *readCloserClosedWrapper) Read(p []byte) (n int, err error) {
return r.parent.Read(p)
}
func (r *readCloserClosedWrapper) Close() error {
r.closed = true
return r.parent.Close()
}