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

limiter: fix get username #226

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
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: 2 additions & 2 deletions autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
findCache bool
)

username := w.Header().Get("X-Forwarded-User")
username := r.Header.Get("X-Forwarded-User")
limiter := h.config.GetUserTagsLimiter(username)

defer func() {
Expand Down Expand Up @@ -441,7 +441,7 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
findCache bool
)

username := w.Header().Get("X-Forwarded-User")
username := r.Header.Get("X-Forwarded-User")
limiter := h.config.GetUserTagsLimiter(username)

defer func() {
Expand Down
2 changes: 1 addition & 1 deletion find/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
query string
)

username := w.Header().Get("X-Forwarded-User")
username := r.Header.Get("X-Forwarded-User")
limiter := h.config.GetUserTagsLimiter(username)

defer func() {
Expand Down
2 changes: 2 additions & 0 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (sl *Limiter) Enter(ctx context.Context, s string) (err error) {
if err = sl.l.enter(ctx, s); err != nil {
sl.m.WaitErrors.Add(1)
}
sl.m.Requests.Add(1)
return
}

Expand All @@ -49,6 +50,7 @@ func (sl *Limiter) TryEnter(ctx context.Context, s string) (err error) {
if err = sl.l.tryEnter(ctx, s); err != nil {
sl.m.WaitErrors.Add(1)
}
sl.m.Requests.Add(1)
return
}

Expand Down
2 changes: 2 additions & 0 deletions limiter/wlimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (sl *WLimiter) Enter(ctx context.Context, s string) (err error) {
err = ErrTimeout
}
}
sl.m.Requests.Add(1)
return
}

Expand All @@ -69,6 +70,7 @@ func (sl *WLimiter) TryEnter(ctx context.Context, s string) (err error) {
err = ErrTimeout
}
}
sl.m.Requests.Add(1)
return
}

Expand Down
5 changes: 5 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,29 @@ type ReqMetric struct {
type WaitMetric struct {
nameErrors string
// wait slot
Requests metrics.Counter
WaitErrors metrics.Counter
WaitTimeName string
}

func NewWaitMetric(enable bool, scope, sub string) WaitMetric {
if enable {
nameRequests := scope + "_wait." + sub + ".requests"
nameErrors := scope + "_wait." + sub + ".errors"
w := WaitMetric{
nameErrors: nameErrors,
Requests: metrics.NewCounter(),
WaitErrors: metrics.NewCounter(),
WaitTimeName: scope + "_wait." + sub + ".requests",
}
metrics.Register(nameRequests, w.Requests)
metrics.Register(nameErrors, w.WaitErrors)

return w
}
return WaitMetric{
WaitErrors: metrics.NilCounter{},
Requests: metrics.NilCounter{},
WaitTimeName: "",
}
}
Expand Down
6 changes: 3 additions & 3 deletions render/data/multi_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func getDataTimeout(cfg *config.Config, m *MultiTarget) time.Duration {
return dataTimeout
}

func GetQueryLimiter(username string, cfg *config.Config, m *MultiTarget) limiter.ServerLimiter {
func GetQueryLimiter(username string, cfg *config.Config, m *MultiTarget) (string, limiter.ServerLimiter) {
n := 0
if username != "" && len(cfg.ClickHouse.UserLimits) > 0 {
if u, ok := cfg.ClickHouse.UserLimits[username]; ok {
return u.Limiter
return username, u.Limiter
}
}

Expand All @@ -98,7 +98,7 @@ func GetQueryLimiter(username string, cfg *config.Config, m *MultiTarget) limite
n = config.GetQueryParam(cfg.ClickHouse.QueryParams, maxDuration)
}

return cfg.ClickHouse.QueryParams[n].Limiter
return "", cfg.ClickHouse.QueryParams[n].Limiter
}

func GetQueryLimiterFrom(username string, cfg *config.Config, from, until int64) limiter.ServerLimiter {
Expand Down
6 changes: 4 additions & 2 deletions render/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
queueDuration time.Duration
err error
fetchRequests data.MultiTarget
luser string
)
start := time.Now()
status := http.StatusOK
Expand All @@ -232,7 +233,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

r = r.WithContext(scope.WithLogger(r.Context(), logger))

username := w.Header().Get("X-Forwarded-User")
username := r.Header.Get("X-Forwarded-User")
var qlimiter limiter.ServerLimiter = limiter.NoopLimiter{}

defer func() {
Expand Down Expand Up @@ -278,7 +279,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
targetsLen += len(targets.List)
}

qlimiter = data.GetQueryLimiter(username, h.config, &fetchRequests)
luser, qlimiter = data.GetQueryLimiter(username, h.config, &fetchRequests)
logger.Debug("use user limiter", zap.String("username", username), zap.String("luser", luser))

Check failure

Code scanning / CodeQL

Log entries created from user input

This log entry depends on a [user-provided value](1).

Check failure

Code scanning / CodeQL

Log entries created from user input

This log entry depends on a [user-provided value](1).

var maxCacheTimeoutStr string
useCache := h.config.Common.FindCache != nil && !parser.TruthyBool(r.FormValue("noCache"))
Expand Down