diff src/dnsbl.cpp @ 62:7ad7d8b100bf

only keep 20% of the open resolver sockets in the ready pool.
author carl
date Sun, 16 Jan 2005 08:25:36 -0800
parents 7f44a4974bf6
children 579dc5955cbe
line wrap: on
line diff
--- a/src/dnsbl.cpp	Sat Jan 08 12:26:30 2005 -0800
+++ b/src/dnsbl.cpp	Sun Jan 16 08:25:36 2005 -0800
@@ -205,6 +205,8 @@
 static int    resolver_socket   = NULL_SOCKET;  // socket used to listen for resolver requests
 static time_t ERROR_SOCKET_TIME = 60;           // number of seconds between attempts to open the spam filter socket
 static time_t last_error_time;
+static int    resolver_sock_count = 0;          // protected with fd_pool_mutex
+static int    resolver_pool_size  = 0;          // protected with fd_pool_mutex
 
 
 // packed structure to allow a single socket write to dump the
@@ -290,6 +292,9 @@
 void my_disconnect(int sock)
 {
     if (sock != NULL_SOCKET) {
+        pthread_mutex_lock(&fd_pool_mutex);
+            resolver_sock_count--;
+        pthread_mutex_unlock(&fd_pool_mutex);
         shutdown(sock, SHUT_RDWR);
         close(sock);
     }
@@ -316,13 +321,17 @@
     if (sock != NULL_SOCKET) {
         bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0);
         if (!rc) {
-            int er = errno;
             my_disconnect(sock);
             sock = NULL_SOCKET;
             last_error_time = now;
         }
     }
     else last_error_time = now;
+    if (sock != NULL_SOCKET) {
+        pthread_mutex_lock(&fd_pool_mutex);
+            resolver_sock_count++;
+        pthread_mutex_unlock(&fd_pool_mutex);
+    }
     return sock;
 }
 
@@ -410,6 +419,7 @@
             err = false;
             fd  = *i;
             fd_pool.erase(fd);
+            resolver_pool_size--;
         }
         else {
             // pool is empty, get a new fd
@@ -434,8 +444,16 @@
     else {
         int result = pthread_mutex_lock(&fd_pool_mutex);
         if (!result) {
+            if (resolver_sock_count > resolver_pool_size*5) {
             // return the fd to the pool
             fd_pool.insert(fd);
+                resolver_pool_size++;
+            }
+            else {
+                // more than 20% of the open resolver sockets are in the pool
+                // that is enough, so just close this one.
+                my_disconnect(fd);
+            }
             pthread_mutex_unlock(&fd_pool_mutex);
         }
         else {