Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2dcd409
Better behaviour in the presence of backing off
j-baker Aug 2, 2018
c583b33
Merge branch 'develop' into jbaker/better_429_behaviour
j-baker Aug 2, 2018
d51d3ac
more docs, easier to read
j-baker Aug 2, 2018
19a8173
Better concurrency limiters
j-baker Aug 3, 2018
d7164de
fixes
j-baker Aug 3, 2018
39388b7
simplify
j-baker Aug 3, 2018
f2927d0
Checkstyle
j-baker Aug 13, 2018
8f57de6
checkstyle
j-baker Aug 13, 2018
8f02b8b
PR comments
j-baker Sep 2, 2018
fbbcc41
Tweak the concurrency limiters lib
j-baker Sep 2, 2018
27a2153
reset flow control test
j-baker Sep 2, 2018
e548996
changes
j-baker Sep 2, 2018
a4c9e68
Changes
j-baker Sep 2, 2018
c68bc89
Passes the build
j-baker Sep 2, 2018
91dd6d2
update lockfiles
j-baker Sep 2, 2018
2229a81
Merge remote-tracking branch 'origin/develop' into jbaker/better_429_…
j-baker Sep 11, 2018
ee8e539
New attempt using interceptor
j-baker Sep 11, 2018
036d45b
more comments
j-baker Sep 11, 2018
1e18435
some bullshit
j-baker Sep 12, 2018
fbdeab1
Perfect
j-baker Sep 12, 2018
951dfdd
cleanup
j-baker Sep 12, 2018
ed18c48
Ready to go?
j-baker Sep 13, 2018
fda4d64
docs
j-baker Sep 13, 2018
8a088d7
Checkstyle
j-baker Sep 13, 2018
a9721e4
chekcstyle
j-baker Sep 13, 2018
1ce7b82
Metric
j-baker Sep 13, 2018
bc38b76
Javadoc
iamdanfox Sep 13, 2018
addbdca
README describes new flow control
iamdanfox Sep 13, 2018
c97925a
Move docs -> class level javadoc
iamdanfox Sep 13, 2018
baaa142
Rename ConcurrencyLimiters#limiter -> acquireLimiter
iamdanfox Sep 13, 2018
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
Prev Previous commit
Next Next commit
Metric
  • Loading branch information
j-baker committed Sep 13, 2018
commit 1ce7b82a3ade75241cd621e4537abb8f2dfbd351
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@

package com.palantir.remoting3.okhttp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamdanfox what's the deal with remoting-vs-conjure in PRs?


import com.codahale.metrics.Timer;
import com.google.common.annotations.VisibleForTesting;
import com.netflix.concurrency.limits.Limiter;
import com.netflix.concurrency.limits.limit.VegasLimit;
import com.netflix.concurrency.limits.limiter.SimpleLimiter;
import com.palantir.logsafe.SafeArg;
import com.palantir.remoting3.tracing.okhttp3.OkhttpTraceInterceptor;
import com.palantir.tritium.metrics.registry.MetricName;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,17 +40,21 @@ class ConcurrencyLimiters {
private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
private static final Void NO_CONTEXT = null;
private static final String FALLBACK = "";
private static final MetricName SLOW_ACQUIRE =
MetricName.builder().safeName("conjure-java-client.qos.request-permit.slow-acquire").build();

private final Timer slowAcquire;
private final ConcurrentMap<String, Limiter<Void>> limiters = new ConcurrentHashMap<>();
private final Duration timeout;

@VisibleForTesting
ConcurrencyLimiters(Duration timeout) {
ConcurrencyLimiters(TaggedMetricRegistry taggedMetricRegistry, Duration timeout) {
this.slowAcquire = taggedMetricRegistry.timer(SLOW_ACQUIRE);
this.timeout = timeout;
}

ConcurrencyLimiters() {
this(DEFAULT_TIMEOUT);
ConcurrencyLimiters(TaggedMetricRegistry taggedMetricRegistry) {
this(taggedMetricRegistry, DEFAULT_TIMEOUT);
}

Limiter.Listener limiter(Request request) {
Expand All @@ -71,9 +79,9 @@ Limiter.Listener limiter(String name) {
}

private Limiter<Void> newLimiter() {
Limiter<Void> limiter = SimpleLimiter.newBuilder()
Limiter<Void> limiter = new InstrumentedLimiter(SimpleLimiter.newBuilder()
.limit(new RemotingWindowedLimit(VegasLimit.newDefault()))
.build();
.build(), slowAcquire);
return RemotingBlockingLimiter.wrap(limiter, timeout);
}

Expand All @@ -85,4 +93,28 @@ private static String limiterKey(Request request) {
return request.method() + " " + pathTemplate;
}
}

private static final class InstrumentedLimiter implements Limiter<Void> {
private final Limiter<Void> delegate;
private final Timer timer;

private InstrumentedLimiter(Limiter<Void> delegate, Timer timer) {
this.delegate = delegate;
this.timer = timer;
}

@Override
public Optional<Listener> acquire(Void context) {
long start = System.nanoTime();
try {
return delegate.acquire(context);
} finally {
long end = System.nanoTime();
long durationNanos = end - start;
if (TimeUnit.NANOSECONDS.toMillis(durationNanos) > 1) {
timer.update(durationNanos, TimeUnit.NANOSECONDS);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.netflix.concurrency.limits.Limiter;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -62,8 +63,8 @@ final class ConcurrencyLimitingInterceptor implements Interceptor {
this.limiters = limiters;
}

ConcurrencyLimitingInterceptor() {
this(new ConcurrencyLimiters());
ConcurrencyLimitingInterceptor(TaggedMetricRegistry taggedMetricRegistry) {
this(new ConcurrencyLimiters(taggedMetricRegistry));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static RemotingOkHttpClient createInternal(
client.sslSocketFactory(config.sslSocketFactory(), config.trustManager());

// Intercept calls to augment request meta data
client.addInterceptor(new ConcurrencyLimitingInterceptor());
client.addInterceptor(new ConcurrencyLimitingInterceptor(registry));
client.addInterceptor(InstrumentedInterceptor.create(registry, hostMetrics, serviceClass));
client.addInterceptor(OkhttpTraceInterceptor.INSTANCE);
client.addInterceptor(UserAgentInterceptor.of(augmentUserAgent(userAgent, serviceClass)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry;
import java.time.Duration;
import java.time.Instant;
import org.junit.Test;

public final class ConcurrencyLimitersTest {
private static final String KEY = "";
private static final Duration TIMEOUT = Duration.ofSeconds(1);
private final ConcurrencyLimiters limiters = new ConcurrencyLimiters(TIMEOUT);
private final ConcurrencyLimiters limiters = new ConcurrencyLimiters(new DefaultTaggedMetricRegistry(), TIMEOUT);

@Test
public void testTimeout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.concurrency.limits.Limiter;
import com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
Expand Down Expand Up @@ -58,7 +59,7 @@ public final class FlowControlTest {
private static final int REQUESTS_PER_THREAD = 5;
private static ListeningExecutorService executorService;

private final ConcurrencyLimiters limiters = new ConcurrencyLimiters();
private final ConcurrencyLimiters limiters = new ConcurrencyLimiters(new DefaultTaggedMetricRegistry());

@BeforeClass
public static void beforeClass() {
Expand Down