-
Notifications
You must be signed in to change notification settings - Fork 25
feat: polling data source now supports one shot configuration #285
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5fbbd63
feat: polling data source now supports one shot configuration
tanderson-ld f935c96
adding tests
tanderson-ld 9975f55
updating number of polls tracking logic to be simpler
tanderson-ld 8bc80f1
testing Github actions runner image version
tanderson-ld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ final class PollingDataSource implements DataSource { | |
private final DataSourceUpdateSink dataSourceUpdateSink; | ||
final long initialDelayMillis; // visible for testing | ||
final long pollIntervalMillis; // visible for testing | ||
private final int maxNumberOfPolls; | ||
int numberOfPollsRemaining; // visible for testing | ||
private final FeatureFetcher fetcher; | ||
private final PlatformState platformState; | ||
private final TaskExecutor taskExecutor; | ||
|
@@ -36,6 +38,7 @@ final class PollingDataSource implements DataSource { | |
* source will report success immediately as it is now running even if data has not been | ||
* fetched. | ||
* @param pollIntervalMillis interval in millis between each polling request | ||
* @param maxNumberOfPolls the maximum number of polling attempts, unlimited if negative is provided | ||
* @param fetcher that will be used for each fetch | ||
* @param platformState used for making decisions based on platform state | ||
* @param taskExecutor that will be used to schedule the polling tasks | ||
|
@@ -46,6 +49,7 @@ final class PollingDataSource implements DataSource { | |
DataSourceUpdateSink dataSourceUpdateSink, | ||
long initialDelayMillis, | ||
long pollIntervalMillis, | ||
int maxNumberOfPolls, | ||
FeatureFetcher fetcher, | ||
PlatformState platformState, | ||
TaskExecutor taskExecutor, | ||
|
@@ -55,6 +59,8 @@ final class PollingDataSource implements DataSource { | |
this.dataSourceUpdateSink = dataSourceUpdateSink; | ||
this.initialDelayMillis = initialDelayMillis; | ||
this.pollIntervalMillis = pollIntervalMillis; | ||
this.maxNumberOfPolls = maxNumberOfPolls; | ||
this.numberOfPollsRemaining = maxNumberOfPolls; | ||
this.fetcher = fetcher; | ||
this.platformState = platformState; | ||
this.taskExecutor = taskExecutor; | ||
|
@@ -63,15 +69,16 @@ final class PollingDataSource implements DataSource { | |
|
||
@Override | ||
public void start(final Callback<Boolean> resultCallback) { | ||
|
||
if (initialDelayMillis > 0) { | ||
// if there is an initial delay, we will immediately report the successful start of the data source | ||
if (maxNumberOfPolls == 0) { | ||
// If there are no polls to be made, we will immediately report the successful start of the data source. This | ||
// may seem strange, but one can think of this data source as behaving like a no-op in this configuration. | ||
resultCallback.onSuccess(true); | ||
return; | ||
} | ||
tanderson-ld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Runnable pollRunnable = () -> poll(resultCallback); | ||
logger.debug("Scheduling polling task with interval of {}ms, starting after {}ms", | ||
pollIntervalMillis, initialDelayMillis); | ||
logger.debug("Scheduling polling task with interval of {}ms, starting after {}ms, with max number of polls of {}", | ||
pollIntervalMillis, initialDelayMillis, maxNumberOfPolls); | ||
ScheduledFuture<?> task = taskExecutor.startRepeatingTask(pollRunnable, | ||
initialDelayMillis, pollIntervalMillis); | ||
currentPollTask.set(task); | ||
|
@@ -87,7 +94,19 @@ public void stop(Callback<Void> completionCallback) { | |
} | ||
|
||
private void poll(Callback<Boolean> resultCallback) { | ||
ConnectivityManager.fetchAndSetData(fetcher, context, dataSourceUpdateSink, | ||
resultCallback, logger); | ||
// poll if there is no max (negative number) or there are polls remaining | ||
if (maxNumberOfPolls < 0 || numberOfPollsRemaining > 0) { | ||
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. From reviewer comment: Update to use just Long numberOfPollsRemaining and unlimited will set it to Long.MAX 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. Done |
||
ConnectivityManager.fetchAndSetData(fetcher, context, dataSourceUpdateSink, | ||
resultCallback, logger); | ||
numberOfPollsRemaining--; // decrementing even when we have unlimited polls has no consequence | ||
} | ||
tanderson-ld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// terminate if we have a max number of polls and no polls remaining | ||
if (maxNumberOfPolls >= 0 && numberOfPollsRemaining <= 0) { | ||
ScheduledFuture<?> task = currentPollTask.getAndSet(null); | ||
if (task != null) { | ||
task.cancel(true); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.