From bd5c4093714e53738c9c0bfec8d2b0179ae7fbb4 Mon Sep 17 00:00:00 2001 From: "Joshua C. Colp" Date: Fri, 17 Oct 2025 08:57:42 -0300 Subject: [PATCH] devicestate: Don't publish redundant device state messages. When publishing device state check the local cache for the existing device state. If the new device state is unchanged from the prior one, don't bother publishing the update. This can reduce the work done by consumers of device state, such as hints and app_queue, by not publishing a message to them. These messages would most often occur with devices that are seeing numerous simultaneous channels. The underlying device state would remain as in use throughout, but an update would be published as channels are created and hung up. --- main/devicestate.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/main/devicestate.c b/main/devicestate.c index ea7194cc9a..14fcef3087 100644 --- a/main/devicestate.c +++ b/main/devicestate.c @@ -309,14 +309,14 @@ enum ast_device_state ast_parse_device_state(const char *device) return res; } -static enum ast_device_state devstate_cached(const char *device) +static enum ast_device_state devstate_cached(const char *device, const struct ast_eid *eid) { struct stasis_message *cached_msg; struct ast_device_state_message *device_state; enum ast_device_state state; cached_msg = stasis_cache_get_by_eid(ast_device_state_cache(), - ast_device_state_message_type(), device, NULL); + ast_device_state_message_type(), device, eid); if (!cached_msg) { return AST_DEVICE_UNKNOWN; } @@ -338,7 +338,7 @@ static enum ast_device_state _ast_device_state(const char *device, int check_cac /* If the last known state is cached, just return that */ if (check_cache) { - res = devstate_cached(device); + res = devstate_cached(device, NULL); if (res != AST_DEVICE_UNKNOWN) { return res; } @@ -725,6 +725,16 @@ int ast_publish_device_state_full( return -1; } + if (cachable) { + enum ast_device_state res; + + /* If the state is unchanged, do not publish */ + res = devstate_cached(device, eid); + if (res == state) { + return 0; + } + } + device_state = device_state_alloc(device, state, cachable, eid); if (!device_state) { return -1; -- 2.47.3