]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ClientInfo.h
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / ClientInfo.h
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID__SRC_CLIENTINFO_H
10 #define SQUID__SRC_CLIENTINFO_H
11
12 #if USE_DELAY_POOLS
13 #include "BandwidthBucket.h"
14 #endif
15 #include "base/ByteCounter.h"
16 #include "cbdata.h"
17 #include "enums.h"
18 #include "hash.h"
19 #include "ip/Address.h"
20 #include "LogTags.h"
21 #include "mem/forward.h"
22 #include "typedefs.h"
23
24 #include <deque>
25
26 #if USE_DELAY_POOLS
27 class CommQuotaQueue;
28 #endif
29
30 class ClientInfo : public hash_link
31 #if USE_DELAY_POOLS
32 , public BandwidthBucket
33 #endif
34 {
35 MEMPROXY_CLASS(ClientInfo);
36
37 public:
38 explicit ClientInfo(const Ip::Address &);
39 ~ClientInfo();
40
41 Ip::Address addr;
42
43 struct Protocol {
44 Protocol() : n_requests(0) {
45 memset(result_hist, 0, sizeof(result_hist));
46 }
47
48 int result_hist[LOG_TYPE_MAX];
49 int n_requests;
50 ByteCounter kbytes_in;
51 ByteCounter kbytes_out;
52 ByteCounter hit_kbytes_out;
53 } Http, Icp;
54
55 struct Cutoff {
56 Cutoff() : time(0), n_req(0), n_denied(0) {}
57
58 time_t time;
59 int n_req;
60 int n_denied;
61 } cutoff;
62 int n_established; /* number of current established connections */
63 time_t last_seen;
64 #if USE_DELAY_POOLS
65 bool writeLimitingActive; ///< Is write limiter active
66 bool firstTimeConnection;///< is this first time connection for this client
67
68 CommQuotaQueue *quotaQueue; ///< clients waiting for more write quota
69 int rationedQuota; ///< precomputed quota preserving fairness among clients
70 int rationedCount; ///< number of clients that will receive rationedQuota
71 bool eventWaiting; ///< waiting for commHandleWriteHelper event to fire
72
73 // all those functions access Comm fd_table and are defined in comm.cc
74 bool hasQueue() const; ///< whether any clients are waiting for write quota
75 bool hasQueue(const CommQuotaQueue*) const; ///< has a given queue
76 unsigned int quotaEnqueue(int fd); ///< client starts waiting in queue; create the queue if necessary
77 int quotaPeekFd() const; ///< retuns the next fd reservation
78 unsigned int quotaPeekReserv() const; ///< returns the next reserv. to pop
79 void quotaDequeue(); ///< pops queue head from queue
80 void kickQuotaQueue(); ///< schedule commHandleWriteHelper call
81
82 /* BandwidthBucket API */
83 virtual int quota() override; ///< allocate quota for a just dequeued client
84 virtual bool applyQuota(int &nleft, Comm::IoCallback *state) override;
85 virtual void scheduleWrite(Comm::IoCallback *state) override;
86 virtual void onFdClosed() override;
87 virtual void reduceBucket(int len) override;
88
89 void quotaDumpQueue(); ///< dumps quota queue for debugging
90
91 /**
92 * Configure client write limiting (note:"client" here means - IP). It is called
93 * by httpAccept in client_side.cc, where the initial bucket size (anInitialBurst)
94 * computed, using the configured maximum bucket vavlue and configured initial
95 * bucket value(50% by default).
96 *
97 * \param writeSpeedLimit is speed limit configured in config for this pool
98 * \param initialBurst is initial bucket size to use for this client(i.e. client can burst at first)
99 * \param highWatermark is maximum bucket value
100 */
101 void setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark);
102 #endif /* USE_DELAY_POOLS */
103 };
104
105 #if USE_DELAY_POOLS
106 // a queue of Comm clients waiting for I/O quota controlled by delay pools
107 class CommQuotaQueue
108 {
109 CBDATA_CLASS(CommQuotaQueue);
110
111 public:
112 CommQuotaQueue(ClientInfo *info);
113 ~CommQuotaQueue();
114
115 bool empty() const { return fds.empty(); }
116 size_t size() const { return fds.size(); }
117 int front() const { return fds.front(); }
118 unsigned int enqueue(int fd);
119 void dequeue();
120
121 ClientInfo *clientInfo; ///< bucket responsible for quota maintenance
122
123 // these counters might overflow; that is OK because they are for IDs only
124 int ins; ///< number of enqueue calls, used to generate a "reservation" ID
125 int outs; ///< number of dequeue calls, used to check the "reservation" ID
126
127 private:
128 // TODO: optimize using a Ring- or List-based store?
129 typedef std::deque<int> Store;
130 Store fds; ///< descriptor queue
131 };
132 #endif /* USE_DELAY_POOLS */
133
134 #endif
135