LCOV - code coverage report
Current view: top level - envoy/event - scaled_timer.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 14 23 60.9 %
Date: 2024-01-05 06:35:25 Functions: 5 11 45.5 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <chrono>
       4             : #include <ostream>
       5             : 
       6             : #include "source/common/common/interval_value.h"
       7             : 
       8             : #include "absl/container/flat_hash_map.h"
       9             : #include "absl/types/variant.h"
      10             : 
      11             : namespace Envoy {
      12             : namespace Event {
      13             : 
      14             : /**
      15             :  * Describes a minimum timer value that is equal to a scale factor applied to the maximum.
      16             :  */
      17             : struct ScaledMinimum {
      18        1298 :   explicit constexpr ScaledMinimum(UnitFloat scale_factor) : scale_factor_(scale_factor) {}
      19           0 :   inline bool operator==(const ScaledMinimum& other) const {
      20           0 :     return other.scale_factor_.value() == scale_factor_.value();
      21           0 :   }
      22             :   const UnitFloat scale_factor_;
      23             : };
      24             : 
      25             : /**
      26             :  * Describes a minimum timer value that is an absolute duration.
      27             :  */
      28             : struct AbsoluteMinimum {
      29           0 :   explicit constexpr AbsoluteMinimum(std::chrono::milliseconds value) : value_(value) {}
      30           0 :   inline bool operator==(const AbsoluteMinimum& other) const { return other.value_ == value_; }
      31             :   const std::chrono::milliseconds value_;
      32             : };
      33             : 
      34             : /**
      35             :  * Class that describes how to compute a minimum timeout given a maximum timeout value. It wraps
      36             :  * ScaledMinimum and AbsoluteMinimum and provides a single computeMinimum() method.
      37             :  */
      38             : class ScaledTimerMinimum {
      39             : public:
      40             :   // Forward arguments to impl_'s constructor.
      41           0 :   constexpr ScaledTimerMinimum(AbsoluteMinimum arg) : impl_(arg) {}
      42        1298 :   constexpr ScaledTimerMinimum(ScaledMinimum arg) : impl_(arg) {}
      43             : 
      44             :   // Computes the minimum value for a given maximum timeout. If this object was constructed with a
      45             :   // - ScaledMinimum value:
      46             :   //     the return value is the scale factor applied to the provided maximum.
      47             :   // - AbsoluteMinimum:
      48             :   //     the return value is that minimum, and the provided maximum is ignored.
      49        3005 :   std::chrono::milliseconds computeMinimum(std::chrono::milliseconds maximum) const {
      50        3005 :     struct Visitor {
      51        3005 :       explicit Visitor(std::chrono::milliseconds value) : value_(value) {}
      52        3005 :       std::chrono::milliseconds operator()(ScaledMinimum scale_factor) {
      53        3005 :         return std::chrono::duration_cast<std::chrono::milliseconds>(
      54        3005 :             scale_factor.scale_factor_.value() * value_);
      55        3005 :       }
      56        3005 :       std::chrono::milliseconds operator()(AbsoluteMinimum absolute_value) {
      57           0 :         return absolute_value.value_;
      58           0 :       }
      59        3005 :       const std::chrono::milliseconds value_;
      60        3005 :     };
      61        3005 :     return absl::visit(Visitor(maximum), impl_);
      62        3005 :   }
      63             : 
      64           0 :   inline bool operator==(const ScaledTimerMinimum& other) const { return impl_ == other.impl_; }
      65             : 
      66             : private:
      67             :   absl::variant<ScaledMinimum, AbsoluteMinimum> impl_;
      68             : };
      69             : 
      70             : enum class ScaledTimerType {
      71             :   // Timers created with this type will never be scaled. This should only be used for testing.
      72             :   UnscaledRealTimerForTest,
      73             :   // The amount of time an HTTP connection to a downstream client can remain idle (no streams). This
      74             :   // corresponds to the HTTP_DOWNSTREAM_CONNECTION_IDLE TimerType in overload.proto.
      75             :   HttpDownstreamIdleConnectionTimeout,
      76             :   // The amount of time an HTTP stream from a downstream client can remain idle. This corresponds to
      77             :   // the HTTP_DOWNSTREAM_STREAM_IDLE TimerType in overload.proto.
      78             :   HttpDownstreamIdleStreamTimeout,
      79             :   // The amount of time a connection to a downstream client can spend waiting for the transport to
      80             :   // report connection establishment before the connection is closed. This corresponds to the
      81             :   // TRANSPORT_SOCKET_CONNECT_TIMEOUT TimerType in overload.proto.
      82             :   TransportSocketConnectTimeout,
      83             : };
      84             : 
      85             : using ScaledTimerTypeMap = absl::flat_hash_map<ScaledTimerType, ScaledTimerMinimum>;
      86             : using ScaledTimerTypeMapConstSharedPtr = std::shared_ptr<const ScaledTimerTypeMap>;
      87             : 
      88             : } // namespace Event
      89             : } // namespace Envoy

Generated by: LCOV version 1.15