• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Pacer.java
1
/*
2
 * Copyright 2019 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.cache;
17

18
import static com.github.benmanes.caffeine.cache.Caffeine.ceilingPowerOfTwo;
19
import static java.util.Objects.requireNonNull;
20

21
import java.util.concurrent.Executor;
22
import java.util.concurrent.Future;
23
import java.util.concurrent.TimeUnit;
24

25
import org.jspecify.annotations.Nullable;
26

27
/**
28
 * A pacing scheduler that prevents executions from happening too frequently. Only one task may be
29
 * scheduled at any given time, the earliest pending task takes precedence, and the delay may be
30
 * increased if it is less than a tolerance threshold.
31
 *
32
 * @author ben.manes@gmail.com (Ben Manes)
33
 */
34
final class Pacer {
35
  static final long TOLERANCE = ceilingPowerOfTwo(TimeUnit.SECONDS.toNanos(1)); // 1.07s
×
36

37
  final Scheduler scheduler;
38

39
  long nextFireTime;
40
  @Nullable Future<?> future;
41

42
  Pacer(Scheduler scheduler) {
×
43
    this.scheduler = requireNonNull(scheduler);
×
44
  }
×
45

46
  /** Schedules the task, pacing the execution if occurring too often. */
47
  public void schedule(Executor executor, Runnable command, long now, long delay) {
48
    long scheduleAt = (now + delay);
×
49

50
    if (future == null) {
×
51
      // short-circuit an immediate scheduler causing an infinite loop during initialization
52
      if (nextFireTime != 0L) {
×
53
        return;
×
54
      }
55
    } else if ((nextFireTime - now) > 0L) {
×
56
      // Determine whether to reschedule
57
      if (!future.isDone() && maySkip(scheduleAt)) {
×
58
        return;
×
59
      }
60
      future.cancel(/* mayInterruptIfRunning= */ false);
×
61
    }
62
    long actualDelay = calculateSchedule(now, delay, scheduleAt);
×
63
    future = scheduler.schedule(executor, command, actualDelay, TimeUnit.NANOSECONDS);
×
64
  }
×
65

66
  /** Attempts to cancel execution of the scheduled task, if present. */
67
  public void cancel() {
68
    if (future != null) {
×
69
      future.cancel(/* mayInterruptIfRunning= */ false);
×
70
      nextFireTime = 0L;
×
71
      future = null;
×
72
    }
73
  }
×
74

75
  /** Returns if a task is scheduled to run. */
76
  public boolean isScheduled() {
77
    return (future != null) && !future.isDone();
×
78
  }
79

80
  /**
81
   * Returns if the current fire time is sooner, or if it is later and within the tolerance limit.
82
   */
83
  boolean maySkip(long scheduleAt) {
84
    long delta = (scheduleAt - nextFireTime);
×
85
    return (delta >= 0L) || (-delta <= TOLERANCE);
×
86
  }
87

88
  /** Returns the delay and sets the next fire time. */
89
  long calculateSchedule(long now, long delay, long scheduleAt) {
90
    if (delay <= TOLERANCE) {
×
91
      // Use a minimum delay if close to now
92
      nextFireTime = (now + TOLERANCE);
×
93
      return TOLERANCE;
×
94
    }
95
    nextFireTime = scheduleAt;
×
96
    return delay;
×
97
  }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc