2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SRC_CLIENTINFO_H
10 #define SQUID_SRC_CLIENTINFO_H
13 #include "BandwidthBucket.h"
15 #include "base/ByteCounter.h"
19 #include "ip/Address.h"
21 #include "mem/forward.h"
30 class ClientInfo
: public hash_link
32 , public BandwidthBucket
35 MEMPROXY_CLASS(ClientInfo
);
38 explicit ClientInfo(const Ip::Address
&);
40 ~ClientInfo() override
;
48 Protocol() : n_requests(0) {
49 memset(result_hist
, 0, sizeof(result_hist
));
52 int result_hist
[LOG_TYPE_MAX
];
54 ByteCounter kbytes_in
;
55 ByteCounter kbytes_out
;
56 ByteCounter hit_kbytes_out
;
60 Cutoff() : time(0), n_req(0), n_denied(0) {}
66 int n_established
; /* number of current established connections */
69 bool writeLimitingActive
; ///< Is write limiter active
70 bool firstTimeConnection
;///< is this first time connection for this client
72 CommQuotaQueue
*quotaQueue
; ///< clients waiting for more write quota
73 int rationedQuota
; ///< precomputed quota preserving fairness among clients
74 int rationedCount
; ///< number of clients that will receive rationedQuota
75 bool eventWaiting
; ///< waiting for commHandleWriteHelper event to fire
77 // all those functions access Comm fd_table and are defined in comm.cc
78 bool hasQueue() const; ///< whether any clients are waiting for write quota
79 bool hasQueue(const CommQuotaQueue
*) const; ///< has a given queue
80 unsigned int quotaEnqueue(int fd
); ///< client starts waiting in queue; create the queue if necessary
81 int quotaPeekFd() const; ///< returns the next fd reservation
82 unsigned int quotaPeekReserv() const; ///< returns the next reserv. to pop
83 void quotaDequeue(); ///< pops queue head from queue
84 void kickQuotaQueue(); ///< schedule commHandleWriteHelper call
85 /// either selects the head descriptor for writing or calls quotaDequeue()
86 void writeOrDequeue();
88 /* BandwidthBucket API */
89 int quota() override
; ///< allocate quota for a just dequeued client
90 bool applyQuota(int &nleft
, Comm::IoCallback
*state
) override
;
91 void scheduleWrite(Comm::IoCallback
*state
) override
;
92 void onFdClosed() override
;
93 void reduceBucket(int len
) override
;
95 void quotaDumpQueue(); ///< dumps quota queue for debugging
98 * Configure client write limiting (note:"client" here means - IP). It is called
99 * by httpAccept in client_side.cc, where the initial bucket size (anInitialBurst)
100 * computed, using the configured maximum bucket value and configured initial
101 * bucket value(50% by default).
103 * \param writeSpeedLimit is speed limit configured in config for this pool
104 * \param initialBurst is initial bucket size to use for this client(i.e. client can burst at first)
105 * \param highWatermark is maximum bucket value
107 void setWriteLimiter(const int aWriteSpeedLimit
, const double anInitialBurst
, const double aHighWatermark
);
108 #endif /* USE_DELAY_POOLS */
112 // a queue of Comm clients waiting for I/O quota controlled by delay pools
115 CBDATA_CLASS(CommQuotaQueue
);
118 CommQuotaQueue(ClientInfo
*info
);
121 bool empty() const { return fds
.empty(); }
122 size_t size() const { return fds
.size(); }
123 int front() const { return fds
.front(); }
124 unsigned int enqueue(int fd
);
127 ClientInfo
*clientInfo
; ///< bucket responsible for quota maintenance
129 // these counters might overflow; that is OK because they are for IDs only
130 int ins
; ///< number of enqueue calls, used to generate a "reservation" ID
131 int outs
; ///< number of dequeue calls, used to check the "reservation" ID
134 // TODO: optimize using a Ring- or List-based store?
135 typedef std::deque
<int> Store
;
136 Store fds
; ///< descriptor queue
138 #endif /* USE_DELAY_POOLS */
140 #endif /* SQUID_SRC_CLIENTINFO_H */