-
Notifications
You must be signed in to change notification settings - Fork 1
feat(discovery): apply policy filtering to home feed #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
808ab54
0057e2c
a658ddb
11df057
5635379
0069081
02858a7
e7fcb65
75ce5d8
b0d271c
e4f73da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.cloudmedia.discovery.api.validation; | ||
|
|
||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
| import java.util.Arrays; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class CountryCodeValidator implements ConstraintValidator<ValidCountryCode, String> { | ||
|
|
||
| private static final Set<String> ISO_COUNTRY_CODES = Arrays.stream(Locale.getISOCountries()) | ||
| .collect(Collectors.toUnmodifiableSet()); | ||
|
|
||
| private static final Pattern COUNTRY_CODE_PATTERN = Pattern.compile("^[A-Z]{2}$"); | ||
|
|
||
| @Override | ||
| public boolean isValid(String value, ConstraintValidatorContext context) { | ||
| if (value == null) { | ||
| return true; | ||
| } | ||
| return COUNTRY_CODE_PATTERN.matcher(value).matches() && ISO_COUNTRY_CODES.contains(value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.cloudmedia.discovery.api.validation; | ||
|
|
||
| import jakarta.validation.Constraint; | ||
| import jakarta.validation.Payload; | ||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Documented | ||
| @Constraint(validatedBy = CountryCodeValidator.class) | ||
| @Target({ElementType.PARAMETER, ElementType.FIELD}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface ValidCountryCode { | ||
|
|
||
| String message() default "must be an ISO 3166-1 alpha-2 uppercase code"; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,21 @@ | ||
| package com.cloudmedia.discovery.policy; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public interface PolicyEvaluationClient { | ||
|
|
||
| PolicyDecision evaluate(String contentId, String countryCode, Boolean ageVerified); | ||
|
|
||
| default Map<String, PolicyDecision> evaluateBatch(Collection<String> contentIds, String countryCode, | ||
| Boolean ageVerified) { | ||
| Map<String, PolicyDecision> decisions = new LinkedHashMap<>(); | ||
| for (String contentId : contentIds) { | ||
| if (contentId != null && !decisions.containsKey(contentId)) { | ||
| decisions.put(contentId, evaluate(contentId, countryCode, ageVerified)); | ||
| } | ||
| } | ||
| return decisions; | ||
| } | ||
|
Comment on lines
+11
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sequential HTTP calls may cause high latency on home feed requests. The default implementation makes N sequential HTTP calls to the policy service (one per unique content ID). For a home feed with 30-50 items, this could add 100-300ms+ of cumulative latency under normal conditions, and much worse under load or partial service degradation. Consider parallelizing the calls or introducing a true batch endpoint on the policy service: ♻️ Option 1: Parallel execution with CompletableFuturedefault Map<String, PolicyDecision> evaluateBatch(Collection<String> contentIds, String countryCode,
Boolean ageVerified) {
List<String> uniqueIds = contentIds.stream()
.filter(Objects::nonNull)
.distinct()
.toList();
Map<String, CompletableFuture<PolicyDecision>> futures = new LinkedHashMap<>();
for (String contentId : uniqueIds) {
futures.put(contentId, CompletableFuture.supplyAsync(
() -> evaluate(contentId, countryCode, ageVerified)));
}
Map<String, PolicyDecision> decisions = new LinkedHashMap<>();
for (var entry : futures.entrySet()) {
decisions.put(entry.getKey(), entry.getValue().join());
}
return decisions;
}This requires adding ♻️ Option 2: True batch API (preferred if policy service supports it)Override 🤖 Prompt for AI Agents |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.