@@ -12,6 +12,7 @@ import (
12
12
"time"
13
13
14
14
"github.com/rs/cors"
15
+ "go.opentelemetry.io/collector/extension/memorylimiter"
15
16
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
16
17
"go.opentelemetry.io/otel"
17
18
"golang.org/x/net/http2"
@@ -234,6 +235,10 @@ type HTTPServerSettings struct {
234
235
// Additional headers attached to each HTTP response sent to the client.
235
236
// Header values are opaque since they may be sensitive.
236
237
ResponseHeaders map [string ]configopaque.String `mapstructure:"response_headers"`
238
+
239
+ // IsLimitMemory is use to enable memory limiter extension if the extension exists. Memory limiter checks
240
+ // total memory usage restricted by the extension and return an error if memory exceeds.
241
+ EnableMemoryLimiter bool `mapstructure:"limit_memory"`
237
242
}
238
243
239
244
// ToListener creates a net.Listener.
@@ -294,6 +299,14 @@ func (hss *HTTPServerSettings) ToServer(host component.Host, settings component.
294
299
o (serverOpts )
295
300
}
296
301
302
+ if hss .EnableMemoryLimiter {
303
+ if ext , err := memorylimiter .GetMemoryLimiterExtension (host .GetExtensions ()); err != nil {
304
+ handler = memoryLimiterInterceptor (handler , ext )
305
+ } else {
306
+ return nil , err
307
+ }
308
+ }
309
+
297
310
handler = httpContentDecompressor (handler , serverOpts .errHandler , serverOpts .decoders )
298
311
299
312
if hss .MaxRequestBodySize > 0 {
@@ -400,3 +413,13 @@ func maxRequestBodySizeInterceptor(next http.Handler, maxRecvSize int64) http.Ha
400
413
next .ServeHTTP (w , r )
401
414
})
402
415
}
416
+
417
+ func memoryLimiterInterceptor (next http.Handler , ml memorylimiter.MemoryLimiter ) http.Handler {
418
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
419
+ if err := ml .CheckMemory (); err != nil {
420
+ http .Error (w , http .StatusText (http .StatusTooManyRequests ), http .StatusTooManyRequests )
421
+ return
422
+ }
423
+ next .ServeHTTP (w , r )
424
+ })
425
+ }
0 commit comments