Skip to content

Commit f7888f5

Browse files
committed
make memory limiter extension a middleware
1 parent cb421ec commit f7888f5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

extension/memorylimiterextension/memorylimiter.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package memorylimiterextension // import "go.opentelemetry.io/collector/extensio
55

66
import (
77
"context"
8+
"net/http"
89

910
"go.uber.org/zap"
1011

@@ -38,3 +39,13 @@ func (ml *memoryLimiterExtension) Shutdown(ctx context.Context) error {
3839
func (ml *memoryLimiterExtension) MustRefuse() bool {
3940
return ml.memLimiter.MustRefuse()
4041
}
42+
43+
func (ml *memoryLimiterExtension) GetHTTPHandler(base http.Handler) (http.Handler, error) {
44+
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
45+
if ml.MustRefuse() {
46+
http.Error(resp, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
47+
return
48+
}
49+
base.ServeHTTP(resp, req)
50+
}), nil
51+
}

0 commit comments

Comments
 (0)