ipfs-chromium
gateway_state.h
1 #ifndef IPFS_GATEWAY_STATE_H_
2 #define IPFS_GATEWAY_STATE_H_
3 
4 #include <ipfs_client/gw/gateway_request_type.h>
5 
6 #include <ctime>
7 
8 #include <array>
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 
13 namespace ipfs {
14 class Client;
15 
16 namespace ctx {
17 class GatewayConfig;
18 }
19 } // namespace ipfs
20 
21 namespace ipfs::gw {
22 class GatewayRequest;
25 class GatewayState {
26  std::string prefix_;
27  std::shared_ptr<Client> api_;
28  std::unordered_map<std::string, long> affinity_success;
29  static constexpr short MinutesTracked = 4;
30  std::array<unsigned, MinutesTracked * 60UL> sent_counts;
31  std::size_t total_sent = 0UL;
32  std::time_t last_hist_update;
33  unsigned& current_bucket();
34  long slowness = 0;
35  bool over_rate(unsigned req_per_min);
36 
37  ctx::GatewayConfig& cfg();
38  ctx::GatewayConfig const& cfg() const;
39 
40  public:
45  GatewayState(std::string_view prefix, std::shared_ptr<Client> api);
46  long score(GatewayRequest const&, unsigned) const;
47  bool bored() const;
48  bool over_rate();
49 
50  void just_sent_one();
51  void hit(GatewayRequestType, GatewayRequest const&);
52  bool miss(GatewayRequestType, GatewayRequest const&);
53  void timed_out();
54  long extra_ms() { return slowness; }
55 };
56 } // namespace ipfs::gw
57 
58 #endif // IPFS_GATEWAY_STATE_H_
Definition: gateway_config.h:13
Definition: gateway_request.h:39
Definition: gateway_state.h:25
GatewayState(std::string_view prefix, std::shared_ptr< Client > api)