2 * Copyright (C) 1996-2018 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
&);
44 Protocol() : n_requests(0) {
45 memset(result_hist
, 0, sizeof(result_hist
));
48 int result_hist
[LOG_TYPE_MAX
];
50 ByteCounter kbytes_in
;
51 ByteCounter kbytes_out
;
52 ByteCounter hit_kbytes_out
;
56 Cutoff() : time(0), n_req(0), n_denied(0) {}
62 int n_established
; /* number of current established connections */
65 bool writeLimitingActive
; ///< Is write limiter active
66 bool firstTimeConnection
;///< is this first time connection for this client
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
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
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
;
89 void quotaDumpQueue(); ///< dumps quota queue for debugging
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).
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
101 void setWriteLimiter(const int aWriteSpeedLimit
, const double anInitialBurst
, const double aHighWatermark
);
102 #endif /* USE_DELAY_POOLS */
106 // a queue of Comm clients waiting for I/O quota controlled by delay pools
109 CBDATA_CLASS(CommQuotaQueue
);
112 CommQuotaQueue(ClientInfo
*info
);
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
);
121 ClientInfo
*clientInfo
; ///< bucket responsible for quota maintenance
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
128 // TODO: optimize using a Ring- or List-based store?
129 typedef std::deque
<int> Store
;
130 Store fds
; ///< descriptor queue
132 #endif /* USE_DELAY_POOLS */