Skip to content
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

remove the notion of Clock #684

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.smallrye.faulttolerance.core.circuit.breaker.CircuitBreaker;
import io.smallrye.faulttolerance.core.circuit.breaker.CircuitBreakerEvents;
import io.smallrye.faulttolerance.core.circuit.breaker.CompletionStageCircuitBreaker;
import io.smallrye.faulttolerance.core.clock.SystemClock;
import io.smallrye.faulttolerance.core.fallback.CompletionStageFallback;
import io.smallrye.faulttolerance.core.fallback.Fallback;
import io.smallrye.faulttolerance.core.fallback.FallbackFunction;
Expand Down Expand Up @@ -256,7 +255,7 @@ private FaultToleranceStrategy<T> buildSyncStrategy(BuilderLazyDependencies lazy
rateLimitBuilder.timeWindowInMillis,
rateLimitBuilder.minSpacingInMillis,
rateLimitBuilder.type,
SystemClock.INSTANCE);
SystemStopwatch.INSTANCE);
}

if (lazyDependencies.ftEnabled() && circuitBreakerBuilder != null) {
Expand Down Expand Up @@ -318,7 +317,7 @@ private <V> FaultToleranceStrategy<CompletionStage<V>> buildAsyncStrategy(Builde
rateLimitBuilder.timeWindowInMillis,
rateLimitBuilder.minSpacingInMillis,
rateLimitBuilder.type,
SystemClock.INSTANCE);
SystemStopwatch.INSTANCE);
}

if (lazyDependencies.ftEnabled() && circuitBreakerBuilder != null) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import io.smallrye.faulttolerance.api.RateLimitType;
import io.smallrye.faulttolerance.core.FaultToleranceStrategy;
import io.smallrye.faulttolerance.core.InvocationContext;
import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

public class CompletionStageRateLimit<V> extends RateLimit<CompletionStage<V>> {
public CompletionStageRateLimit(FaultToleranceStrategy<CompletionStage<V>> delegate, String description, int maxInvocations,
long timeWindowInMillis, long minSpacingInMillis, RateLimitType type, Clock clock) {
super(delegate, description, maxInvocations, timeWindowInMillis, minSpacingInMillis, type, clock);
long timeWindowInMillis, long minSpacingInMillis, RateLimitType type, Stopwatch stopwatch) {
super(delegate, description, maxInvocations, timeWindowInMillis, minSpacingInMillis, type, stopwatch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.smallrye.faulttolerance.core.rate.limit;

import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.RunningStopwatch;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

final class FixedWindow implements TimeWindow {
private final Clock clock;
private final RunningStopwatch stopwatch;

private final int maxInvocations;
private final long timeWindowInMillis;
Expand All @@ -14,21 +15,20 @@ final class FixedWindow implements TimeWindow {

private long lastInvocation;

FixedWindow(Clock clock, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.clock = clock;
FixedWindow(Stopwatch stopwatch, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.stopwatch = stopwatch.start();
this.maxInvocations = maxInvocations;
this.timeWindowInMillis = timeWindowInMillis;
this.minSpacingInMillis = minSpacingInMillis;

long now = clock.currentTimeInMillis();
this.currentPermits = maxInvocations;
this.nextRefresh = now + timeWindowInMillis;
this.lastInvocation = now - minSpacingInMillis;
this.nextRefresh = timeWindowInMillis;
this.lastInvocation = -minSpacingInMillis;
}

@Override
public synchronized boolean record() {
long now = clock.currentTimeInMillis();
long now = stopwatch.elapsedTimeInMillis();
if (now >= nextRefresh) {
currentPermits = maxInvocations;
// how many time windows has passed: (now - nextRefresh) / timeWindowInMillis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import java.util.ArrayList;
import java.util.List;

import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.RunningStopwatch;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

final class NaiveRollingWindow implements TimeWindow {
private final Clock clock;
private final RunningStopwatch stopwatch;

private final int maxInvocations;
private final long timeWindowInMillis;
private final long minSpacingInMillis;

private final List<Long> timestamps = new ArrayList<>();

NaiveRollingWindow(Clock clock, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.clock = clock;
NaiveRollingWindow(Stopwatch stopwatch, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.stopwatch = stopwatch.start();
this.maxInvocations = maxInvocations;
this.timeWindowInMillis = timeWindowInMillis;
this.minSpacingInMillis = minSpacingInMillis;
}

@Override
public synchronized boolean record() {
long now = clock.currentTimeInMillis();
long now = stopwatch.elapsedTimeInMillis();
long validity = now - timeWindowInMillis; // all entries before or at this timestamp have expired

timestamps.removeIf(it -> it <= validity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.smallrye.faulttolerance.api.RateLimitType;
import io.smallrye.faulttolerance.core.FaultToleranceStrategy;
import io.smallrye.faulttolerance.core.InvocationContext;
import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

public class RateLimit<V> implements FaultToleranceStrategy<V> {
final FaultToleranceStrategy<V> delegate;
Expand All @@ -17,17 +17,17 @@ public class RateLimit<V> implements FaultToleranceStrategy<V> {
final TimeWindow timeWindow;

public RateLimit(FaultToleranceStrategy<V> delegate, String description, int maxInvocations, long timeWindowInMillis,
long minSpacingInMillis, RateLimitType type, Clock clock) {
long minSpacingInMillis, RateLimitType type, Stopwatch stopwatch) {
this.delegate = checkNotNull(delegate, "Rate limit delegate must be set");
this.description = checkNotNull(description, "Rate limit description must be set");
checkNotNull(type, "Rate limit type must be set");
check(maxInvocations, maxInvocations > 0, "Max invocations must be > 0");
check(timeWindowInMillis, timeWindowInMillis > 0, "Time window length must be > 0");
check(minSpacingInMillis, minSpacingInMillis >= 0, "Min spacing must be >= 0");
checkNotNull(clock, "Clock must be set");
checkNotNull(stopwatch, "Stopwatch must be set");
this.timeWindow = type == RateLimitType.FIXED
? TimeWindow.createFixed(clock, maxInvocations, timeWindowInMillis, minSpacingInMillis)
: TimeWindow.createRolling(clock, maxInvocations, timeWindowInMillis, minSpacingInMillis);
? TimeWindow.createFixed(stopwatch, maxInvocations, timeWindowInMillis, minSpacingInMillis)
: TimeWindow.createRolling(stopwatch, maxInvocations, timeWindowInMillis, minSpacingInMillis);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.Arrays;

import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.RunningStopwatch;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

final class RingBufferRollingWindow implements TimeWindow {
private final Clock clock;
private final RunningStopwatch stopwatch;

private final long timeWindowInMillis;
private final long minSpacingInMillis;
Expand All @@ -15,8 +16,8 @@ final class RingBufferRollingWindow implements TimeWindow {
private int head; // index of newest entry
private int tail; // index of oldest still valid entry

RingBufferRollingWindow(Clock clock, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.clock = clock;
RingBufferRollingWindow(Stopwatch stopwatch, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
this.stopwatch = stopwatch.start();
this.timeWindowInMillis = timeWindowInMillis;
this.minSpacingInMillis = minSpacingInMillis;
this.timestamps = new long[maxInvocations];
Expand All @@ -27,7 +28,7 @@ final class RingBufferRollingWindow implements TimeWindow {

@Override
public synchronized boolean record() {
long now = clock.currentTimeInMillis();
long now = stopwatch.elapsedTimeInMillis();
long validity = now - timeWindowInMillis; // all entries before or at this timestamp have expired

while (timestamps[tail] <= validity && head != tail) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.smallrye.faulttolerance.core.rate.limit;

import io.smallrye.faulttolerance.core.clock.Clock;
import io.smallrye.faulttolerance.core.stopwatch.Stopwatch;

public interface TimeWindow {
/**
Expand All @@ -10,11 +10,11 @@ public interface TimeWindow {
*/
boolean record();

static TimeWindow createFixed(Clock clock, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
return new FixedWindow(clock, maxInvocations, timeWindowInMillis, minSpacingInMillis);
static TimeWindow createFixed(Stopwatch stopwatch, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
return new FixedWindow(stopwatch, maxInvocations, timeWindowInMillis, minSpacingInMillis);
}

static TimeWindow createRolling(Clock clock, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
return new RingBufferRollingWindow(clock, maxInvocations, timeWindowInMillis, minSpacingInMillis);
static TimeWindow createRolling(Stopwatch stopwatch, int maxInvocations, long timeWindowInMillis, long minSpacingInMillis) {
return new RingBufferRollingWindow(stopwatch, maxInvocations, timeWindowInMillis, minSpacingInMillis);
}
}

This file was deleted.

This file was deleted.

Loading