BandwidthBucket.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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#include "squid.h"
10
11#if USE_DELAY_POOLS
12
13#include "BandwidthBucket.h"
14#include "ClientInfo.h"
15#include "comm/Connection.h"
16#include "debug/Stream.h"
17#include "fde.h"
18
19BandwidthBucket::BandwidthBucket(const int speed, const int initialLevelPercent, const double sizeLimit) :
20 bucketLevel( sizeLimit * (initialLevelPercent / 100.0)),
21 selectWaiting(false),
22 writeSpeedLimit(speed),
23 bucketSizeLimit(sizeLimit)
24{
26 /* put current time to have something sensible here */
28}
29
30void
32{
33 if (noLimit())
34 return;
35 // all these times are in seconds, with double precision
36 const double currTime = current_dtime;
37 const double timePassed = currTime - prevTime;
38
39 // Calculate allowance for the time passed. Use double to avoid
40 // accumulating rounding errors for small intervals. For example, always
41 // adding 1 byte instead of 1.4 results in 29% bandwidth allocation error.
42 const double gain = timePassed * writeSpeedLimit;
43
44 // to further combat error accumulation during micro updates,
45 // quit before updating time if we cannot add at least one byte
46 if (gain < 1.0)
47 return;
48
49 prevTime = currTime;
50
51 // for "first" connections, drain initial fat before refilling but keep
52 // updating prevTime to avoid bursts after the fat is gone
54 debugs(77, 4, "not refilling while draining initial fat");
55 return;
56 }
57
58 bucketLevel += gain;
59
60 // obey quota limits
63}
64
65bool
67{
68 const int q = quota();
69 if (!q)
70 return false;
71 else if (q < 0)
72 return true;
73 const int nleft_corrected = min(nleft, q);
74 if (nleft != nleft_corrected) {
75 debugs(77, 5, state->conn << " writes only " <<
76 nleft_corrected << " out of " << nleft);
77 nleft = nleft_corrected;
78 }
79 return true;
80}
81
82void
84{
85 if (len <= 0 || noLimit())
86 return;
87 bucketLevel -= len;
88 if (bucketLevel < 0.0) {
89 debugs(77, DBG_IMPORTANT, "drained too much"); // should not happen
90 bucketLevel = 0;
91 }
92}
93
96{
98 if (!bucket) {
99 ClientInfo *clientInfo = f->clientInfo;
100 if (clientInfo && clientInfo->writeLimitingActive)
101 bucket = clientInfo;
102 }
103 return bucket;
104}
105
106#endif /* USE_DELAY_POOLS */
107
#define false
Definition: GnuRegex.c:240
Base class for Squid-to-client bandwidth limiting.
bool noLimit() const
Whether this bucket will not do bandwidth limiting.
double bucketLevel
how much can be written now
void refillBucket()
Increases the bucket level with the writeSpeedLimit speed.
double bucketSizeLimit
maximum bucket size
BandwidthBucket(const int speed, const int initialLevelPercent, const double sizeLimit)
double prevTime
previous time when we checked
virtual bool applyQuota(int &nleft, Comm::IoCallback *state)
virtual int quota()=0
double writeSpeedLimit
Write speed limit in bytes per second.
static BandwidthBucket * SelectBucket(fde *f)
virtual void reduceBucket(const int len)
Decreases the bucket level.
bool writeLimitingActive
Is write limiter active.
Definition: ClientInfo.h:65
Details about a particular Comm IO callback event.
Definition: IoCallback.h:30
Comm::ConnectionPointer conn
Definition: IoCallback.h:33
C * getRaw() const
Definition: RefCount.h:80
Definition: fde.h:52
MessageBucket::Pointer writeQuotaHandler
response write limiter, if configured
Definition: fde.h:144
ClientInfo * clientInfo
pointer to client info used in client write limiter or nullptr if not present
Definition: fde.h:143
A const & min(A const &lhs, A const &rhs)
#define DBG_IMPORTANT
Definition: Stream.h:41
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
double current_dtime
the current UNIX time in seconds (with microsecond precision)
Definition: stub_libtime.cc:19
time_t getCurrentTime() STUB_RETVAL(0) int tvSubUsec(struct timeval

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors