Skip to content

Commit

Permalink
[homekit] improve bundle deactivation time (openhab#13566)
Browse files Browse the repository at this point in the history
If you have many instances, it can take a while. So stop all the
instances in parallel. Also, fix a race condition where the update
debouncer might get called again after being stopped, because the
change listener was deregistered _after_ the debouncer was stopped.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
ccutrer authored and andrasU committed Nov 12, 2022
1 parent b90f128 commit fa86a1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Debouncer {
private final Logger logger = LoggerFactory.getLogger(Debouncer.class);

private volatile Long lastCallAttempt;
private ScheduledFuture<?> feature;
private ScheduledFuture<?> future;

/**
* Highly performant generic debouncer
Expand Down Expand Up @@ -76,14 +76,14 @@ void call() {
lastCallAttempt = clock.millis();
calls.incrementAndGet();
if (pending.compareAndSet(false, true)) {
feature = scheduler.schedule(this::tryActionOrPostpone, delayMs, TimeUnit.MILLISECONDS);
future = scheduler.schedule(this::tryActionOrPostpone, delayMs, TimeUnit.MILLISECONDS);
}
}

public void stop() {
logger.trace("stop debouncer");
if (feature != null) {
feature.cancel(true);
if (future != null) {
future.cancel(true);
calls.set(0);
pending.set(false);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ private void tryActionOrPostpone() {
// Note: we use Math.max as there's a _very_ small chance lastCallAttempt could advance in another thread,
// and result in a negative calculation
long delay = Math.max(1, lastCallAttempt - now + delayMs);
feature = scheduler.schedule(this::tryActionOrPostpone, delay, TimeUnit.MILLISECONDS);
future = scheduler.schedule(this::tryActionOrPostpone, delay, TimeUnit.MILLISECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ public synchronized void setBridge(HomekitRoot bridge) {
accessoryRegistry.setBridge(bridge);
}

public synchronized void unsetBridge() {
applyUpdatesDebouncer.stop();
accessoryRegistry.unsetBridge();
}

public void setUpdater(HomekitAccessoryUpdater updater) {
this.updater = updater;
}
Expand All @@ -271,9 +266,11 @@ public void updateSettings(HomekitSettings settings) {
this.settings = settings;
}

public void stop() {
public synchronized void stop() {
this.itemRegistry.removeRegistryChangeListener(this);
this.metadataRegistry.removeRegistryChangeListener(metadataChangeListener);
applyUpdatesDebouncer.stop();
accessoryRegistry.unsetBridge();
}

public Map<String, HomekitAccessory> getAccessories() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,9 @@ private void startHomekitServer() throws IOException, InvalidAlgorithmParameterE

private void stopHomekitServer() {
logger.trace("stop HomeKit bridge");
for (int i = 0; i < homekitServers.size(); ++i) {
changeListeners.get(i).unsetBridge();
bridges.get(i).stop();
homekitServers.get(i).stop();
changeListeners.get(i).stop();
}
changeListeners.parallelStream().forEach(HomekitChangeListener::stop);
bridges.parallelStream().forEach(HomekitRoot::stop);
homekitServers.parallelStream().forEach(HomekitServer::stop);
homekitServers.clear();
bridges.clear();
changeListeners.clear();
Expand Down

0 comments on commit fa86a1b

Please sign in to comment.