File: allow_limiter.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (40 lines) | stat: -rw-r--r-- 925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package metric

import (
	"context"

	otelmetric "go.opentelemetry.io/otel/metric"
	"go.opentelemetry.io/otel/trace"
)

var (
	_ AllowLimiter = (*AllowLimiterInstrumentation)(nil)
)

type AllowLimiter interface {
	Allow(context.Context) bool
}

type AllowLimiterInstrumentation struct {
	wrapper  *LimiterWrapper
	delegate AllowLimiter
}

func NewAllowLimiterInstrumentation(limiterName string, limit float64, limitUnit string, tr trace.Tracer, m otelmetric.Meter, delegate AllowLimiter) (*AllowLimiterInstrumentation, error) {
	w, err := NewLimiterWrapper(limiterName, limit, limitUnit, m, tr)
	if err != nil {
		return nil, err
	}
	return &AllowLimiterInstrumentation{
		wrapper:  w,
		delegate: delegate,
	}, nil
}

func (i *AllowLimiterInstrumentation) Allow(ctx context.Context) (allowed bool) {
	ctx, done := i.wrapper.Start(ctx)
	defer func() { // to handle panics
		done(allowed)
	}()
	return i.delegate.Allow(ctx)
}