@@ -6,13 +6,16 @@ import (
6
6
"net/http"
7
7
"sync"
8
8
9
+ "github.com/kava-labs/kava-proxy-service/logging"
9
10
"github.com/kava-labs/kava-proxy-service/service/cachemdw"
10
11
)
11
12
12
13
// BatchProcessor makes multiple requests to the underlying handler and then combines all the
13
14
// responses into a single response.
14
15
// It assumes all individual responses are valid json. Each response is then marshaled into an array.
15
16
type BatchProcessor struct {
17
+ * logging.ServiceLogger
18
+
16
19
handler http.HandlerFunc
17
20
requests []* http.Request
18
21
responses []* bytes.Buffer
@@ -23,14 +26,15 @@ type BatchProcessor struct {
23
26
}
24
27
25
28
// NewBatchProcessor creates a BatchProcessor for combining the responses of reqs to the handler
26
- func NewBatchProcessor (handler http.HandlerFunc , reqs []* http.Request ) * BatchProcessor {
29
+ func NewBatchProcessor (serviceLogger * logging. ServiceLogger , handler http.HandlerFunc , reqs []* http.Request ) * BatchProcessor {
27
30
return & BatchProcessor {
28
- handler : handler ,
29
- requests : reqs ,
30
- responses : make ([]* bytes.Buffer , len (reqs )),
31
- header : nil ,
32
- status : http .StatusOK ,
33
- mu : sync.Mutex {},
31
+ ServiceLogger : serviceLogger ,
32
+ handler : handler ,
33
+ requests : reqs ,
34
+ responses : make ([]* bytes.Buffer , len (reqs )),
35
+ header : nil ,
36
+ status : http .StatusOK ,
37
+ mu : sync.Mutex {},
34
38
}
35
39
}
36
40
@@ -42,6 +46,15 @@ func (bp *BatchProcessor) RequestAndServe(w http.ResponseWriter) error {
42
46
wg .Add (1 )
43
47
44
48
go func (idx int , req * http.Request ) {
49
+ // if a client closes the connection prematurely, it can cause panics from batch sub-requests.
50
+ // when that happens, log & discard the panic.
51
+ // https://github.com/golang/go/issues/28239
52
+ defer func () {
53
+ if recover () != nil {
54
+ wg .Done ()
55
+ bp .Error ().Int ("sub-request index" , idx ).Msg ("sub-request for batch panicked" )
56
+ }
57
+ }()
45
58
46
59
buf := new (bytes.Buffer )
47
60
frw := newFakeResponseWriter (buf , bp .setErrStatus )
0 commit comments