Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i/p/requestprompts: add timeout for prompts (#14390)
* i/p/requestprompts: add timeout for prompts When there has been no retrieval of prompt details for a given user after a particular duration, expire all outstanding prompts for that user, as this suggests that there is no prompt client running for that user. If a user retrieves prompt details or interacts with prompts in some way, such as by retrieving all prompts, a prompt by ID, or attempting to reply to a prompt, bump the timeout to a much longer timeout, since this indicates that a prompt client is running for that user. Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: add helper to send reply for prompt Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: do not expire prompts on access race Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: avoid data races around close and expiration When `Close` is called, explicitly stop the expiration timers, and when the expiration timer fires and acquires the DB lock, return early if the DB has already been closed. Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: leave prompts unchanged if timeout race happens Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: use testutil.Mock instead of testutil.Backup Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: reorder user entry creation and map insertion Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: move timeout callback into dedicated method Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * daemon,i/p/requestprompts,o/i/apparmorprompting: explicitly indicate client activity Callers of the requestprompts methods can specify whether they indicate that a prompting client is present and active, and thus the prompt expiration timeout should be reset. Previously, any call to these methods was implicitly treated as activity. Now, these functions have the ability to be called internally or while testing or debugging in a way which does not cause the timeout to be reset. Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * daemon,i/p/requestprompts,o/i/apparmorprompting: add client activity fields for adding/patching rules Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * daemon: add prompting client activity helper and improve comments Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: make internal errors noticef instead of debugf Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: add helper method to reset expiration timer Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * many: remove clientActivity plumbing for rule-related functions Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: unlock prompt db on early return from timeout callback Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: use testtime timer interface and test timer Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: fix typo and simplify mockable function Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: simplify reset helper and clarify comment Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: remove racy timer stop during close and clarify behavior The `Close` method calling `Stop` on the expiration timers races with those timers firing, and it's unnecessary since the the callback function checks whether the DB has been closed and returns early if it has. Thus, remove the explicit `Stop` and add some comments about the reasoning behind this. Additionally, add a `isClosed` helper so functions don't have to directly call a method on the contained `maxIDMmap` to check whether the prompt DB has been closed. Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: improve timeout arithmetic in tests Signed-off-by: Oliver Calder <oliver.calder@canonical.com> * i/p/requestprompts: add comment to use clear() once on Go 1.21+ Signed-off-by: Oliver Calder <oliver.calder@canonical.com> --------- Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
- Loading branch information