]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ClientInfo.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / ClientInfo.h
1 /*
2 * Copyright (C) 1996-2017 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 #include "base/ByteCounter.h"
13 #include "cbdata.h"
14 #include "enums.h"
15 #include "hash.h"
16 #include "ip/Address.h"
17 #include "LogTags.h"
18 #include "mem/forward.h"
19 #include "typedefs.h"
20
21 #include <deque>
22
23 #if USE_DELAY_POOLS
24 class CommQuotaQueue;
25 #endif
26
27 class ClientInfo
28 {
29 MEMPROXY_CLASS(ClientInfo);
30
31 public:
32 explicit ClientInfo(const Ip::Address &);
33 ~ClientInfo();
34
35 hash_link hash; /* must be first */
36
37 Ip::Address addr;
38
39 struct Protocol {
40 Protocol() : n_requests(0) {
41 memset(result_hist, 0, sizeof(result_hist));
42 }
43
44 int result_hist[LOG_TYPE_MAX];
45 int n_requests;
46 ByteCounter kbytes_in;
47 ByteCounter kbytes_out;
48 ByteCounter hit_kbytes_out;
49 } Http, Icp;
50
51 struct Cutoff {
52 Cutoff() : time(0), n_req(0), n_denied(0) {}
53
54 time_t time;
55 int n_req;
56 int n_denied;
57 } cutoff;
58 int n_established; /* number of current established connections */
59 time_t last_seen;
60 #if USE_DELAY_POOLS
61 double writeSpeedLimit;///< Write speed limit in bytes per second, can be less than 1, if too close to zero this could result in timeouts from client
62 double prevTime; ///< previous time when we checked
63 double bucketSize; ///< how much can be written now
64 double bucketSizeLimit; ///< maximum bucket size
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 selectWaiting; ///< is between commSetSelect and commHandleWrite
72 bool eventWaiting; ///< waiting for commHandleWriteHelper event to fire
73
74 // all those functions access Comm fd_table and are defined in comm.cc
75 bool hasQueue() const; ///< whether any clients are waiting for write quota
76 bool hasQueue(const CommQuotaQueue*) const; ///< has a given queue
77 unsigned int quotaEnqueue(int fd); ///< client starts waiting in queue; create the queue if necessary
78 int quotaPeekFd() const; ///< retuns the next fd reservation
79 unsigned int quotaPeekReserv() const; ///< returns the next reserv. to pop
80 void quotaDequeue(); ///< pops queue head from queue
81 void kickQuotaQueue(); ///< schedule commHandleWriteHelper call
82 int quotaForDequed(); ///< allocate quota for a just dequeued client
83 void refillBucket(); ///< adds bytes to bucket based on rate and time
84
85 void quotaDumpQueue(); ///< dumps quota queue for debugging
86
87 /**
88 * Configure client write limiting (note:"client" here means - IP). It is called
89 * by httpAccept in client_side.cc, where the initial bucket size (anInitialBurst)
90 * computed, using the configured maximum bucket vavlue and configured initial
91 * bucket value(50% by default).
92 *
93 * \param writeSpeedLimit is speed limit configured in config for this pool
94 * \param initialBurst is initial bucket size to use for this client(i.e. client can burst at first)
95 * \param highWatermark is maximum bucket value
96 */
97 void setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark);
98 #endif /* USE_DELAY_POOLS */
99 };
100
101 #if USE_DELAY_POOLS
102 // a queue of Comm clients waiting for I/O quota controlled by delay pools
103 class CommQuotaQueue
104 {
105 CBDATA_CLASS(CommQuotaQueue);
106
107 public:
108 CommQuotaQueue(ClientInfo *info);
109 ~CommQuotaQueue();
110
111 bool empty() const { return fds.empty(); }
112 size_t size() const { return fds.size(); }
113 int front() const { return fds.front(); }
114 unsigned int enqueue(int fd);
115 void dequeue();
116
117 ClientInfo *clientInfo; ///< bucket responsible for quota maintenance
118
119 // these counters might overflow; that is OK because they are for IDs only
120 int ins; ///< number of enqueue calls, used to generate a "reservation" ID
121 int outs; ///< number of dequeue calls, used to check the "reservation" ID
122
123 private:
124 // TODO: optimize using a Ring- or List-based store?
125 typedef std::deque<int> Store;
126 Store fds; ///< descriptor queue
127 };
128 #endif /* USE_DELAY_POOLS */
129
130 #endif
131