Skip to content
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

fix(expr): deadlock on uninitialized (nil) limiter #805

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion expr/expr.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,9 @@ import (
type evaluator struct{}

func (eval evaluator) Fetch(ctx context.Context, exprs []parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (map[parser.MetricRequest][]*types.MetricData, error) {
config.Config.Limiter.Enter()
if err := config.Config.Limiter.Enter(ctx); err != nil {
return nil, err
}
defer config.Config.Limiter.Leave()

multiFetchRequest := pb.MultiFetchRequest{}
22 changes: 20 additions & 2 deletions limiter/simple.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package limiter

import "context"

type SimpleLimiter chan struct{}

func (l SimpleLimiter) Enter() { l <- struct{}{} }
func (l SimpleLimiter) Leave() { <-l }
func (l SimpleLimiter) Enter(ctx context.Context) error {
if l == nil {
return nil
}

select {
case l <- struct{}{}:
return nil
case <-ctx.Done():
return ErrTimeout
}
}

func (l SimpleLimiter) Leave() {
if l != nil {
<-l
}
}

func NewSimpleLimiter(l int) SimpleLimiter {
return make(chan struct{}, l)