2 * Copyright (C) 1996-2017 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
12 #include "base/ByteCounter.h"
16 #include "ip/Address.h"
18 #include "mem/forward.h"
29 MEMPROXY_CLASS(ClientInfo
);
32 explicit ClientInfo(const Ip::Address
&);
35 hash_link hash
; /* must be first */
40 Protocol() : n_requests(0) {
41 memset(result_hist
, 0, sizeof(result_hist
));
44 int result_hist
[LOG_TYPE_MAX
];
46 ByteCounter kbytes_in
;
47 ByteCounter kbytes_out
;
48 ByteCounter hit_kbytes_out
;
52 Cutoff() : time(0), n_req(0), n_denied(0) {}
58 int n_established
; /* number of current established connections */
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
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
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
85 void quotaDumpQueue(); ///< dumps quota queue for debugging
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).
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
97 void setWriteLimiter(const int aWriteSpeedLimit
, const double anInitialBurst
, const double aHighWatermark
);
98 #endif /* USE_DELAY_POOLS */
102 // a queue of Comm clients waiting for I/O quota controlled by delay pools
105 CBDATA_CLASS(CommQuotaQueue
);
108 CommQuotaQueue(ClientInfo
*info
);
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
);
117 ClientInfo
*clientInfo
; ///< bucket responsible for quota maintenance
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
124 // TODO: optimize using a Ring- or List-based store?
125 typedef std::deque
<int> Store
;
126 Store fds
; ///< descriptor queue
128 #endif /* USE_DELAY_POOLS */