Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/envoy/event/scaled_timer.h
Line
Count
Source (jump to first uncovered line)
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
5.46k
  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
5.46k
  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
16.2k
  std::chrono::milliseconds computeMinimum(std::chrono::milliseconds maximum) const {
50
16.2k
    struct Visitor {
51
16.2k
      explicit Visitor(std::chrono::milliseconds value) : value_(value) {}
52
16.2k
      std::chrono::milliseconds operator()(ScaledMinimum scale_factor) {
53
16.2k
        return std::chrono::duration_cast<std::chrono::milliseconds>(
54
16.2k
            scale_factor.scale_factor_.value() * value_);
55
16.2k
      }
56
16.2k
      std::chrono::milliseconds operator()(AbsoluteMinimum absolute_value) {
57
0
        return absolute_value.value_;
58
0
      }
59
16.2k
      const std::chrono::milliseconds value_;
60
16.2k
    };
61
16.2k
    return absl::visit(Visitor(maximum), impl_);
62
16.2k
  }
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