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