-
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
server-session: remove busy-wait loop in App.java using ScheduledExecutorService #3445
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 all commits
5b89975
3156d06
0c0ebab
cfb221d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.Predicate; | ||
|
|
||
|
|
@@ -53,6 +56,8 @@ public interface HandleErrorIssue<T> { | |
| } | ||
|
|
||
| private static final SecureRandom RANDOM = new SecureRandom(); | ||
| private static final ScheduledExecutorService scheduler = | ||
| Executors.newSingleThreadScheduledExecutor(); | ||
|
|
||
| private final Operation op; | ||
| private final HandleErrorIssue<T> handleError; | ||
|
|
@@ -98,9 +103,23 @@ public void perform(List<Exception> list, T obj) { | |
| long testDelay = | ||
| (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000); | ||
| long delay = Math.min(testDelay, this.maxDelay); | ||
| Thread.sleep(delay); | ||
| } catch (InterruptedException f) { | ||
| // ignore | ||
| scheduler.schedule(() -> perform(list, obj), delay, TimeUnit.MILLISECONDS); | ||
| return; | ||
| } catch (Exception j) { | ||
| this.errors.add(j); | ||
|
|
||
| if (this.attempts.incrementAndGet() >= this.maxAttempts || !this.test.test(j)) { | ||
| this.handleError.handleIssue(obj, j); | ||
| return; | ||
| } | ||
|
|
||
| long testDelay = | ||
| (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000); | ||
|
|
||
| long delay = Math.min(testDelay, this.maxDelay); | ||
|
|
||
| scheduler.schedule(() -> perform(list, obj), delay, TimeUnit.MILLISECONDS); | ||
| return; | ||
| } | ||
|
Comment on lines
103
to
123
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. This backoff now uses a scheduled task to retry. Watch for concurrency: the same 'list' and 'errors' structures are captured and reused by the scheduled task, which may execute in parallel with the caller thread. If 'errors' is not thread-safe, this may cause concurrent modification exceptions or data races. Recommend making 'errors' a thread-safe list (e.g., CopyOnWriteArrayList) or guarding modifications with synchronization. Also consider whether a per-attempt scheduling model is intended to share a single scheduler across all retries; if not, scope the scheduler to the Retry instance. |
||
| } | ||
| } while (true); | ||
|
|
||
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.
Potential concurrency risk: a static singleton scheduler is used to drive retries for all Retry instances. This can cause race conditions on shared state (errors/list) if multiple invocations run in parallel. Consider per-instance scheduling or ensure proper lifecycle shutdown of the scheduler, and make shared state thread-safe.