-
Notifications
You must be signed in to change notification settings - Fork 358
feat: add Memory Protection Middleware #2646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| internal/mocks/*.go linguist-generated=true | ||
| *.pb.go linguist-generated=true | ||
| *.pb.*.go linguist-generated=true | ||
| proto/internal/buf.lock linguist-generated=true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ global: | |
| scrape_configs: | ||
| - job_name: "spicedb" | ||
| static_configs: | ||
| - targets: ["spicedb:9090"] | ||
| - targets: ["spicedb-1:9090"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI so that we can verify the new metrics in Grafana |
||
| labels: | ||
| service: "spicedb" | ||
| service: "spicedb-1" | ||
| - targets: ["spicedb-2:9090"] | ||
| labels: | ||
| service: "spicedb-2" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
internal/middleware/memoryprotection/memory_protection.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package memoryprotection | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime/debug" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| log "github.com/authzed/spicedb/internal/logging" | ||
| ) | ||
|
|
||
| // RequestsProcessed tracks requests that were processed by this middleware. | ||
| var RequestsProcessed = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "spicedb", | ||
| Subsystem: "memory_middleware", | ||
| Name: "requests_processed_total", | ||
| Help: "Total requests processed by the memory protection middleware (flag --memory-protection-enabled)", | ||
| }, []string{"endpoint", "accepted"}) | ||
|
|
||
| // Config holds configuration for the memory protection middleware | ||
| type Config struct { | ||
| // ThresholdPercent is the memory usage threshold for requests. If zero or negative, this middleware has no effect | ||
| ThresholdPercent float64 | ||
| } | ||
|
|
||
| // DefaultConfig returns reasonable default configuration for API requests | ||
| func DefaultConfig() Config { | ||
| return Config{ | ||
| ThresholdPercent: 0.90, | ||
| } | ||
| } | ||
|
|
||
| // DefaultDispatchConfig returns reasonable default configuration for dispatch requests | ||
| func DefaultDispatchConfig() Config { | ||
| return Config{ | ||
| ThresholdPercent: 0.95, | ||
| } | ||
| } | ||
|
|
||
| // MemoryLimitProvider gets and sets the limit of memory usage. | ||
| // In production, use DefaultMemoryLimitProvider. | ||
| // For testing, use HardCodedMemoryLimitProvider. | ||
| type MemoryLimitProvider interface { | ||
| GetInBytes() int64 | ||
| SetInBytes(int64) | ||
| } | ||
|
|
||
| var ( | ||
| _ MemoryLimitProvider = (*DefaultMemoryLimitProvider)(nil) | ||
| _ MemoryLimitProvider = (*HardCodedMemoryLimitProvider)(nil) | ||
| ) | ||
|
|
||
| type DefaultMemoryLimitProvider struct{} | ||
|
|
||
| func (p *DefaultMemoryLimitProvider) GetInBytes() int64 { | ||
| // SetMemoryLimit returns the previously set memory limit. | ||
| // A negative input does not adjust the limit, and allows for retrieval of the currently set memory limit | ||
| return debug.SetMemoryLimit(-1) | ||
| } | ||
|
|
||
| func (p *DefaultMemoryLimitProvider) SetInBytes(limit int64) { | ||
| debug.SetMemoryLimit(limit) | ||
| } | ||
|
|
||
| type HardCodedMemoryLimitProvider struct { | ||
| Hardcodedlimit int64 | ||
| } | ||
|
|
||
| func (p *HardCodedMemoryLimitProvider) GetInBytes() int64 { | ||
| return p.Hardcodedlimit | ||
| } | ||
|
|
||
| func (p *HardCodedMemoryLimitProvider) SetInBytes(limit int64) { | ||
| p.Hardcodedlimit = limit | ||
| } | ||
|
|
||
| type MemoryProtectionMiddleware struct { | ||
| config Config | ||
| sampler MemorySampler | ||
| } | ||
|
|
||
| // New creates a new memory admission middleware with the given sampler, which is assumed to have been started already. | ||
| func New(config Config, sampler MemorySampler, name string) *MemoryProtectionMiddleware { | ||
| am := MemoryProtectionMiddleware{ | ||
| config: config, | ||
| sampler: sampler, | ||
| } | ||
|
|
||
| if am.disabled() { | ||
| log.Warn().Str("name", name).Msg("memory protection middleware disabled") | ||
| return &am | ||
| } | ||
|
|
||
| log.Info(). | ||
| Str("name", name). | ||
| Float64("threshold_percent", config.ThresholdPercent). | ||
| Msg("memory protection middleware initialized") | ||
|
|
||
| return &am | ||
| } | ||
|
|
||
| // UnaryServerInterceptor returns a unary server interceptor that rejects incoming requests is memory usage is too high | ||
| func (am *MemoryProtectionMiddleware) UnaryServerInterceptor() grpc.UnaryServerInterceptor { | ||
| return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { | ||
| if err := am.checkAdmission(info.FullMethod); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return handler(ctx, req) | ||
| } | ||
| } | ||
|
|
||
| // StreamServerInterceptor returns a stream server interceptor that rejects incoming requests is memory usage is too high | ||
| func (am *MemoryProtectionMiddleware) StreamServerInterceptor() grpc.StreamServerInterceptor { | ||
| return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
| if err := am.checkAdmission(info.FullMethod); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| wrapped := middleware.WrapServerStream(stream) | ||
| return handler(srv, wrapped) | ||
| } | ||
| } | ||
|
|
||
| // checkAdmission returns an error if the request should be denied because memory usage is too high. | ||
| func (am *MemoryProtectionMiddleware) checkAdmission(method string) error { | ||
| if am.disabled() { | ||
| return nil | ||
| } | ||
|
|
||
| accept := true | ||
| defer func() { | ||
| am.recordMetric(method, accept) | ||
| }() | ||
|
|
||
| if am.sampler.GetMemoryUsagePercent() >= am.config.ThresholdPercent { | ||
| accept = false | ||
| return status.Error(codes.ResourceExhausted, "server rejected the request because memory usage is above configured threshold") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (am *MemoryProtectionMiddleware) disabled() bool { | ||
| return am.config.ThresholdPercent <= 0 | ||
| } | ||
|
|
||
| // recordMetric updates the RequestsProcessed metric and returns the endpoint type for the input method. | ||
| func (am *MemoryProtectionMiddleware) recordMetric(fullMethod string, accepted bool) string { | ||
| endpointType := "api" | ||
| if strings.HasPrefix(fullMethod, "/dispatch.v1.DispatchService") { | ||
| endpointType = "dispatch" | ||
| } | ||
|
|
||
| acceptedStr := strconv.FormatBool(accepted) | ||
|
|
||
| RequestsProcessed.WithLabelValues(endpointType, acceptedStr).Inc() | ||
| return endpointType | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI so that when changing these, the "View PR" view on Github says that the files are automatically generated and people can just mark "viewed" on them