@Test public void test() throws Exception { ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory(); SharedLimiterKey res1key = new SharedLimiterKey("res1"); Map<String, String> configMap = ImmutableMap.<String, String>builder() .put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS) .put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "100") .build(); ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig(); guiceServletConfig.initialize(ConfigFactory.parseMap(configMap)); Injector injector = guiceServletConfig.getInjector(); EmbeddedRestliServer server = EmbeddedRestliServer.builder().resources( Lists.<Class<? extends BaseResource>>newArrayList(LimiterServerResource.class)).injector(injector).build(); SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); try { server.startAsync(); server.awaitRunning(); RestliServiceBasedLimiter limiter = RestliServiceBasedLimiter.builder() .requestSender(new RedirectAwareRestClientRequestSender(broker, Lists.newArrayList(server.getURIPrefix()))) .resourceLimited(res1key.getResourceLimitedPath()).serviceIdentifier("service").build(); Assert.assertNotNull(limiter.acquirePermits(20)); Assert.assertNotNull(limiter.acquirePermits(20)); Assert.assertNull(limiter.acquirePermits(1000)); } finally { if (server.isRunning()) { server.stopAsync(); server.awaitTerminated(); } } }
@Override public Closeable acquirePermits(long permits) throws InterruptedException { Instrumented.markMeter(this.permitsRequestedMeter, permits); boolean permitsGranted = this.bachedPermitsContainer.getPermits(permits); Instrumented.markMeter(this.permitsGrantedMeter, permits); return permitsGranted ? NoopCloseable.INSTANCE : null; }
RestliServiceBasedLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Instrumented.markMeter(this.permitsRequestedMeter, permits); boolean permitsGranted = this.bachedPermitsContainer.getPermits(permits); Instrumented.markMeter(this.permitsGrantedMeter, permits); return permitsGranted ? NoopCloseable.INSTANCE : null; } }
RestliServiceBasedLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Instrumented.markMeter(this.permitsRequestedMeter, permits); boolean permitsGranted = this.bachedPermitsContainer.getPermits(permits); Instrumented.markMeter(this.permitsGrantedMeter, permits); return permitsGranted ? NoopCloseable.INSTANCE : null; } @Builder private  RestliServiceBasedLimiter(String resourceLimited, String serviceIdentifier,
      MetricContext metricContext, RequestSender requestSender, long permitRequestTimeoutMillis); }
RestliServiceBasedLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Instrumented.markMeter(this.permitsRequestedMeter, permits); boolean permitsGranted = this.bachedPermitsContainer.getPermits(permits); Instrumented.markMeter(this.permitsGrantedMeter, permits); return permitsGranted ? NoopCloseable.INSTANCE : null; } @Builder private  RestliServiceBasedLimiter(String resourceLimited, String serviceIdentifier,
      MetricContext metricContext, RequestSender requestSender, long permitRequestTimeoutMillis); @Override void start(); @Override Closeable acquirePermits(long permits); @Override void stop(); @VisibleForTesting long getUnusedPermits(); @VisibleForTesting void clearAllStoredPermits(); }
RestliServiceBasedLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Instrumented.markMeter(this.permitsRequestedMeter, permits); boolean permitsGranted = this.bachedPermitsContainer.getPermits(permits); Instrumented.markMeter(this.permitsGrantedMeter, permits); return permitsGranted ? NoopCloseable.INSTANCE : null; } @Builder private  RestliServiceBasedLimiter(String resourceLimited, String serviceIdentifier,
      MetricContext metricContext, RequestSender requestSender, long permitRequestTimeoutMillis); @Override void start(); @Override Closeable acquirePermits(long permits); @Override void stop(); @VisibleForTesting long getUnusedPermits(); @VisibleForTesting void clearAllStoredPermits(); static final String PERMITS_REQUESTED_METER_NAME; static final String PERMITS_GRANTED_METER_NAME; }