github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/connpool/pool.go (about)

     1  package connpool
     2  
     3  import (
     4  	"sync"
     5  	"syscall"
     6  
     7  	"github.com/Cloud-Foundations/Dominator/lib/resourcepool"
     8  )
     9  
    10  var (
    11  	lock sync.Mutex
    12  	pool *resourcepool.Pool
    13  )
    14  
    15  func getConnectionLimit() uint {
    16  	var rlim syscall.Rlimit
    17  	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
    18  		return 900
    19  	}
    20  	maxConnAttempts := rlim.Cur - 50
    21  	maxConnAttempts = (maxConnAttempts / 100)
    22  	if maxConnAttempts < 1 {
    23  		maxConnAttempts = 1
    24  	} else {
    25  		maxConnAttempts *= 100
    26  	}
    27  	return uint(maxConnAttempts)
    28  }
    29  
    30  func getResourcePool() *resourcepool.Pool {
    31  	// Delay setting of internal limits to allow application code to increase
    32  	// the limit on file descriptors first.
    33  	if pool == nil {
    34  		lock.Lock()
    35  		if pool == nil {
    36  			pool = resourcepool.New(getConnectionLimit(), "connections")
    37  		}
    38  		lock.Unlock()
    39  	}
    40  	return pool
    41  }