Skip to content

Commit 40d70e9

Browse files
committed
Consolidate implementation.
1 parent d34565a commit 40d70e9

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/integrations/FileDataSourceImpl.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class FileDataSourceImpl implements DataSource {
3333
private final LDLogger logger;
3434
private Thread updateThread;
3535

36+
private final boolean autoUpdate;
37+
3638
FileDataSourceImpl(
3739
DataSourceUpdateSink dataSourceUpdates,
3840
List<SourceInfo> sources,
@@ -42,8 +44,8 @@ final class FileDataSourceImpl implements DataSource {
4244
) {
4345
this.dataSourceUpdates = dataSourceUpdates;
4446
this.logger = logger;
45-
// The FDv1
4647
this.synchronizer = new FileSynchronizer(sources, autoUpdate, duplicateKeysHandling, logger, true);
48+
this.autoUpdate = autoUpdate;
4749
}
4850

4951
@Override
@@ -62,14 +64,13 @@ public Future<Void> start() {
6264

6365
processResult(initialResult);
6466

65-
// Note that if the initial load finds any errors, it will not set our status to "initialized".
66-
// But we will still do all the other startup steps, because we still might end up getting
67-
// valid data if we are told to reload by the file watcher.
68-
69-
// Start a background thread to listen for file changes
70-
updateThread = new Thread(this::runUpdateLoop, FileDataSourceImpl.class.getName());
71-
updateThread.setDaemon(true);
72-
updateThread.start();
67+
// We only need to drive the update loop if auto-updating is enabled.
68+
if(autoUpdate) {
69+
// Start a background thread to listen for file changes
70+
updateThread = new Thread(this::runUpdateLoop, FileDataSourceImpl.class.getName());
71+
updateThread.setDaemon(true);
72+
updateThread.start();
73+
}
7374

7475
return initFuture;
7576
}

lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/integrations/FileInitializer.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.launchdarkly.sdk.server.datasources.Initializer;
66
import com.launchdarkly.sdk.server.integrations.FileDataSourceBuilder.SourceInfo;
77

8+
import java.io.IOException;
89
import java.util.List;
910
import java.util.concurrent.CompletableFuture;
1011

@@ -14,28 +15,40 @@
1415
* This implements the {@link Initializer} interface, loading files once and returning
1516
* the result. If loading fails, it returns a terminal error since an initializer
1617
* cannot retry.
18+
* <p>
19+
* Internally delegates to {@link FileSynchronizer} with auto-update disabled.
1720
*/
18-
final class FileInitializer extends FileDataSourceBase implements Initializer {
19-
private final CompletableFuture<FDv2SourceResult> shutdownFuture = new CompletableFuture<>();
21+
final class FileInitializer implements Initializer {
22+
private final FileSynchronizer synchronizer;
2023

2124
FileInitializer(
2225
List<SourceInfo> sources,
2326
FileData.DuplicateKeysHandling duplicateKeysHandling,
2427
LDLogger logger,
2528
boolean persist
2629
) {
27-
super(sources, duplicateKeysHandling, logger, persist);
30+
// Use FileSynchronizer with autoUpdate=false for the actual file loading
31+
this.synchronizer = new FileSynchronizer(sources, false, duplicateKeysHandling, logger, persist);
2832
}
2933

3034
@Override
3135
public CompletableFuture<FDv2SourceResult> run() {
32-
CompletableFuture<FDv2SourceResult> loadResult = CompletableFuture.supplyAsync(() -> loadData(true));
33-
return CompletableFuture.anyOf(shutdownFuture, loadResult)
34-
.thenApply(result -> (FDv2SourceResult) result);
36+
return synchronizer.next().thenApply(result -> {
37+
// Convert INTERRUPTED to TERMINAL_ERROR for initializer semantics
38+
// (initializers can't retry, so all errors are terminal)
39+
if (result.getResultType() == FDv2SourceResult.ResultType.STATUS &&
40+
result.getStatus().getState() == FDv2SourceResult.State.INTERRUPTED) {
41+
return FDv2SourceResult.terminalError(
42+
result.getStatus().getErrorInfo(),
43+
result.isFdv1Fallback()
44+
);
45+
}
46+
return result;
47+
});
3548
}
3649

3750
@Override
38-
public void close() {
39-
shutdownFuture.complete(FDv2SourceResult.shutdown());
51+
public void close() throws IOException {
52+
synchronizer.close();
4053
}
4154
}

0 commit comments

Comments
 (0)