Skip to content

Commit

Permalink
add GRPC_EXPERIMENTAL_RLQS_DRY_RUN
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Oct 17, 2024
1 parent 48f5673 commit ad4d9f9
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions xds/src/main/java/io/grpc/xds/RlqsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaBucketSettings;
import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaFilterConfig;
import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaOverride;
import io.grpc.InternalLogId;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.internal.GrpcUtil;
import io.grpc.xds.Filter.ServerInterceptorBuilder;
import io.grpc.xds.client.XdsLogger;
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
import io.grpc.xds.internal.datatype.GrpcService;
import io.grpc.xds.internal.matchers.HttpMatchInput;
import io.grpc.xds.internal.matchers.Matcher;
Expand All @@ -53,6 +56,10 @@
final class RlqsFilter implements Filter, ServerInterceptorBuilder {
static final boolean enabled = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_ENABLE_RLQS", false);

// TODO(sergiitk): [IMPL] remove
// Do do not fail on parsing errors, only log requests.
static final boolean dryRun = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_RLQS_DRY_RUN", false);

static final RlqsFilter INSTANCE = new RlqsFilter();

static final String TYPE_URL = "type.googleapis.com/"
Expand All @@ -62,6 +69,15 @@ final class RlqsFilter implements Filter, ServerInterceptorBuilder {

private final AtomicReference<RlqsCache> rlqsCache = new AtomicReference<>();

private final InternalLogId logId;
private final XdsLogger logger;

public RlqsFilter() {
logId = InternalLogId.allocate("rlqs-filter", null);
logger = XdsLogger.withLogId(logId);
logger.log(XdsLogLevel.INFO, "Created RLQS Filter with logId=" + logId);
}

@Override
public String[] typeUrls() {
return new String[]{TYPE_URL, TYPE_URL_OVERRIDE_CONFIG};
Expand Down Expand Up @@ -158,7 +174,15 @@ private ServerInterceptor generateRlqsInterceptor(RlqsFilterConfig config) {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
RlqsRateLimitResult result = rlqsEngine.rateLimit(HttpMatchInput.create(headers, call));
HttpMatchInput httpMatchInput = HttpMatchInput.create(headers, call);

// TODO(sergiitk): [IMPL] Remove
if (dryRun) {
logger.log(XdsLogLevel.INFO, "RLQS DRY RUN: request <<" + httpMatchInput + ">>");
return next.startCall(call, headers);
}

RlqsRateLimitResult result = rlqsEngine.rateLimit(httpMatchInput);
if (result.isAllowed()) {
return next.startCall(call, headers);
}
Expand All @@ -170,7 +194,7 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(
}

@VisibleForTesting
static RlqsFilterConfig parseRlqsFilter(RateLimitQuotaFilterConfig rlqsFilterProto)
RlqsFilterConfig parseRlqsFilter(RateLimitQuotaFilterConfig rlqsFilterProto)
throws ResourceInvalidException, InvalidProtocolBufferException {
RlqsFilterConfig.Builder builder = RlqsFilterConfig.builder();
if (rlqsFilterProto.getDomain().isEmpty()) {
Expand All @@ -179,6 +203,12 @@ static RlqsFilterConfig parseRlqsFilter(RateLimitQuotaFilterConfig rlqsFilterPro
builder.domain(rlqsFilterProto.getDomain())
.rlqsService(GrpcService.fromEnvoyProto(rlqsFilterProto.getRlqsServer()));

// TODO(sergiitk): [IMPL] Remove
if (dryRun) {
logger.log(XdsLogLevel.INFO, "RLQS DRY RUN: skipping matchers");
return builder.build();
}

// TODO(sergiitk): [IMPL] actually parse, move to RlqsBucketSettings.fromProto()
RateLimitQuotaBucketSettings fallbackBucketSettingsProto = unpackAny(
rlqsFilterProto.getBucketMatchers().getOnNoMatch().getAction().getTypedConfig(),
Expand Down

0 comments on commit ad4d9f9

Please sign in to comment.