Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.yashdotdev.distributed_traffic_control.config;

import com.yashdotdev.distributed_traffic_control.lease.GlobalCapacityKey;
import com.yashdotdev.distributed_traffic_control.traffic.TrafficSubject;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;

public class RedisKeyBuilder {

private final String namespace;

public RedisKeyBuilder(String namespace) {
if (namespace == null || namespace.isBlank() || namespace.endsWith(":")) {
throw new IllegalArgumentException(
"Redis key namespace must not be null or blank and must not end with ':'"
);
}
this.namespace = namespace;
}

public RedisKeyBuilder(RedisKeyProperties properties) {
if (properties == null) {
throw new IllegalArgumentException("properties must not be null");
}
String ns = properties.getKeyNamespace();
if (ns == null || ns.isBlank() || ns.endsWith(":")) {
throw new IllegalArgumentException(
"Redis key namespace must not be null or blank and must not end with ':'"
);
}
this.namespace = ns;
}

public String getNamespace() {
return namespace;
}

public String defaultPolicyKey() {
return namespace + ":policies:default";
}

public String resourcePolicyKey(String resource) {
return namespace + ":policies:resource:" + encode(resource);
}

public String subjectPolicyKey(
TrafficSubject subject,
String resource) {

Objects.requireNonNull(subject, "subject must not be null");

return namespace + ":policies:subject:"
+ encode(subject.getType().name())
+ ":"
+ encode(subject.getSubjectId())
+ ":"
+ encode(resource);
}

public String resourceIndexKey() {
return namespace + ":policies:resources";
}

public String globalCapacityKey(GlobalCapacityKey capacityKey) {
Objects.requireNonNull(capacityKey, "capacityKey must not be null");

Check failure on line 67 in src/main/java/com/yashdotdev/distributed_traffic_control/config/RedisKeyBuilder.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "capacityKey must not be null" 3 times.

See more on https://sonarcloud.io/project/issues?id=yashdotdev13_distributed-traffic-control&issues=AaIExBKPFqfB9fmDtud4&open=AaIExBKPFqfB9fmDtud4&pullRequest=70

return namespace + ":quota:"
+ capacityKey.policyId()
+ ":"
+ capacityKey.resource();
}

public String leaseRegistryKey(GlobalCapacityKey capacityKey) {
Objects.requireNonNull(capacityKey, "capacityKey must not be null");
return globalCapacityKey(capacityKey) + ":leases";
}

public String leaseKey(String leaseId) {
if (leaseId == null || leaseId.isBlank()) {
throw new IllegalArgumentException(
"leaseId must not be null or blank");
}

return namespace + ":lease:" + leaseId;
}

public String tokenBucketKey(GlobalCapacityKey capacityKey) {
Objects.requireNonNull(capacityKey, "capacityKey must not be null");

return namespace + ":token-bucket:"
+ capacityKey.policyId()
+ ":"
+ capacityKey.resource();
}

private String encode(String value) {
return Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(value.getBytes(StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

package com.yashdotdev.distributed_traffic_control.config;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class RedisKeyNamespaceValidator
implements ConstraintValidator<ValidRedisKeyNamespace, String> {

@Override
public boolean isValid(
String value,
ConstraintValidatorContext context) {

if (value == null) {
return true;
}

return !value.endsWith(":");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.yashdotdev.distributed_traffic_control.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;

@ConfigurationProperties(prefix = "traffic-control.redis")
@Validated
public class RedisKeyProperties {

@NotBlank(message = "Redis key prefix must not be blank")
@ValidRedisKeyNamespace
private String keyNamespace = "traffic-control";

public RedisKeyProperties() {
}

public RedisKeyProperties(String keyNamespace) {
this.keyNamespace = keyNamespace;
}

public String getKeyNamespace() {
return keyNamespace;
}

public void setKeyNamespace(String keyNamespace) {
this.keyNamespace = keyNamespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.yashdotdev.distributed_traffic_control.traffic.TrafficDecisionEngine;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand All @@ -28,6 +30,7 @@

@Configuration
@Import(AllocationStrategyConfiguration.class)
@EnableConfigurationProperties(RedisKeyProperties.class)
public class TrafficControlConfiguration {

@Bean
Expand Down Expand Up @@ -66,9 +69,11 @@ public QuotaCoordinator quotaCoordinator(Clock clock) {
public LeaseCoordinator leaseCoordinator(
StringRedisTemplate redisTemplate,
Clock clock,
ObservationRegistry observationRegistry) {
ObservationRegistry observationRegistry,
RedisKeyBuilder redisKeyBuilder) {
return new RedisLeaseCoordinator(
redisTemplate,
redisKeyBuilder,
clock,
observationRegistry);
}
Expand Down Expand Up @@ -109,4 +114,8 @@ public TrafficControlMetrics trafficControlMetrics(MeterRegistry meterRegistry)
return new TrafficControlMetrics(meterRegistry);
}

}
@Bean
public RedisKeyBuilder redisKeyBuilder(RedisKeyProperties properties) {
return new RedisKeyBuilder(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

package com.yashdotdev.distributed_traffic_control.config;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint(validatedBy = RedisKeyNamespaceValidator.class)
@Target({
ElementType.FIELD,
ElementType.PARAMETER,
ElementType.ANNOTATION_TYPE
})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidRedisKeyNamespace {

String message() default "Redis key namespace must not end with a colon";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.yashdotdev.distributed_traffic_control.lease;


public record GlobalCapacityKey(String policyId, String resource) {

public GlobalCapacityKey {
Expand Down
Loading
Loading