comparison src/dnsbl.cpp @ 64:6add229288a8 stable-4-3

only keep 20% of the open resolver sockets in the ready pool.
author carl
date Sun, 16 Jan 2005 09:20:50 -0800
parents 579dc5955cbe
children 1ab70970c8c8
comparison
equal deleted inserted replaced
63:579dc5955cbe 64:6add229288a8
286 286
287 287
288 //////////////////////////////////////////////// 288 ////////////////////////////////////////////////
289 // disconnect the fd from the dns resolver process 289 // disconnect the fd from the dns resolver process
290 // 290 //
291 void my_disconnect(int sock); 291 void my_disconnect(int sock, bool decrement = true);
292 void my_disconnect(int sock) 292 void my_disconnect(int sock, bool decrement)
293 { 293 {
294 if (sock != NULL_SOCKET) { 294 if (sock != NULL_SOCKET) {
295 pthread_mutex_lock(&fd_pool_mutex); 295 if (decrement) {
296 resolver_sock_count--; 296 pthread_mutex_lock(&fd_pool_mutex);
297 pthread_mutex_unlock(&fd_pool_mutex); 297 resolver_sock_count--;
298 pthread_mutex_unlock(&fd_pool_mutex);
299 }
298 shutdown(sock, SHUT_RDWR); 300 shutdown(sock, SHUT_RDWR);
299 close(sock); 301 close(sock);
300 } 302 }
301 } 303 }
302 304
319 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); 321 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1);
320 sock = socket(AF_UNIX, SOCK_STREAM, 0); 322 sock = socket(AF_UNIX, SOCK_STREAM, 0);
321 if (sock != NULL_SOCKET) { 323 if (sock != NULL_SOCKET) {
322 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); 324 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0);
323 if (!rc) { 325 if (!rc) {
324 my_disconnect(sock); 326 my_disconnect(sock, false);
325 sock = NULL_SOCKET; 327 sock = NULL_SOCKET;
326 last_error_time = now; 328 last_error_time = now;
327 } 329 }
328 } 330 }
329 else last_error_time = now; 331 else last_error_time = now;
418 // have at least one fd in the pool 420 // have at least one fd in the pool
419 err = false; 421 err = false;
420 fd = *i; 422 fd = *i;
421 fd_pool.erase(fd); 423 fd_pool.erase(fd);
422 resolver_pool_size--; 424 resolver_pool_size--;
425 pthread_mutex_unlock(&fd_pool_mutex);
423 } 426 }
424 else { 427 else {
425 // pool is empty, get a new fd 428 // pool is empty, get a new fd
429 pthread_mutex_unlock(&fd_pool_mutex);
426 fd = my_connect(); 430 fd = my_connect();
427 err = (fd == NULL_SOCKET); 431 err = (fd == NULL_SOCKET);
428 } 432 }
429 pthread_mutex_unlock(&fd_pool_mutex);
430 } 433 }
431 else { 434 else {
432 // cannot lock the pool, just get a new fd 435 // cannot lock the pool, just get a new fd
433 fd = my_connect(); 436 fd = my_connect();
434 err = (fd == NULL_SOCKET); 437 err = (fd == NULL_SOCKET);
446 if (!result) { 449 if (!result) {
447 if ((resolver_sock_count > resolver_pool_size*5) || (resolver_pool_size < 5)) { 450 if ((resolver_sock_count > resolver_pool_size*5) || (resolver_pool_size < 5)) {
448 // return the fd to the pool 451 // return the fd to the pool
449 fd_pool.insert(fd); 452 fd_pool.insert(fd);
450 resolver_pool_size++; 453 resolver_pool_size++;
454 pthread_mutex_unlock(&fd_pool_mutex);
451 } 455 }
452 else { 456 else {
453 // more than 20% of the open resolver sockets are in the pool, and the 457 // more than 20% of the open resolver sockets are in the pool, and the
454 // pool as at least 5 sockets. that is enough, so just close this one. 458 // pool as at least 5 sockets. that is enough, so just close this one.
459 pthread_mutex_unlock(&fd_pool_mutex);
455 my_disconnect(fd); 460 my_disconnect(fd);
456 } 461 }
457 pthread_mutex_unlock(&fd_pool_mutex);
458 } 462 }
459 else { 463 else {
460 // could not lock the pool, so just close the fd 464 // could not lock the pool, so just close the fd
461 my_disconnect(fd); 465 my_disconnect(fd);
462 } 466 }