Skip to content

Commit b363d29

Browse files
committed
use the computed value instead of doing nil check everywhere
1 parent 5d0feb8 commit b363d29

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

instrumentation/appsec/proxy/message_processor.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"io"
1313
"sync"
14-
"sync/atomic"
1514

1615
"github.com/DataDog/dd-trace-go/v2/appsec"
1716
"github.com/DataDog/dd-trace-go/v2/instrumentation"
@@ -28,17 +27,21 @@ type Processor struct {
2827
ProcessorConfig
2928
instr *instrumentation.Instrumentation
3029

31-
metrics *metrics
32-
done context.CancelFunc
33-
firstRequest sync.Once
34-
bodyParsingConfigured atomic.Bool
30+
metrics *metrics
31+
done context.CancelFunc
32+
firstRequest sync.Once
33+
computedBodyParsingSizeLimit int
3534
}
3635

3736
// NewProcessor creates a new [Processor] instance with the given configuration and instrumentation
3837
// It also initializes the metrics reporter and a context cancellation function
3938
func NewProcessor(config ProcessorConfig, instr *instrumentation.Instrumentation) Processor {
40-
if config.BodyParsingSizeLimit != nil && *config.BodyParsingSizeLimit <= 0 {
41-
instr.Logger().Info("external_processing: body parsing size limit set to 0 or negative. The request and response bodies will NOT be analyzed.")
39+
computedBodyParsingSizeLimit := 0
40+
if config.BodyParsingSizeLimit != nil {
41+
computedBodyParsingSizeLimit = *config.BodyParsingSizeLimit
42+
if computedBodyParsingSizeLimit <= 0 {
43+
instr.Logger().Info("external_processing: body parsing size limit set to 0 or negative. The request and response bodies will NOT be analyzed.")
44+
}
4245
}
4346

4447
if config.Context == nil {
@@ -47,10 +50,11 @@ func NewProcessor(config ProcessorConfig, instr *instrumentation.Instrumentation
4750
var done context.CancelFunc
4851
config.Context, done = context.WithCancel(config.Context)
4952
return Processor{
50-
ProcessorConfig: config,
51-
instr: instr,
52-
metrics: newMetricsReporter(config.Context, instr.Logger()),
53-
done: done,
53+
ProcessorConfig: config,
54+
instr: instr,
55+
metrics: newMetricsReporter(config.Context, instr.Logger()),
56+
done: done,
57+
computedBodyParsingSizeLimit: computedBodyParsingSizeLimit,
5458
}
5559
}
5660

@@ -72,20 +76,17 @@ func (mp *Processor) OnRequestHeaders(ctx context.Context, req RequestHeaders) (
7276

7377
mp.firstRequest.Do(func() {
7478
if mp.BodyParsingSizeLimit == nil {
75-
bodySizeLimit := req.BodyParsingSizeLimit(ctx)
76-
mp.BodyParsingSizeLimit = &bodySizeLimit
79+
mp.computedBodyParsingSizeLimit = req.BodyParsingSizeLimit(ctx)
80+
if mp.computedBodyParsingSizeLimit <= 0 {
81+
mp.instr.Logger().Info("external_processing: body parsing size limit set to 0 or negative. The request and response bodies will NOT be analyzed.")
82+
}
7783
}
78-
mp.instr.Logger().Info("external_processing: first request received. Configuration: BlockingUnavailable=%v, BodyParsingSizeLimit=%dB, Framework=%s", mp.BlockingUnavailable, *mp.BodyParsingSizeLimit, mp.Framework)
84+
mp.instr.Logger().Info("external_processing: first request received. Configuration: BlockingUnavailable=%v, BodyParsingSizeLimit=%dB, Framework=%s", mp.BlockingUnavailable, mp.computedBodyParsingSizeLimit, mp.Framework)
7985
})
8086

81-
var bodyParsingSizeLimit int
82-
if mp.BodyParsingSizeLimit != nil {
83-
bodyParsingSizeLimit = *mp.BodyParsingSizeLimit
84-
}
85-
8687
reqState, blocked := newRequestState(
8788
httpRequest,
88-
bodyParsingSizeLimit,
89+
mp.computedBodyParsingSizeLimit,
8990
mp.Framework,
9091
req.SpanOptions(ctx)...,
9192
)
@@ -138,7 +139,7 @@ func (mp *Processor) OnRequestBody(req HTTPBody, reqState *RequestState) error {
138139

139140
mp.instr.Logger().Debug("message_processor: received request body: %v - EOS: %v\n", len(req.GetBody()), req.GetEndOfStream())
140141

141-
if mp.BodyParsingSizeLimit == nil || *mp.BodyParsingSizeLimit <= 0 || reqState.State != MessageTypeRequestBody {
142+
if mp.computedBodyParsingSizeLimit <= 0 || reqState.State != MessageTypeRequestBody {
142143
mp.instr.Logger().Error("message_processor: the body parsing has been wrongly configured. " +
143144
"Please refer to the official documentation for guidance on the proper settings or contact support.")
144145

@@ -222,7 +223,7 @@ func (mp *Processor) OnResponseBody(resp HTTPBody, reqState *RequestState) error
222223

223224
mp.instr.Logger().Debug("message_processor: received response body: %v - EOS: %v\n", len(resp.GetBody()), resp.GetEndOfStream())
224225

225-
if mp.BodyParsingSizeLimit == nil || *mp.BodyParsingSizeLimit <= 0 || reqState.State != MessageTypeResponseBody {
226+
if mp.computedBodyParsingSizeLimit <= 0 || reqState.State != MessageTypeResponseBody {
226227
mp.instr.Logger().Error("message_processor: the body parsing has been wrongly configured. " +
227228
"Please refer to the official documentation for guidance on the proper settings or contact support.")
228229
return io.EOF
@@ -279,7 +280,7 @@ func processBody(ctx context.Context, bodyBuffer *bodyBuffer, body []byte, eos b
279280

280281
// isBodySupported checks if the body should be analyzed based on content type
281282
func (mp *Processor) isBodySupported(contentType string) bool {
282-
if mp.BodyParsingSizeLimit == nil || *mp.BodyParsingSizeLimit <= 0 {
283+
if mp.computedBodyParsingSizeLimit <= 0 {
283284
return false
284285
}
285286

0 commit comments

Comments
 (0)