github.com/Cloud-Foundations/Dominator@v0.3.4/lib/connpool/pool.go (about) 1 package connpool 2 3 import ( 4 "sync" 5 6 "github.com/Cloud-Foundations/Dominator/lib/resourcepool" 7 "github.com/Cloud-Foundations/Dominator/lib/wsyscall" 8 ) 9 10 var ( 11 lock sync.Mutex 12 pool *resourcepool.Pool 13 ) 14 15 func getConnectionLimit() uint { 16 maxFD, _, err := wsyscall.GetFileDescriptorLimit() 17 if err != nil { 18 return 900 19 } 20 maxConnAttempts := maxFD - 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 }