1
#pragma once
2

            
3
#include "envoy/common/pure.h"
4
#include "envoy/event/deferred_deletable.h"
5
#include "envoy/upstream/upstream.h"
6

            
7
namespace Envoy {
8
namespace ConnectionPool {
9

            
10
/**
11
 * Controls the behavior of a canceled stream.
12
 */
13
enum class CancelPolicy {
14
  // By default, canceled streams allow a pending connection to complete and become
15
  // available for a future stream.
16
  Default,
17
  // When a stream is canceled, closes a pending connection if there will still be sufficient
18
  // connections to serve pending streams. CloseExcess is largely useful for callers that never
19
  // re-use connections (e.g. by closing rather than releasing connections). Using CloseExcess in
20
  // this situation guarantees that no idle connections will be held open by the conn pool awaiting
21
  // a connection stream.
22
  CloseExcess,
23
};
24

            
25
/**
26
 * Handle that allows a pending connection or stream to be canceled before it is completed.
27
 */
28
class Cancellable {
29
public:
30
32671
  virtual ~Cancellable() = default;
31

            
32
  /**
33
   * Cancel the pending connection or stream.
34
   * @param cancel_policy a CancelPolicy that controls the behavior of this cancellation.
35
   */
36
  virtual void cancel(CancelPolicy cancel_policy) PURE;
37
};
38

            
39
/**
40
 * Controls the behavior when draining a connection pool.
41
 */
42
enum class DrainBehavior {
43
  // Starts draining a pool, by gracefully completing all requests and gracefully closing all
44
  // connections, in preparation for deletion. It is invalid to create new streams or
45
  // connections from this pool after draining a pool with this behavior.
46
  DrainAndDelete,
47
  // Actively drain all existing connection pool connections. This can be used in cases where
48
  // the connection pool is not being destroyed, but the caller wishes to make sure that
49
  // all new streams take place on a new connection. For example, when a health check failure
50
  // occurs.
51
  DrainExistingConnections,
52
  // Same as DrainExistingConnections, but only drains connections unable to migrate to a different
53
  // network on mobile.
54
  DrainExistingNonMigratableConnections,
55
};
56

            
57
/**
58
 * An instance of a generic connection pool.
59
 */
60
class Instance {
61
public:
62
99700
  virtual ~Instance() = default;
63

            
64
  /**
65
   * Called when a connection pool has no pending streams, busy connections, or ready connections.
66
   */
67
  using IdleCb = std::function<void()>;
68

            
69
  /**
70
   * Register a callback that gets called when the connection pool is fully idle.
71
   */
72
  virtual void addIdleCallback(IdleCb cb) PURE;
73

            
74
  /**
75
   * Returns true if the pool does not have any connections or pending requests.
76
   */
77
  virtual bool isIdle() const PURE;
78

            
79
  /**
80
   * Drains the connections in a pool.
81
   * @param drain_behavior A DrainBehavior that controls the behavior of the draining.
82
   */
83
  virtual void drainConnections(DrainBehavior drain_behavior) PURE;
84

            
85
  /**
86
   * @return Upstream::HostDescriptionConstSharedPtr the host for which connections are pooled.
87
   */
88
  virtual Upstream::HostDescriptionConstSharedPtr host() const PURE;
89

            
90
  /**
91
   * Creates an upstream connection, if existing connections do not meet both current and
92
   * anticipated load.
93
   *
94
   * @return true if a connection was preconnected, false otherwise.
95
   */
96
  virtual bool maybePreconnect(float preconnect_ratio) PURE;
97
};
98

            
99
enum class PoolFailureReason {
100
  // A resource overflowed and policy prevented a new connection from being created.
101
  Overflow,
102
  // A local connection failure took place while creating a new connection.
103
  LocalConnectionFailure,
104
  // A remote connection failure took place while creating a new connection.
105
  RemoteConnectionFailure,
106
  // A timeout occurred while creating a new connection.
107
  Timeout,
108
};
109

            
110
} // namespace ConnectionPool
111
} // namespace Envoy