Skip to content

Commit

Permalink
Fixed quota bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Jan 11, 2016
1 parent 467ee8f commit 7b7f6af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
- Added new feature: Detailed logging, enable by setting `analytics_config.enable_detailed_recording` to true, two new fields will be added to analytics data: rawRequest and rawResponse, these will be in wire format and are *NOT* anonymised. This adds additional processing complexity to request throughput so could degrade performance.
- Added a check for connection failures
- Updating a key with a quota reset set to true will also remove any rate limit sentinels
- CURL Rewrites and cache interactions now work properly, although you need to define the cached entry as the rewritten pattern in a seperate entry.
- URL Rewrites and cache interactions now work properly, although you need to define the cached entry as the rewritten pattern in a seperate entry.
- Org quotas monitors now only fire when the renewal is in the future, not the past.

- Fixed bug where quotas would not reset (regression introduced by switch to Redis Cluster), Tyk will automaticall correct quota entries taht are incorrect.
- Using golang builtins for time checking
# 1.9

- Gateway Mongo Driver updated to be compatible with MongoDB v3.0
Expand Down
17 changes: 10 additions & 7 deletions auth_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func (b DefaultAuthorisationManager) IsKeyAuthorised(keyName string) (SessionSta
// IsKeyExpired checks if a key has expired, if the value of SessionState.Expires is 0, it will be ignored
func (b DefaultAuthorisationManager) IsKeyExpired(newSession *SessionState) bool {
if newSession.Expires >= 1 {
diff := newSession.Expires - time.Now().Unix()
if diff > 0 {
return false
//diff := newSession.Expires - time.Now().Unix()
if time.Now().After(time.Unix(newSession.Expires, 0)) {
return true
}
return true
return false
}
return false
}
Expand All @@ -91,12 +91,15 @@ func (b *DefaultSessionManager) GetStore() StorageHandler {

func (b *DefaultSessionManager) ResetQuota(keyName string, session SessionState) {
log.Warning("Tracked quota reset for key: ", keyName)
rawKey := QuotaKeyPrefix + keyName
log.Debug("Setting: ", rawKey)
rawKey := QuotaKeyPrefix + publicHash(keyName)
log.Info("Setting key quota: ", rawKey)

rateLimiterSentinelKey := RateLimitKeyPrefix + publicHash(keyName) + ".BLOCKED"
// Clear the rate limiter
go b.Store.DeleteRawKey(rateLimiterSentinelKey)
go b.Store.SetKey(rawKey, "0", session.QuotaRenewalRate)
// Fix the raw key
go b.Store.DeleteRawKey(rawKey)
//go b.Store.SetKey(rawKey, "0", session.QuotaRenewalRate)
}

// UpdateSession updates the session state in the storage engine
Expand Down
15 changes: 14 additions & 1 deletion session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,20 @@ func (l SessionLimiter) IsRedisQuotaExceeded(currentSession *SessionState, key s

// if the returned val is >= quota: block
if (int64(qInt) - 1) >= currentSession.QuotaMax {
return true
RenewalDate := time.Unix(currentSession.QuotaRenews, 0)
log.Debug("Renewal Date is: ", RenewalDate)
log.Debug("Now:", time.Now())
if time.Now().After(RenewalDate) {
// The renewal date is in the past, we should update the quota!
// Also, this fixes legacy issues where there is no TTL on quota buckets
log.Warning("Incorrect key expiry setting detected, correcting.")
go store.DeleteRawKey(rawKey)
qInt = 1
} else {
// Renewal date is in the future and the quota is exceeded
return true
}

}

// If this is a new Quota period, ensure we let the end user know
Expand Down

0 comments on commit 7b7f6af

Please sign in to comment.