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
115481
  explicit constexpr ScaledMinimum(UnitFloat scale_factor) : scale_factor_(scale_factor) {}
19
12
  inline bool operator==(const ScaledMinimum& other) const {
20
12
    return other.scale_factor_.value() == scale_factor_.value();
21
12
  }
22
  const UnitFloat scale_factor_;
23
};
24

            
25
/**
26
 * Describes a minimum timer value that is an absolute duration.
27
 */
28
struct AbsoluteMinimum {
29
99
  explicit constexpr AbsoluteMinimum(std::chrono::milliseconds value) : value_(value) {}
30
2
  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
99
  constexpr ScaledTimerMinimum(AbsoluteMinimum arg) : impl_(arg) {}
42
115481
  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
607281
  std::chrono::milliseconds computeMinimum(std::chrono::milliseconds maximum) const {
50
607281
    struct Visitor {
51
607281
      explicit Visitor(std::chrono::milliseconds value) : value_(value) {}
52
607281
      std::chrono::milliseconds operator()(ScaledMinimum scale_factor) {
53
607102
        return std::chrono::duration_cast<std::chrono::milliseconds>(
54
607102
            scale_factor.scale_factor_.value() * value_);
55
607102
      }
56
607281
      std::chrono::milliseconds operator()(AbsoluteMinimum absolute_value) {
57
179
        return absolute_value.value_;
58
179
      }
59
607281
      const std::chrono::milliseconds value_;
60
607281
    };
61
607281
    return absl::visit(Visitor(maximum), impl_);
62
607281
  }
63

            
64
14
  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
  // The max time an HTTP connection to a downstream client can be connected at all. This
84
  // corresponds to the HTTP_DOWNSTREAM_CONNECTION_MAX TimerType in overload.proto.
85
  HttpDownstreamMaxConnectionTimeout,
86
  // The max time the downstream codec will wait to flush an ended response stream. This corresponds
87
  // to HTTP_DOWNSTREAM_STREAM_FLUSH TimerType in overload.proto.
88
  HttpDownstreamStreamFlush,
89
};
90

            
91
using ScaledTimerTypeMap = absl::flat_hash_map<ScaledTimerType, ScaledTimerMinimum>;
92
using ScaledTimerTypeMapConstSharedPtr = std::shared_ptr<const ScaledTimerTypeMap>;
93

            
94
} // namespace Event
95
} // namespace Envoy