Skip to content

Commit

Permalink
Merge pull request #805 from msaf1980/fix/evaluator_limiter
Browse files Browse the repository at this point in the history
fix(expr): deadlock on uninitialized (nil) limiter
  • Loading branch information
Civil authored Nov 1, 2023
2 parents e8a5fe4 + 6f47b58 commit 39b476a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
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)
Expand Down

0 comments on commit 39b476a

Please sign in to comment.