1
#pragma once
2

            
3
#include "envoy/http/async_client.h"
4

            
5
namespace Envoy {
6
namespace Http {
7

            
8
/**
9
 * Keeps track of active async HTTP requests to be able to cancel them on destruction.
10
 */
11
class AsyncClientRequestTracker {
12
public:
13
  /**
14
   * Cancels all known active async HTTP requests.
15
   */
16
  ~AsyncClientRequestTracker();
17
  /**
18
   * Includes a given async HTTP request into a set of known active requests.
19
   * @param request request handle
20
   */
21
  void add(AsyncClient::Request& request);
22
  /**
23
   * Excludes a given async HTTP request from a set of known active requests.
24
   *
25
   * NOTE: Asymmetry between signatures of add() and remove() is caused by the difference
26
   *       between contexts in which these methods will be used.
27
   *       add() will be called right after AsyncClient::send() when request.cancel() is
28
   *       perfectly valid and desirable.
29
   *       However, remove() will be called in the context of
30
   *       AsyncClient::Callbacks::[onSuccess | onFailure] where request.cancel() is no longer
31
   *       expected and therefore get prevented by means of "const" modifier.
32
   *
33
   * @param request request handle
34
   */
35
  void remove(const AsyncClient::Request& request);
36

            
37
private:
38
  // Track active async HTTP requests to be able to cancel them on destruction.
39
  absl::flat_hash_set<AsyncClient::Request*> active_requests_;
40
};
41

            
42
} // namespace Http
43
} // namespace Envoy