Skip to content

Commit 12d1fa1

Browse files
committed
Spend tokens if unlimited.
1 parent 04ea6a5 commit 12d1fa1

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/main/java/org/barcodeapi/server/cache/CachedLimiter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public String getTokenCountStr() {
132132
* @return true if limiter is unlimited
133133
*/
134134
public boolean isUnlimited() {
135-
return ((tokenCount == -1) || !isEnforced());
135+
return (tokenCount == -1);
136136
}
137137

138138
/**
@@ -144,10 +144,16 @@ public double getTokenSpend() {
144144
return tokenSpend;
145145
}
146146

147+
/**
148+
* Returns true if the token balance allows the cost spend.
149+
*
150+
* @param cost the cost of the request
151+
* @return true if balance allows spend
152+
*/
147153
public boolean allowSpend(double cost) {
148154

149-
// Allow if unlimited, or balance exceeds cost
150-
return (isUnlimited() || this.tokenCount > cost);
155+
// Allow if balance exceeds cost, unlimited, or not enforced
156+
return ((this.tokenCount > cost) || isUnlimited() || !isEnforced());
151157
}
152158

153159
/**

0 commit comments

Comments
 (0)