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:
41  GatewayState(std::string_view prefix, std::shared_ptr<Client>);
42  long score(GatewayRequest const&, unsigned) const;
43  bool bored() const;
44  bool over_rate();
45 
46  void just_sent_one();
47  void hit(GatewayRequestType, GatewayRequest const&);
48  bool miss(GatewayRequestType, GatewayRequest const&);
49  void timed_out();
50  long extra_ms() { return slowness; }
51 };
52 } // namespace ipfs::gw
53 
54 #endif // IPFS_GATEWAY_STATE_H_
Definition: gateway_config.h:13
Definition: gateway_request.h:39
Definition: gateway_state.h:25