Clean up old files only once a second
authorAlan T. DeKok <aland@freeradius.org>
Wed, 17 Feb 2016 16:22:36 +0000 (11:22 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 17 Feb 2016 16:22:36 +0000 (11:22 -0500)
src/main/exfile.c

index e63a426..1684c2b 100644 (file)
@@ -41,6 +41,7 @@ typedef struct exfile_entry_t {
 struct exfile_t {
        uint32_t        max_entries;    //!< How many file descriptors we keep track of.
        uint32_t        max_idle;       //!< Maximum idle time for a descriptor.
+       time_t          last_cleaned;
 
 #ifdef HAVE_PTHREAD_H
        pthread_mutex_t mutex;
@@ -151,18 +152,22 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app
        /*
         *      Clean up old entries.
         */
-       for (i = 0; i < ef->max_entries; i++) {
-               if (!ef->entries[i].filename) continue;
-
-               if ((ef->entries[i].last_used + ef->max_idle) < now) {
-                       /*
-                        *      This will block forever if a thread is
-                        *      doing something stupid.
-                        */
-                       TALLOC_FREE(ef->entries[i].filename);
-                       ef->entries[i].hash = 0;
-                       close(ef->entries[i].fd);
-                       ef->entries[i].fd = -1;
+       if (now > (ef->last_cleaned + 1)) {
+               ef->last_cleaned = now;
+
+               for (i = 0; i < ef->max_entries; i++) {
+                       if (!ef->entries[i].filename) continue;
+
+                       if ((ef->entries[i].last_used + ef->max_idle) < now) {
+                               /*
+                                *      This will block forever if a thread is
+                                *      doing something stupid.
+                                */
+                               TALLOC_FREE(ef->entries[i].filename);
+                               ef->entries[i].hash = 0;
+                               close(ef->entries[i].fd);
+                               ef->entries[i].fd = -1;
+                       }
                }
        }