comparison 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
comparison
equal deleted inserted replaced
61:7f44a4974bf6 62:7ad7d8b100bf
196 196
197 static pthread_mutex_t config_mutex; 197 static pthread_mutex_t config_mutex;
198 static pthread_mutex_t syslog_mutex; 198 static pthread_mutex_t syslog_mutex;
199 static pthread_mutex_t resolve_mutex; 199 static pthread_mutex_t resolve_mutex;
200 static pthread_mutex_t fd_pool_mutex; 200 static pthread_mutex_t fd_pool_mutex;
201
201 static std::set<int> fd_pool; 202 static std::set<int> fd_pool;
202
203 static int NULL_SOCKET = -1; 203 static int NULL_SOCKET = -1;
204 static char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process 204 static char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process
205 static int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests 205 static int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests
206 static time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open the spam filter socket 206 static time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open the spam filter socket
207 static time_t last_error_time; 207 static time_t last_error_time;
208 static int resolver_sock_count = 0; // protected with fd_pool_mutex
209 static int resolver_pool_size = 0; // protected with fd_pool_mutex
208 210
209 211
210 // packed structure to allow a single socket write to dump the 212 // packed structure to allow a single socket write to dump the
211 // length and the following answer. The packing attribute is gcc specific. 213 // length and the following answer. The packing attribute is gcc specific.
212 struct glommer { 214 struct glommer {
288 // 290 //
289 void my_disconnect(int sock); 291 void my_disconnect(int sock);
290 void my_disconnect(int sock) 292 void my_disconnect(int sock)
291 { 293 {
292 if (sock != NULL_SOCKET) { 294 if (sock != NULL_SOCKET) {
295 pthread_mutex_lock(&fd_pool_mutex);
296 resolver_sock_count--;
297 pthread_mutex_unlock(&fd_pool_mutex);
293 shutdown(sock, SHUT_RDWR); 298 shutdown(sock, SHUT_RDWR);
294 close(sock); 299 close(sock);
295 } 300 }
296 } 301 }
297 302
314 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); 319 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1);
315 sock = socket(AF_UNIX, SOCK_STREAM, 0); 320 sock = socket(AF_UNIX, SOCK_STREAM, 0);
316 if (sock != NULL_SOCKET) { 321 if (sock != NULL_SOCKET) {
317 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); 322 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0);
318 if (!rc) { 323 if (!rc) {
319 int er = errno;
320 my_disconnect(sock); 324 my_disconnect(sock);
321 sock = NULL_SOCKET; 325 sock = NULL_SOCKET;
322 last_error_time = now; 326 last_error_time = now;
323 } 327 }
324 } 328 }
325 else last_error_time = now; 329 else last_error_time = now;
330 if (sock != NULL_SOCKET) {
331 pthread_mutex_lock(&fd_pool_mutex);
332 resolver_sock_count++;
333 pthread_mutex_unlock(&fd_pool_mutex);
334 }
326 return sock; 335 return sock;
327 } 336 }
328 337
329 338
330 //////////////////////////////////////////////// 339 ////////////////////////////////////////////////
408 if (i != fd_pool.end()) { 417 if (i != fd_pool.end()) {
409 // have at least one fd in the pool 418 // have at least one fd in the pool
410 err = false; 419 err = false;
411 fd = *i; 420 fd = *i;
412 fd_pool.erase(fd); 421 fd_pool.erase(fd);
422 resolver_pool_size--;
413 } 423 }
414 else { 424 else {
415 // pool is empty, get a new fd 425 // pool is empty, get a new fd
416 fd = my_connect(); 426 fd = my_connect();
417 err = (fd == NULL_SOCKET); 427 err = (fd == NULL_SOCKET);
432 my_disconnect(fd); 442 my_disconnect(fd);
433 } 443 }
434 else { 444 else {
435 int result = pthread_mutex_lock(&fd_pool_mutex); 445 int result = pthread_mutex_lock(&fd_pool_mutex);
436 if (!result) { 446 if (!result) {
437 // return the fd to the pool 447 if (resolver_sock_count > resolver_pool_size*5) {
438 fd_pool.insert(fd); 448 // return the fd to the pool
449 fd_pool.insert(fd);
450 resolver_pool_size++;
451 }
452 else {
453 // more than 20% of the open resolver sockets are in the pool
454 // that is enough, so just close this one.
455 my_disconnect(fd);
456 }
439 pthread_mutex_unlock(&fd_pool_mutex); 457 pthread_mutex_unlock(&fd_pool_mutex);
440 } 458 }
441 else { 459 else {
442 // could not lock the pool, so just close the fd 460 // could not lock the pool, so just close the fd
443 my_disconnect(fd); 461 my_disconnect(fd);