LCOV - code coverage report
Current view: top level - model - WaitManager.hpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 3 3
Test Date: 2026-04-17 17:21:26 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #pragma once
      37              : 
      38              : #include <string>
      39              : #include <mutex>
      40              : #include <condition_variable>
      41              : #include <cstddef>
      42              : 
      43              : #include <nlohmann/json.hpp>
      44              : 
      45              : namespace h2agent
      46              : {
      47              : namespace model
      48              : {
      49              : 
      50              : class Vault;
      51              : 
      52              : /**
      53              :  * Manages blocking wait (long-poll) for vault entry changes.
      54              :  *
      55              :  * Provides condition-variable-based synchronization so that external
      56              :  * orchestrators can block until a vault entry changes, eliminating
      57              :  * polling loops.
      58              :  */
      59              : class WaitManager
      60              : {
      61              :     std::mutex mutex_;
      62              :     std::condition_variable cv_;
      63              :     size_t active_waiters_{};
      64              :     bool shutdown_{};
      65              : 
      66              :     Vault *vault_{};
      67              : 
      68              : public:
      69              :     static constexpr size_t MAX_WAITERS = 32;
      70              :     static constexpr unsigned int MAX_TIMEOUT_MS = 300000; // 5 minutes
      71              : 
      72            5 :     WaitManager() = default;
      73            5 :     ~WaitManager() = default;
      74              : 
      75            5 :     void setVault(Vault *p) { vault_ = p; }
      76              : 
      77              :     /** Signals all waiters to abort and prevents new waits. */
      78              :     void shutdown();
      79              : 
      80              :     /**
      81              :      * Blocks until a vault entry changes (any change or specific value).
      82              :      *
      83              :      * @param key Variable key to watch.
      84              :      * @param targetValue If non-empty, wait until variable equals this value.
      85              :      *                    If empty, wait for any change from current value.
      86              :      * @param timeoutMs Maximum wait time in milliseconds (capped at MAX_TIMEOUT_MS).
      87              :      * @param resultValue Filled with the variable value on return.
      88              :      * @param previousValue Filled with the value captured before waiting.
      89              :      *
      90              :      * @return true if condition met, false on timeout.
      91              :      *         Returns false if too many waiters (caller should use 429).
      92              :      */
      93              :     bool waitForVault(const std::string &key, const nlohmann::json &targetValue,
      94              :                                unsigned int timeoutMs,
      95              :                                nlohmann::json &resultValue, nlohmann::json &previousValue);
      96              : 
      97              :     /** Wakes all waiters (called after vault entry mutation). */
      98              :     void notify();
      99              : 
     100              :     /** Returns current number of active waiters. */
     101              :     size_t activeWaiters() const;
     102              : 
     103              :     /** Returns true if waiter limit reached. */
     104              :     bool isFull() const;
     105              : };
     106              : 
     107              : }
     108              : }
        

Generated by: LCOV version 2.0-1