model: Chain completions to avoid race conditions
Otherwise getConfigAsync().thenCompose(x -> setState()) would be unsafe. This reverts commit a6595a273afd50524cc66765c6bfbdcc34cb12e4. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
daacc06a0d
commit
933a685585
@ -91,21 +91,24 @@ public class Tunnel extends BaseObservable implements Keyed<String> {
|
||||
return CompletableFuture.completedFuture(statistics);
|
||||
}
|
||||
|
||||
void onConfigChanged(final Config config) {
|
||||
Config onConfigChanged(final Config config) {
|
||||
this.config = config;
|
||||
notifyPropertyChanged(BR.config);
|
||||
return config;
|
||||
}
|
||||
|
||||
void onStateChanged(final State state) {
|
||||
State onStateChanged(final State state) {
|
||||
if (state != State.UP)
|
||||
onStatisticsChanged(null);
|
||||
this.state = state;
|
||||
notifyPropertyChanged(BR.state);
|
||||
return state;
|
||||
}
|
||||
|
||||
void onStatisticsChanged(final Statistics statistics) {
|
||||
Statistics onStatisticsChanged(final Statistics statistics) {
|
||||
this.statistics = statistics;
|
||||
notifyPropertyChanged(BR.statistics);
|
||||
return statistics;
|
||||
}
|
||||
|
||||
public CompletionStage<Tunnel> rename(@NonNull final String name) {
|
||||
|
@ -110,24 +110,18 @@ public final class TunnelManager extends BaseObservable {
|
||||
}
|
||||
|
||||
CompletionStage<Config> getTunnelConfig(final Tunnel tunnel) {
|
||||
final CompletionStage<Config> completion =
|
||||
asyncWorker.supplyAsync(() -> configStore.load(tunnel.getName()));
|
||||
completion.thenAccept(tunnel::onConfigChanged);
|
||||
return completion;
|
||||
return asyncWorker.supplyAsync(() -> configStore.load(tunnel.getName()))
|
||||
.thenApply(tunnel::onConfigChanged);
|
||||
}
|
||||
|
||||
CompletionStage<State> getTunnelState(final Tunnel tunnel) {
|
||||
final CompletionStage<State> completion =
|
||||
asyncWorker.supplyAsync(() -> backend.getState(tunnel));
|
||||
completion.thenAccept(tunnel::onStateChanged);
|
||||
return completion;
|
||||
return asyncWorker.supplyAsync(() -> backend.getState(tunnel))
|
||||
.thenApply(tunnel::onStateChanged);
|
||||
}
|
||||
|
||||
CompletionStage<Statistics> getTunnelStatistics(final Tunnel tunnel) {
|
||||
final CompletionStage<Statistics> completion =
|
||||
asyncWorker.supplyAsync(() -> backend.getStatistics(tunnel));
|
||||
completion.thenAccept(tunnel::onStatisticsChanged);
|
||||
return completion;
|
||||
return asyncWorker.supplyAsync(() -> backend.getStatistics(tunnel))
|
||||
.thenApply(tunnel::onStatisticsChanged);
|
||||
}
|
||||
|
||||
public ObservableKeyedList<String, Tunnel> getTunnels() {
|
||||
@ -227,23 +221,21 @@ public final class TunnelManager extends BaseObservable {
|
||||
}
|
||||
|
||||
CompletionStage<Config> setTunnelConfig(final Tunnel tunnel, final Config config) {
|
||||
final CompletionStage<Config> completion = asyncWorker.supplyAsync(() -> {
|
||||
return asyncWorker.supplyAsync(() -> {
|
||||
final Config appliedConfig = backend.applyConfig(tunnel, config);
|
||||
return configStore.save(tunnel.getName(), appliedConfig);
|
||||
});
|
||||
completion.thenAccept(tunnel::onConfigChanged);
|
||||
return completion;
|
||||
}).thenApply(tunnel::onConfigChanged);
|
||||
}
|
||||
|
||||
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
|
||||
final CompletionStage<State> completion =
|
||||
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state));
|
||||
completion.whenComplete((newState, e) -> {
|
||||
// Ensure the configuration is loaded before trying to use it.
|
||||
return tunnel.getConfigAsync().thenCompose(x ->
|
||||
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state))
|
||||
).whenComplete((newState, e) -> {
|
||||
// Ensure onStateChanged is always called (failure or not), and with the correct state.
|
||||
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
|
||||
if (e == null && newState == State.UP)
|
||||
setLastUsedTunnel(tunnel);
|
||||
});
|
||||
return completion;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user