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

ratelimit/keeper: refactor with inversions and early returns for more idiomatic Go #4

Open
odeke-em opened this issue Mar 9, 2024 · 0 comments

Comments

@odeke-em
Copy link

odeke-em commented Mar 9, 2024

Just while reading through this code I noticed this

func (k Keeper) BeginBlocker(ctx sdk.Context) {
if epochStarting, epochNumber := k.CheckHourEpochStarting(ctx); epochStarting {
for _, rateLimit := range k.GetAllRateLimits(ctx) {
if rateLimit.Quota.DurationHours != 0 && epochNumber%rateLimit.Quota.DurationHours == 0 {
err := k.ResetRateLimit(ctx, rateLimit.Path.Denom, rateLimit.Path.ChannelId)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Unable to reset quota for Denom: %s, ChannelId: %s", rateLimit.Path.Denom, rateLimit.Path.ChannelId))
which can be refactored for more idiomatic Go, using early returns and inversions to reduce nesting but also make conditions much simpler to read and reason about so

func (k Keeper) BeginBlocker(ctx sdk.Context) {
   // As long as epochStarting is true,
   // epochNumber will always be >0.
	epochStarting, epochNumber := k.CheckHourEpochStarting(ctx)
   if !epochStarting {
      return
   }
	
   for _, rateLimit := range k.GetAllRateLimits(ctx) {
      ok := rateLimit.Quota.DurationHours != 0 && epochNumber%rateLimit.Quota.DurationHours == 0
      if !ok {
         continue
      }
			
      err := k.ResetRateLimit(ctx, rateLimit.Path.Denom, rateLimit.Path.ChannelId)
      if err != nil {
         k.Logger(ctx).Error(fmt.Sprintf("Unable to reset quota for Denom: %s, ChannelId: %s", rateLimit.Path.Denom, rateLimit.Path.ChannelId))
		 }
	}
}
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

No branches or pull requests

1 participant