]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Moved PPS calculations to a utility function
authorAlan T. DeKok <aland@freeradius.org>
Wed, 22 Feb 2012 15:43:30 +0000 (16:43 +0100)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 22 Feb 2012 15:48:31 +0000 (16:48 +0100)
src/include/radiusd.h
src/main/listen.c
src/main/process.c
src/main/util.c

index dde6732273b39a68c9faaa5cf086ac4d6cf125c3..991ad9b385b85607e484223a8640ce913b3fc4f0 100644 (file)
@@ -615,6 +615,8 @@ void                *request_data_reference(REQUEST *request,
                                  void *unique_ptr, int unique_int);
 int            rad_copy_string(char *dst, const char *src);
 int            rad_copy_variable(char *dst, const char *from);
+int            rad_pps(int *past, int *present, time_t *then,
+                       struct timeval *now);
 
 /* client.c */
 RADCLIENT_LIST *clients_init(void);
index 48b3a4a37ee2131ab88fad53d0732b9571e253af..9769bae543c77077144e756e55fceaf83c233299 100644 (file)
@@ -892,7 +892,7 @@ static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
                              &max_pps, "0");
        if (rcode < 0) return -1;
 
-       if ((max_pps < 10) || (max_pps > 1000000)) {
+       if (max_pps && ((max_pps < 10) || (max_pps > 1000000))) {
                        cf_log_err(cf_sectiontoitem(cs),
                                   "Invalid value for \"max_pps\"");
                        return -1;
index 1eaecc2b68ed88f5713c0663c0613316204a1808..362cf28c3198a910b61a102dd86239e891eca407 100644 (file)
@@ -1304,39 +1304,8 @@ int request_receive(rad_listen_t *listener, RADIUS_PACKET *packet,
        if (sock->max_rate) {
                int pps;
 
-               /*
-                *      Track packets in the previous second.
-                */
-               if (sock->rate_time != now.tv_sec) {
-                       sock->rate_time = now.tv_sec;
-                       sock->rate_pps_old = sock->rate_pps_now;
-                       sock->rate_pps_now = 0;
-               }
-
-               /*
-                *      Bootstrap PPS by looking at a percentage of
-                *      the previous PPS.  This lets us take a moving
-                *      count, without doing a moving average.  If
-                *      we're a fraction "f" (0..1) into the current
-                *      second, we can get a good guess for PPS by
-                *      doing:
-                *
-                *      PPS = pps_now + pps_old * (1 - f)
-                *
-                *      It's an instantaneous measurement, rather than
-                *      a moving average.  This will hopefully let it
-                *      respond better to sudden spikes.
-                *
-                *      Doing the calculations by thousands allows us
-                *      to not overflow 2^32, AND to not underflow
-                *      when we divide by USEC.
-                */
-               pps = USEC - now.tv_usec;
-               pps /= 1000;               /* now 0..9999 */
-               pps *= sock->rate_pps_old; /* capped at 1000000 */
-
-               pps += sock->rate_pps_now * 1000;
-               pps /= 1000;
+               pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now,
+                             &sock->rate_time, &now);
 
                if (pps > sock->max_rate) {
                        DEBUG("Dropping request due to rate limiting");
index e6a1a26ba67f6b0f417708041012b3f9672a6195..ca3ab52f5ed89b932ff33756b3d743f354dd9fd7 100644 (file)
@@ -610,3 +610,43 @@ int rad_copy_variable(char *to, const char *from)
        return -1;
 }
 
+#ifndef USEC
+#define USEC 1000000
+#endif
+
+int rad_pps(int *past, int *present, time_t *then, struct timeval *now)
+{
+       int pps;
+
+       if (*then != now->tv_sec) {
+               *then = now->tv_sec;
+               *past = *present;
+               *present = 0;
+       }
+
+       /*
+        *      Bootstrap PPS by looking at a percentage of
+        *      the previous PPS.  This lets us take a moving
+        *      count, without doing a moving average.  If
+        *      we're a fraction "f" (0..1) into the current
+        *      second, we can get a good guess for PPS by
+        *      doing:
+        *
+        *      PPS = pps_now + pps_old * (1 - f)
+        *
+        *      It's an instantaneous measurement, rather than
+        *      a moving average.  This will hopefully let it
+        *      respond better to sudden spikes.
+        *
+        *      Doing the calculations by thousands allows us
+        *      to not overflow 2^32, AND to not underflow
+        *      when we divide by USEC.
+        */
+       pps = USEC - now->tv_usec; /* useconds left in previous second */
+       pps /= 1000;               /* scale to milliseconds */
+       pps *= *past;              /* multiply by past count to get fraction */
+       pps /= 1000;               /* scale to usec again */
+       pps += *present;           /* add in current count */
+
+       return pps;
+}