• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2004--2006, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <list>
29 #include <string>
30 
31 #include "talk/xmpp/ratelimitmanager.h"
32 
33 namespace buzz {
34 
GetRateLimit(const std::string event_name)35 RateLimitManager::RateLimit* RateLimitManager::GetRateLimit(
36     const std::string event_name) {
37   RateLimitMap::iterator it = rate_limits_.find(event_name);
38   if (it != rate_limits_.end()) {
39     return it->second;
40   }
41   return NULL;
42 }
43 
IsWithinRateLimit(const std::string event_name)44 bool RateLimitManager::IsWithinRateLimit(const std::string event_name) {
45   RateLimit* current_rate = GetRateLimit(event_name);
46   if (current_rate) {
47     return current_rate->IsWithinRateLimit();
48   }
49   return true; // If no rate limit is set, then you must be under the limit
50 }
51 
UpdateRateLimit(const std::string event_name,int max_count,int per_x_seconds)52 void RateLimitManager::UpdateRateLimit(const std::string event_name,
53                                        int max_count,
54                                        int per_x_seconds) {
55   RateLimit* current_rate = GetRateLimit(event_name);
56   if (!current_rate) {
57     current_rate = new RateLimit(max_count, per_x_seconds);
58     rate_limits_[event_name] = current_rate;
59   }
60   current_rate->UpdateRateLimit();
61 }
62 
VerifyRateLimit(const std::string event_name,int max_count,int per_x_seconds)63 bool RateLimitManager::VerifyRateLimit(const std::string event_name,
64                                        int max_count,
65                                        int per_x_seconds) {
66   return VerifyRateLimit(event_name, max_count, per_x_seconds, false);
67 }
68 
VerifyRateLimit(const std::string event_name,int max_count,int per_x_seconds,bool always_update)69 bool RateLimitManager::VerifyRateLimit(const std::string event_name,
70                                        int max_count,
71                                        int per_x_seconds,
72                                        bool always_update) {
73   bool within_rate_limit = IsWithinRateLimit(event_name);
74   if (within_rate_limit || always_update) {
75     UpdateRateLimit(event_name, max_count, per_x_seconds);
76   }
77   return within_rate_limit;
78 }
79 
80 }
81