Changeset 9797


Ignore:
Timestamp:
Dec 18, 2009, 7:05:36 AM (12 years ago)
Author:
charles
Message:

(trunk libT) possibly reduce the frequency of the "too many open files" errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r9629 r9797  
    5454 #include <sys/resource.h> /* getrlimit */
    5555#endif
     56#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
    5657#include <unistd.h>
    57 #include <fcntl.h> /* O_LARGEFILE posix_fadvise */
    5858
    5959#include <evutil.h>
     
    7777***
    7878**/
    79 
    80 enum
    81 {
    82     NOFILE_BUFFER = 512, /* the process' number of open files is
    83                             globalMaxPeers + NOFILE_BUFFER */
    84 };
    8579
    8680struct tr_openfile
     
    823817}
    824818
     819static TR_INLINE int clamp( int val, int lo, int hi )
     820{
     821    if( val < lo ) val = lo;
     822    if( val > hi ) val = hi;
     823    return val;
     824}
     825   
     826
    825827void
    826 tr_fdSetPeerLimit( tr_session * session, int limit )
     828tr_fdSetPeerLimit( tr_session * session, int socketLimit )
    827829{
    828830    struct tr_fdInfo * gFd;
     
    835837    {
    836838        struct rlimit rlim;
     839        const int NOFILE_BUFFER = 512;
     840        const int open_max = sysconf( _SC_OPEN_MAX );
    837841        getrlimit( RLIMIT_NOFILE, &rlim );
    838         rlim.rlim_cur = MIN( rlim.rlim_max, (rlim_t)( limit + NOFILE_BUFFER ) );
     842        rlim.rlim_cur = clamp( open_max, 1024, rlim.rlim_max );
    839843        setrlimit( RLIMIT_NOFILE, &rlim );
    840         gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
    841844        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
     845        gFd->socketLimit = MIN( socketLimit, (int)rlim.rlim_cur - NOFILE_BUFFER );
    842846    }
    843847#else
    844     gFd->socketLimit = limit;
    845 #endif
    846     gFd->publicSocketLimit = limit;
    847 
    848     tr_dbg( "%d usable file descriptors", limit );
     848    gFd->socketLimit = socketLimit;
     849#endif
     850    gFd->publicSocketLimit = socketLimit;
     851
     852    tr_dbg( "socket limit is %d", (int)gFd->socketLimit );
    849853}
    850854
Note: See TracChangeset for help on using the changeset viewer.