-
Notifications
You must be signed in to change notification settings - Fork 95
Better behaviour in the presence of 429s #786
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 1 commit
2dcd409
c583b33
d51d3ac
19a8173
d7164de
39388b7
f2927d0
8f57de6
8f02b8b
fbbcc41
27a2153
e548996
a4c9e68
c68bc89
91dd6d2
2229a81
ee8e539
036d45b
1e18435
fbdeab1
951dfdd
ed18c48
fda4d64
8a088d7
a9721e4
1ce7b82
bc38b76
addbdca
c97925a
baaa142
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Copyright 2018 Palantir Technologies, Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.remoting3.okhttp; | ||
|
|
||
| import com.codahale.metrics.Meter; | ||
| import com.google.common.util.concurrent.Futures; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
| import com.google.common.util.concurrent.ListeningExecutorService; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import com.google.common.util.concurrent.RateLimiter; | ||
| import com.netflix.concurrency.limits.Limiter; | ||
| import com.palantir.remoting3.okhttp.ConcurrencyLimiters.ConcurrencyLimiter; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Stream; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * This class is a simulation of the flow control primitives used by this library, in order to allow the developer | ||
| * to try different strategies. | ||
| * <p> | ||
| * It is run in CI, but only to prevent code breakages - this is in general a | ||
| */ | ||
| public final class FlowControlTest { | ||
| private static final Logger log = LoggerFactory.getLogger(FlowControlTest.class); | ||
| private static final int REQUESTS_PER_THREAD = System.getenv("CI") == null ? 1000 : 1; | ||
| private static final ConcurrencyLimiters limiters = new ConcurrencyLimiters(); | ||
| private static ListeningExecutorService executorService; | ||
|
|
||
| private final ConcurrencyLimiter limiter = limiters.limiter(UUID.randomUUID().toString()); | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() { | ||
| executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void afterClass() { | ||
| executorService.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| public void test16ThreadsRateLimit20() throws ExecutionException, InterruptedException { | ||
| Meter rate = new Meter(); | ||
| List<ListenableFuture<?>> tasks = createWorkers(rate, 16, 20, Duration.ofMillis(100)) | ||
|
||
| .map(executorService::submit) | ||
| .collect(Collectors.toList()); | ||
| ListenableFuture<?> task = Futures.allAsList(tasks); | ||
| while (!task.isDone()) { | ||
| sleep(1000); | ||
| log.info("Average rate is {}, 1 minute rate is {}", rate.getMeanRate(), rate.getOneMinuteRate()); | ||
|
Contributor
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.
Contributor
Author
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. It's actually kinda interesting, you don't expect to necessarily get that close to the maximum. Basically if the target concurrency rate is small enough, it won't be able to compensate right. So for example in this case, you basically cycle between: 1 thread, 10 requests a second. and you basically end up at about 16, overall. |
||
| } | ||
| task.get(); | ||
| } | ||
|
|
||
| private Stream<Worker> createWorkers(Meter rate, int numThreads, int rateLimit, Duration delay) { | ||
| RateLimiter rateLimiter = RateLimiter.create(rateLimit); | ||
| return IntStream.range(0, numThreads) | ||
| .mapToObj(unused -> new Worker( | ||
| () -> new ExponentialBackoff(4, Duration.ofMillis(250), ThreadLocalRandom.current()), | ||
| limiter, | ||
| delay, | ||
| rateLimiter, | ||
| rate)); | ||
| } | ||
|
|
||
| private static class Worker implements Runnable { | ||
| private final Supplier<BackoffStrategy> backoffFactory; | ||
| private final ConcurrencyLimiter limiter; | ||
| private final Duration successDuration; | ||
| private final RateLimiter rateLimiter; | ||
| private final Meter meter; | ||
|
|
||
| private BackoffStrategy backoff; | ||
|
|
||
| private Worker( | ||
| Supplier<BackoffStrategy> backoffFactory, | ||
| ConcurrencyLimiter limiter, | ||
| Duration successDuration, | ||
| RateLimiter rateLimiter, | ||
| Meter meter) { | ||
| this.backoffFactory = backoffFactory; | ||
| this.limiter = limiter; | ||
| this.successDuration = successDuration; | ||
| this.rateLimiter = rateLimiter; | ||
| this.meter = meter; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| for (int i = 0; i < REQUESTS_PER_THREAD;) { | ||
| Limiter.Listener listener = Futures.getUnchecked(limiter.acquire()); | ||
| boolean gotRateLimited = !rateLimiter.tryAcquire(); | ||
| if (!gotRateLimited) { | ||
| meter.mark(); | ||
| sleep(successDuration.toMillis()); | ||
| listener.onSuccess(); | ||
| backoff = null; | ||
| i++; | ||
| } else { | ||
| initializeBackoff(); | ||
| Optional<Duration> sleep = backoff.nextBackoff(); | ||
| if (!sleep.isPresent()) { | ||
| listener.onIgnore(); | ||
| throw new RuntimeException("Failed on request " + i); | ||
| } else { | ||
| sleep(sleep.get().toMillis()); | ||
| } | ||
| listener.onDropped(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void initializeBackoff() { | ||
j-baker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (backoff != null) { | ||
| return; | ||
| } | ||
| backoff = backoffFactory.get(); | ||
| } | ||
| } | ||
|
|
||
| private static void sleep(long duration) { | ||
| try { | ||
| Thread.sleep(duration); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"...in general a"?