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

feat(erigon): add api ratelimit and apikey #42

Merged
merged 6 commits into from
Jul 22, 2024
Merged

feat(erigon): add api ratelimit and apikey #42

merged 6 commits into from
Jul 22, 2024

Conversation

zibuyu28
Copy link

No description provided.

@KamiD KamiD merged commit 2e6020c into dev Jul 22, 2024
3 checks passed
sync.RWMutex
}

var rateLimit = &RateLimit{}
Copy link

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.

Copy link

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.

Copy link
Author

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()
Copy link

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.

Copy link
Author

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
Copy link

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()
Copy link

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)
Copy link

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)
Copy link

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)
Copy link

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)
Copy link

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)
Copy link

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)
Copy link

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{}
Copy link

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 {
Copy link

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

Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants