]> git.ipfire.org Git - thirdparty/squid.git/blob - src/DelayUser.cc
merge from SslServerCertValidator r12332
[thirdparty/squid.git] / src / DelayUser.cc
1
2 /*
3 * DEBUG: section 77 Delay Pools
4 * AUTHOR: Robert Collins <robertc@squid-cache.org>
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 *
33 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
34 */
35
36 #include "squid.h"
37
38 #if USE_DELAY_POOLS && USE_AUTH
39 #include "auth/User.h"
40 #include "auth/UserRequest.h"
41 #include "comm/Connection.h"
42 #include "DelayUser.h"
43 #include "NullDelayId.h"
44 #include "Store.h"
45
46 void *
47 DelayUser::operator new(size_t size)
48 {
49 DelayPools::MemoryUsed += sizeof (DelayUser);
50 return ::operator new (size);
51 }
52
53 void
54 DelayUser::operator delete (void *address)
55 {
56 DelayPools::MemoryUsed -= sizeof (DelayUser);
57 ::operator delete (address);
58 }
59
60 DelayUser::DelayUser()
61 {
62 DelayPools::registerForUpdates (this);
63 }
64
65 static SplayNode<DelayUserBucket::Pointer>::SPLAYFREE DelayUserFree;
66
67 DelayUser::~DelayUser()
68 {
69 DelayPools::deregisterForUpdates (this);
70 buckets.head->destroy (DelayUserFree);
71 }
72
73 static SplayNode<DelayUserBucket::Pointer>::SPLAYCMP DelayUserCmp;
74
75 int
76 DelayUserCmp(DelayUserBucket::Pointer const &left, DelayUserBucket::Pointer const &right)
77 {
78 /* Verify for re-currance of Bug 2127. either of these missing will crash strcasecmp() */
79 assert(left->authUser->username() != NULL);
80 assert(right->authUser->username() != NULL);
81
82 /* for rate limiting, case insensitive */
83 return strcasecmp(left->authUser->username(), right->authUser->username());
84 }
85
86 void
87 DelayUserFree(DelayUserBucket::Pointer &)
88 {}
89
90 void
91 DelayUserStatsWalkee(DelayUserBucket::Pointer const &current, void *state)
92 {
93 current->stats ((StoreEntry *)state);
94 }
95
96 void
97 DelayUser::stats(StoreEntry * sentry)
98 {
99 spec.stats (sentry, "Per User");
100
101 if (spec.restore_bps == -1)
102 return;
103
104 storeAppendPrintf(sentry, "\t\tCurrent: ");
105
106 if (!buckets.head) {
107 storeAppendPrintf (sentry, "Not used yet.\n\n");
108 return;
109 }
110
111 buckets.head->walk(DelayUserStatsWalkee, sentry);
112 storeAppendPrintf(sentry, "\n\n");
113 }
114
115 void
116 DelayUser::dump(StoreEntry *entry) const
117 {
118 spec.dump(entry);
119 }
120
121 struct DelayUserUpdater {
122 DelayUserUpdater (DelaySpec &_spec, int _incr):spec(_spec),incr(_incr) {};
123
124 DelaySpec spec;
125 int incr;
126 };
127
128 void
129 DelayUserUpdateWalkee(DelayUserBucket::Pointer const &current, void *state)
130 {
131 DelayUserUpdater *t = (DelayUserUpdater *)state;
132 /* This doesn't change the value of the DelayUserBucket, so is safe */
133 const_cast<DelayUserBucket *>(current.getRaw())->theBucket.update(t->spec, t->incr);
134 }
135
136 void
137 DelayUser::update(int incr)
138 {
139 DelayUserUpdater updater(spec, incr);
140 buckets.head->walk (DelayUserUpdateWalkee, &updater);
141 }
142
143 void
144 DelayUser::parse()
145 {
146 spec.parse();
147 }
148
149 DelayIdComposite::Pointer
150 DelayUser::id(CompositePoolNode::CompositeSelectionDetails &details)
151 {
152 if (!details.user || !details.user->user() || !details.user->user()->username())
153 return new NullDelayId;
154
155 debugs(77, 3, HERE << "Adding a slow-down for User '" << details.user->user()->username() << "'");
156 return new Id(this, details.user->user());
157 }
158
159 void *
160 DelayUser::Id::operator new(size_t size)
161 {
162 DelayPools::MemoryUsed += sizeof (Id);
163 return ::operator new (size);
164 }
165
166 void
167 DelayUser::Id::operator delete (void *address)
168 {
169 DelayPools::MemoryUsed -= sizeof (Id);
170 ::operator delete (address);
171 }
172
173 void *
174 DelayUserBucket::operator new(size_t size)
175 {
176 DelayPools::MemoryUsed += sizeof (DelayUserBucket);
177 return ::operator new (size);
178 }
179
180 void
181 DelayUserBucket::operator delete(void *address)
182 {
183 DelayPools::MemoryUsed -= sizeof(DelayUserBucket);
184 ::operator delete(address);
185 }
186
187 DelayUserBucket::DelayUserBucket(Auth::User::Pointer aUser) : authUser(aUser)
188 {
189 debugs(77, 3, "DelayUserBucket::DelayUserBucket");
190 }
191
192 DelayUserBucket::~DelayUserBucket()
193 {
194 authUser = NULL;
195 debugs(77, 3, "DelayUserBucket::~DelayUserBucket");
196 }
197
198 void
199 DelayUserBucket::stats (StoreEntry *entry) const
200 {
201 storeAppendPrintf(entry, " %s:", authUser->username());
202 theBucket.stats(entry);
203 }
204
205 DelayUser::Id::Id(DelayUser::Pointer aDelayUser, Auth::User::Pointer aUser) : theUser(aDelayUser)
206 {
207 theBucket = new DelayUserBucket(aUser);
208 DelayUserBucket::Pointer const *existing = theUser->buckets.find(theBucket, DelayUserCmp);
209
210 if (existing) {
211 theBucket = *existing;
212 return;
213 }
214
215 theBucket->theBucket.init(theUser->spec);
216 theUser->buckets.head = theUser->buckets.head->insert (theBucket, DelayUserCmp);
217 }
218
219 DelayUser::Id::~Id()
220 {
221 debugs(77, 3, "DelayUser::Id::~Id");
222 }
223
224 int
225 DelayUser::Id::bytesWanted (int min, int max) const
226 {
227 return theBucket->theBucket.bytesWanted(min,max);
228 }
229
230 void
231 DelayUser::Id::bytesIn(int qty)
232 {
233 theBucket->theBucket.bytesIn(qty);
234 }
235
236 #endif /* USE_DELAY_POOLS && USE_AUTH */