-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat(erigon): add api ratelimit and apikey #42
Conversation
sync.RWMutex | ||
} | ||
|
||
var rateLimit = &RateLimit{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rename this better to ensure that the global rate limiter config is better managed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also initialize the map here instead of checking everywhere in the code whether the pointer is nil or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, greate! This is a very good suggestion for me
func methodRateLimitAllow(method string) bool { | ||
rateLimit.RLock() | ||
rlm := rateLimit.rlm | ||
rateLimit.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing defer here as read operation is not finish. We get the pointer to the map, and then unlock the mutex, which can cause issues downstream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we just need to get this pointer of this rlm map, becasue we use rateLimit.rlm = new(map)
to update this rlm(we update thie pointer, but not the value in this map), so if I got this map pointer, I will read the old value this time, but the next time i will get a new one.
} | ||
return rlm | ||
} | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is problematic and unnecessary here to set the map to nil if config is uninitialized.
func apikeyMethodRateLimitAllow(api, method string) bool { | ||
apiKeyRateLimit.RLock() | ||
rlm := apiKeyRateLimit.rlm | ||
apiKeyRateLimit.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to above, missing defer
k.Key = strings.ToLower(k.Key) | ||
parse, err := time.Parse("2006-01-02", k.Timeout) | ||
if err != nil { | ||
log.Warn("parse key [%+v], error parsing timeout: %v", k, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
rlc := RateLimitConfig{} | ||
err := json.Unmarshal([]byte(cfg), &rlc) | ||
if err != nil { | ||
log.Warn("invalid rate limit config: %s", cfg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
|
||
// updateRateLimit updates the rate limit config | ||
func updateRateLimit(rateLimit RateLimitConfig) map[string]*rate.Limiter { | ||
log.Info("rate limit config updated", "config", rateLimit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
func updateRateLimit(rateLimit RateLimitConfig) map[string]*rate.Limiter { | ||
log.Info("rate limit config updated", "config", rateLimit) | ||
if len(rateLimit.RateLimitApis) > 0 { | ||
log.Info("rate limit enabled", "api", rateLimit.RateLimitApis, "count", rateLimit.RateLimitCount, "bucket", rateLimit.RateLimitBucket) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
log.Info("apikey rate limit config updated", "config", rateLimit) | ||
for apikey, config := range rateLimit { | ||
if len(config.RateLimitApis) > 0 { | ||
log.Info("rate limit enabled", "api", config.RateLimitApis, "count", config.RateLimitCount, "bucket", config.RateLimitBucket) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
// updateApikeyRateLimit updates the rate limit config | ||
func updateApikeyRateLimit(rateLimit map[string]*RateLimitConfig) map[string]map[string]*rate.Limiter { | ||
akrlm := make(map[string]map[string]*rate.Limiter) | ||
log.Info("apikey rate limit config updated", "config", rateLimit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect use of the logger
sync.RWMutex | ||
} | ||
|
||
var rateLimit = &RateLimit{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also initialize the map here instead of checking everywhere in the code whether the pointer is nil or not.
} | ||
|
||
// updateRateLimit updates the rate limit config | ||
func updateRateLimit(rateLimit RateLimitConfig) map[string]*rate.Limiter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rateLimit shadowing variable here is confusing and unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function is prepar to be used by apollo dynamic config
No description provided.