Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1047 from zalando/INC-44977-hacky-throttle
Browse files Browse the repository at this point in the history
INC-44977: hacky throttle for stups_zalos;
  • Loading branch information
rcillo authored May 8, 2019
2 parents 47a4223 + 2dc7eb3 commit a420d10
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

Expand All @@ -55,6 +56,7 @@
import static org.zalando.problem.Status.INTERNAL_SERVER_ERROR;
import static org.zalando.problem.Status.NOT_FOUND;
import static org.zalando.problem.Status.SERVICE_UNAVAILABLE;
import static org.zalando.problem.Status.TOO_MANY_REQUESTS;
import static org.zalando.problem.Status.UNPROCESSABLE_ENTITY;

@RestController
Expand All @@ -70,6 +72,7 @@ public class SubscriptionStreamController {
private final MetricRegistry metricRegistry;
private final SubscriptionDbRepository subscriptionDbRepository;
private final SubscriptionValidationService subscriptionValidationService;
private final Random random;

@Autowired
public SubscriptionStreamController(final SubscriptionStreamerFactory subscriptionStreamerFactory,
Expand All @@ -88,6 +91,7 @@ public SubscriptionStreamController(final SubscriptionStreamerFactory subscripti
this.metricRegistry = metricRegistry;
this.subscriptionDbRepository = subscriptionDbRepository;
this.subscriptionValidationService = subscriptionValidationService;
this.random = new Random();
}

class SubscriptionOutputImpl implements SubscriptionOutput {
Expand Down Expand Up @@ -212,6 +216,12 @@ private StreamingResponseBody stream(final String subscriptionId,
SubscriptionStreamer streamer = null;
final SubscriptionOutputImpl output = new SubscriptionOutputImpl(response, outputStream);
try {
if ("stups_zalos".equals(client.getClientId()) && random.nextInt(10) >= 5) {
writeProblemResponse(response, outputStream,
Problem.valueOf(TOO_MANY_REQUESTS, "Application stups_zalos is throttled"));
return;
}

if (blacklistService.isSubscriptionConsumptionBlocked(subscriptionId, client.getClientId())) {
writeProblemResponse(response, outputStream,
Problem.valueOf(FORBIDDEN, "Application or event type is blocked"));
Expand Down

0 comments on commit a420d10

Please sign in to comment.