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

API limit remaining quota fixed #2048

Merged
merged 1 commit into from
Jan 28, 2019
Merged

API limit remaining quota fixed #2048

merged 1 commit into from
Jan 28, 2019

Conversation

dencoded
Copy link
Contributor

@dencoded dencoded commented Jan 7, 2019

added changes for https://github.com/TykTechnologies/tyk-analytics/issues/987

The problem with quota-remaining is that it never makes to storage, we schedule session update in storage only when new renewal period starts, so we have to populate "remaining quota" counters directly from counters in redis when key gets requested via API.

Also, added some tests for quota on API limit, something we should have done initially.

@dencoded dencoded requested a review from buger January 7, 2019 19:55
@evityuk evityuk self-requested a review January 25, 2019 14:57
@buger buger merged commit dc5124d into master Jan 28, 2019
@buger buger deleted the api-limit-fix branch January 28, 2019 15:07
if byHash {
limQuotaKey = QuotaKeyPrefix + id + "-" + sessionKey
}
if usedQuota, err := sessionManager.Store().GetRawKey(limQuotaKey); err == nil {
Copy link
Contributor

@evityuk evityuk Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to optimize this part by:

  1. moving storage.HashKey(sessionKey) out of loop(see line#366)

  2. [optional]moving apply quota logic to separate helper func in order to unify handling of sessionKey-quota & access-rights-quota with next pseudo-go-code

func applyQuota(quotaHandle interface{ ApplyQuota(int64) (int64, ?error) { 
    this.QuotaRemaining = ... // lines[371;377]
    return this.QuotaRemaining, errorIfAny
  } }, quotaStorageKey) error {
var usedQuota = ... //step1: get quotaFromStorage
quotaHandle.AppyQuota(usedQuota) //step2: apply quota and return error
}

Copy link
Contributor Author

@dencoded dencoded Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we are both replying to already merged PR :)

eventually there is lots of room to refactor here, the biggest piece I see is eliminating somehow repeated logic to populate usedQuota starting at
line https://github.com/TykTechnologies/tyk/pull/2048/files#diff-651a84f8ad6a38c1f7ccdf63f2692410R342
and
line https://github.com/TykTechnologies/tyk/pull/2048/files#diff-651a84f8ad6a38c1f7ccdf63f2692410R366

continue
}

limQuotaKey := QuotaKeyPrefix + id + "-" + storage.HashKey(sessionKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you re-use already initialized session-key hash from line #337 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quotaKey (to store quota on session level) and var limQuotaKey here (to store quota on session ACL level) - those keys have different format

Copy link
Contributor

@evityuk evityuk Jan 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They share the same suffix part - storage.HashKey(sessionKey), which can be initialized at the beginning once.

limitQuotaRemaining = currAccessRight.Limit.QuotaRemaining
limitQuotaRenews = currAccessRight.Limit.QuotaRenews
}
accessRights.Limit.QuotaRemaining = limitQuotaRemaining
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Isn't the changing of global policy from global policiesByID map will mutate policy default value for the next request, is it?
  2. Shouldn't there be default values for limitQuotaRemaining & limitQuotaRenews? It seems it can override current(which is default if I'm correct) values for policy. Or is it for synchronization with values fetched from redis before inside this block?

@buger FYI
Feel free to confirm or disprove

},
},
}...)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

buger pushed a commit that referenced this pull request Feb 11, 2019
added changes for TykTechnologies/tyk-analytics#987

The problem with quota-remaining is that it never makes to storage, we schedule session update in storage only when new renewal period starts, so we have to populate "remaining quota" counters directly from counters in redis when key gets requested via API.

Also, added some tests for quota on API limit, something we should have done initially.
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.

None yet

3 participants