BandwidthBucket.cc
Go to the documentation of this file.
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 #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.h"
17 #include "fde.h"
18 
19 BandwidthBucket::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 
30 void
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 
65 bool
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 
82 void
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 
Base class for Squid-to-client bandwidth limiting.
virtual void reduceBucket(const int len)
Decreases the bucket level.
double prevTime
previous time when we checked
BandwidthBucket(const int speed, const int initialLevelPercent, const double sizeLimit)
void refillBucket()
Increases the bucket level with the writeSpeedLimit speed.
double writeSpeedLimit
Write speed limit in bytes per second.
Comm::ConnectionPointer conn
Definition: IoCallback.h:33
double bucketSizeLimit
maximum bucket size
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
bool noLimit() const
Whether this bucket will not do bandwidth limiting.
double bucketLevel
how much can be written now
static BandwidthBucket * SelectBucket(fde *f)
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
bool writeLimitingActive
Is write limiter active.
Definition: ClientInfo.h:65
virtual bool applyQuota(int &nleft, Comm::IoCallback *state)
time_t getCurrentTime(void)
Get current time.
double current_dtime
Definition: stub_time.cc:16
Definition: fde.h:49
virtual int quota()=0
MessageBucket::Pointer writeQuotaHandler
response write limiter, if configured
Definition: fde.h:121
C * getRaw() const
Definition: RefCount.h:74
Details about a particular Comm IO callback event.
Definition: IoCallback.h:29
A const & min(A const &lhs, A const &rhs)
#define false
Definition: GnuRegex.c:233
ClientInfo * clientInfo
pointer to client info used in client write limiter or nullptr if not present
Definition: fde.h:120

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors