Skip to content

Commit 0f055d7

Browse files
committed
Disable reputation enforcement for subscribers.
1 parent 13414de commit 0f055d7

7 files changed

Lines changed: 44 additions & 41 deletions

File tree

src/main/java/org/barcodeapi/server/admin/LimiterListHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ protected void onRequest(RequestContext c, HttpServletResponse r) throws JSONExc
4646

4747
// Add all to map if no filters applied
4848
if (!filtered) {
49-
byKey.put(limiter.getCaller(), limiter.getTokens().getTotalSpend());
49+
byKey.put(limiter.getCallerID(), limiter.getTokens().getTotalSpend());
5050
continue;
5151
}
5252

5353
// Conditionally filter based on low reputation status
5454
if (filterReputation && limiter.getReputation().isAbuser()) {
55-
byKey.put(limiter.getCaller(), limiter.getReputation().value());
55+
byKey.put(limiter.getCallerID(), limiter.getReputation().value());
5656
continue;
5757
}
5858

5959
// Conditionally filter based on low tokens count
6060
if (filterTokenLimit && limiter.getTokens().isLowBalance()) {
61-
byKey.put(limiter.getCaller(), limiter.getTokens().getCount());
61+
byKey.put(limiter.getCallerID(), limiter.getTokens().getCount());
6262
}
6363
}
6464

src/main/java/org/barcodeapi/server/api/BarcodeAPIHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void onRequest(RequestContext c, HttpServletResponse r) throws IOExcep
6161
// Check for abuse
6262
if (limiter.getReputation().isAbuser()) {
6363
throw new GenerationException(ExceptionType.ABUSE, new Throwable(String.format(//
64-
"Bad reputation for IP, try again later. (u:%s)", limiter.getCaller())));
64+
"Bad reputation for IP, try again later. (u:%s)", limiter.getCallerID())));
6565
}
6666

6767
// Parse the request
@@ -78,7 +78,7 @@ protected void onRequest(RequestContext c, HttpServletResponse r) throws IOExcep
7878

7979
// Return rate limited barcode to user
8080
throw new GenerationException(ExceptionType.LIMITED, new Throwable(String.format(//
81-
"Client is rate limited, try again later. (u:%s)", limiter.getCaller())));
81+
"Client is rate limited, try again later. (u:%s)", limiter.getCallerID())));
8282
}
8383

8484
// Generate user requested barcode

src/main/java/org/barcodeapi/server/api/LimiterHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected void doDELETE(RequestContext c, HttpServletResponse r) throws IOExcept
6565
}
6666

6767
// Log the reset request
68-
LibLog._logF("Limiter reset request: %s", c.getLimiter().getCaller());
68+
LibLog._logF("Limiter reset request: %s", c.getLimiter().getCallerID());
6969

7070
// Reset the limiter balance to full
7171
Tokens userTokens = c.getLimiter().getTokens();

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.barcodeapi.server.cache;
22

3+
import org.barcodeapi.core.Config;
4+
import org.barcodeapi.core.Config.Cfg;
35
import org.barcodeapi.server.core.Reputation;
46
import org.barcodeapi.server.core.Tokens;
57
import org.json.JSONObject;
@@ -11,22 +13,43 @@
1113
*/
1214
public class CachedLimiter extends CachedObject {
1315

16+
// Default values for new limiters
17+
private static final int DEFLIMIT_RATE;
18+
private static final boolean DEFLIMIT_ENFORCE;
19+
20+
static {
21+
22+
// Load plan from configuration
23+
JSONObject freePlan = Config//
24+
.get(Cfg.Plans).getJSONObject("free");
25+
26+
// Free plan defaults
27+
DEFLIMIT_RATE = freePlan.getInt("limit");
28+
DEFLIMIT_ENFORCE = freePlan.getBoolean("enforce");
29+
}
30+
1431
private static final long serialVersionUID = 20260503L;
1532

16-
private final String caller;
33+
private final String callerID;
1734

1835
private final Reputation reputation;
1936

2037
private final Tokens tokens;
2138

22-
public CachedLimiter(boolean enforce, String caller, long requests) {
39+
public CachedLimiter(Subscriber sub, String address) {
2340
super("limiter");
2441

25-
this.caller = caller;
42+
// Assign the userID
43+
this.callerID = (sub != null) ? sub.getCustomer() : address;
2644

27-
this.reputation = new Reputation(enforce);
45+
// Determine token limits for the limiter
46+
int limit = (sub != null) ? sub.getLimit() : DEFLIMIT_RATE;
47+
boolean enforce = (sub != null) ? sub.isEnforced() : DEFLIMIT_ENFORCE;
48+
this.tokens = new Tokens(enforce, limit);
2849

29-
this.tokens = new Tokens(enforce, requests);
50+
// Setup user reputation tracker
51+
boolean repBlock = (sub != null) ? false : enforce;
52+
this.reputation = new Reputation(repBlock);
3053
}
3154

3255
public void touch(CachedSession session) {
@@ -54,8 +77,8 @@ public boolean isShortLived() {
5477
*
5578
* @return caller
5679
*/
57-
public String getCaller() {
58-
return caller;
80+
public String getCallerID() {
81+
return callerID;
5982
}
6083

6184
/**
@@ -101,7 +124,7 @@ public boolean onRequest(boolean valid, double cost) {
101124
public JSONObject asJSON() {
102125

103126
return (new JSONObject()//
104-
.put("caller", getCaller())//
127+
.put("callerID", getCallerID())//
105128
.put("requests", getAccessCount())//
106129
.put("reputation", reputation.value())//
107130
.put("time", new JSONObject() //

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package org.barcodeapi.server.cache;
22

3-
import org.barcodeapi.core.Config;
4-
import org.barcodeapi.core.Config.Cfg;
5-
import org.json.JSONObject;
6-
73
import com.mclarkdev.tools.libmetrics.LibMetrics;
84

95
/**
@@ -13,23 +9,11 @@
139
*/
1410
public class LimiterCache {
1511

16-
// Default values for new limiters
17-
private static final int DEFLIMIT_RATE;
18-
private static final boolean DEFLIMIT_ENFORCE;
19-
2012
// Local instance of the limiters cache
2113
private static final ObjectCache _LIMITERS;
2214

2315
static {
2416

25-
// Load plan from configuration
26-
JSONObject freePlan = Config//
27-
.get(Cfg.Plans).getJSONObject("free");
28-
29-
// Free plan defaults
30-
DEFLIMIT_RATE = freePlan.getInt("limit");
31-
DEFLIMIT_ENFORCE = freePlan.getBoolean("enforce");
32-
3317
// Get the limiter cache
3418
_LIMITERS = ObjectCache.getCache(ObjectCache.CACHE_LIMITERS);
3519
}
@@ -44,23 +28,19 @@ public static CachedLimiter getLimiter(Subscriber sub, String address) {
4428
LibMetrics.hitMethodRunCounter();
4529

4630
// User ID is customer or address
47-
String userID = (sub != null) ? sub.getCustomer() : address;
31+
String caller = (sub != null) ? sub.getCustomer() : address;
4832

4933
// Determine if limiter exists
5034
CachedLimiter limiter;
51-
if (_LIMITERS.has(userID)) {
35+
if (_LIMITERS.has(caller)) {
5236

5337
// Get the existing limiter from the cache
54-
limiter = (CachedLimiter) _LIMITERS.get(userID);
38+
limiter = (CachedLimiter) _LIMITERS.get(caller);
5539
} else {
5640

57-
// Determine enforce / limits for the new limiter
58-
int limit = (sub != null) ? sub.getLimit() : DEFLIMIT_RATE;
59-
boolean enforce = (sub != null) ? sub.isEnforced() : DEFLIMIT_ENFORCE;
60-
6141
// Create a new limiter and add it to the cache
62-
limiter = new CachedLimiter(enforce, userID, limit);
63-
_LIMITERS.put(userID, limiter);
42+
limiter = new CachedLimiter(sub, address);
43+
_LIMITERS.put(caller, limiter);
6444
}
6545

6646
// Return the limiter

src/main/java/org/barcodeapi/server/core/RestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
160160
// Send token count to user
161161
response.setHeader("X-RateLimit-Tokens", //
162162
String.format("%.2f", ctx.getLimiter().getTokens().getCount()));
163-
response.setHeader("X-RateLimit-Caller", ctx.getLimiter().getCaller());
163+
response.setHeader("X-RateLimit-Caller", ctx.getLimiter().getCallerID());
164164

165165
// Request complete if only options
166166
if (ctx.getMethod().equals("OPTIONS")) {

src/main/java/org/barcodeapi/server/tasks/ReputationDecayTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void onRun() {
4545
if (rep.isAbuser()) {
4646
LibLog.logF("reputation", //
4747
"Reputation decay: %s has %.4f (+%.4f)", //
48-
limiter.getCaller(), rep.value(), diff);
48+
limiter.getCallerID(), rep.value(), diff);
4949
}
5050
}
5151

0 commit comments

Comments
 (0)