@@ -42,13 +42,26 @@ public class RateLimitService {
4242 private final Logger logger = LoggerFactory .getLogger (RateLimitService .class );
4343
4444 private final ProxyManager <byte []> proxyManager ;
45- private final Map <Customer , BucketConfiguration > configurationByCustomer ;
45+ private final Map <Customer , BucketConfigurationWrapper > configurationByCustomer ;
46+ private final Customer anonymousCustomer ;
4647
4748 public RateLimitService (ProxyManager <byte []> proxyManager ) {
4849 this .proxyManager = proxyManager ;
4950 this .configurationByCustomer = new ConcurrentHashMap <>();
51+
52+ // We need to set up a dummy customer for anonymous users as key for the configuration map
53+ // as ConcurrentHashMap does not allow null keys or values.
54+ var customer = new Customer ();
55+ customer .setName ("internalAnonymousCustomer" );
56+ this .anonymousCustomer = customer ;
5057 }
5158
59+ /**
60+ * A wrapper for {@link BucketConfiguration} objects as {@link ConcurrentHashMap}
61+ * does not support {@code null} values.
62+ */
63+ public record BucketConfigurationWrapper (@ Nullable BucketConfiguration configuration ) {}
64+
5265 public record BucketPair (@ Nullable Bucket bucket , long availableTokens ) {
5366 public static BucketPair empty () {
5467 return new BucketPair (null , 0 );
@@ -83,11 +96,11 @@ public BucketPair getBucket(ResolvedIdentity identity) {
8396 }
8497
8598 private @ Nullable BucketConfiguration getBucketConfiguration (ResolvedIdentity identity ) {
99+ var customer = identity .customer () != null ? identity .customer () : anonymousCustomer ;
86100 return configurationByCustomer .computeIfAbsent (
87- // if the identity is not a customer, it will be null
88- identity .customer (),
89- (_ ) -> createBucketConfiguration (identity )
90- );
101+ customer ,
102+ (_ ) -> new BucketConfigurationWrapper (createBucketConfiguration (identity ))
103+ ).configuration ;
91104 }
92105
93106 private @ Nullable BucketConfiguration createBucketConfiguration (ResolvedIdentity identity ) {
0 commit comments