[homekit] improve bundle deactivation time (#13566)

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>
This commit is contained in:
Cody Cutrer 2022-10-19 03:18:02 -06:00 committed by GitHub
parent 77013bca39
commit 91383250d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 17 deletions

View File

@ -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
@ -76,14 +76,14 @@ class Debouncer {
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);
}
@ -111,7 +111,7 @@ class Debouncer {
// 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);
}
}
}

View File

@ -258,11 +258,6 @@ public class HomekitChangeListener implements ItemRegistryChangeListener {
accessoryRegistry.setBridge(bridge);
}
public synchronized void unsetBridge() {
applyUpdatesDebouncer.stop();
accessoryRegistry.unsetBridge();
}
public void setUpdater(HomekitAccessoryUpdater updater) {
this.updater = updater;
}
@ -271,9 +266,11 @@ public class HomekitChangeListener implements ItemRegistryChangeListener {
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() {

View File

@ -257,12 +257,9 @@ public class HomekitImpl implements Homekit, NetworkAddressChangeListener {
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();