(ctx context.Context, req *common.NormalizedRequest)
| 268 | } |
| 269 | |
| 270 | func (p *PreparedProject) AcquireRateLimitPermit(ctx context.Context, req *common.NormalizedRequest) error { |
| 271 | if p.Config.RateLimitBudget == "" { |
| 272 | return nil |
| 273 | } |
| 274 | |
| 275 | rlb, errNetLimit := p.rateLimitersRegistry.GetBudget(p.Config.RateLimitBudget) |
| 276 | if errNetLimit != nil { |
| 277 | return errNetLimit |
| 278 | } |
| 279 | if rlb == nil { |
| 280 | return nil |
| 281 | } |
| 282 | |
| 283 | method, errMethod := req.Method() |
| 284 | if errMethod != nil { |
| 285 | return errMethod |
| 286 | } |
| 287 | lg := p.Logger.With().Str("method", method).Logger() |
| 288 | |
| 289 | rules, errRules := rlb.GetRulesByMethod(method) |
| 290 | if errRules != nil { |
| 291 | return errRules |
| 292 | } |
| 293 | lg.Debug().Msgf("found %d network-level rate limiters", len(rules)) |
| 294 | |
| 295 | if len(rules) > 0 { |
| 296 | allowed, err := rlb.TryAcquirePermit(ctx, p.Config.Id, req, method, "", "", "", "project") |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | if !allowed { |
| 301 | return common.NewErrProjectRateLimitRuleExceeded( |
| 302 | p.Config.Id, |
| 303 | p.Config.RateLimitBudget, |
| 304 | fmt.Sprintf("method:%s", method), |
| 305 | ) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return nil |
| 310 | } |
no test coverage detected