NewProfile returns a new ratelimit.Interface from profile configuration.
(ctx context.Context, conf config.RateLimitingProfile, storeConf StoreConfig)
| 102 | |
| 103 | // NewProfile returns a new ratelimit.Interface from profile configuration. |
| 104 | func NewProfile(ctx context.Context, conf config.RateLimitingProfile, storeConf StoreConfig) (Interface, error) { |
| 105 | if s := &storeConf.Memory.MaxSize; *s == 0 { |
| 106 | *s = defaultMaxSize |
| 107 | } |
| 108 | if conf.MaxPerMin == 0 { |
| 109 | return nil, errInvalidRate.WithAttributes("rate", conf.MaxPerMin, "name", conf.Name) |
| 110 | } |
| 111 | store, err := newStore(storeConf) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | if conf.MaxBurst == 0 { |
| 116 | conf.MaxBurst = conf.MaxPerMin |
| 117 | } |
| 118 | quota := throttled.RateQuota{ |
| 119 | MaxRate: throttled.PerMin(int(conf.MaxPerMin)), |
| 120 | MaxBurst: int(conf.MaxBurst - 1), |
| 121 | } |
| 122 | limiter, err := throttled.NewGCRARateLimiterCtx(store, quota) |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | return &rateLimiter{ctx, limiter}, nil |
| 127 | } |
no test coverage detected