mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 07:11:10 +00:00 
			
		
		
		
	improve rlimit usage (#982)
* improve rlimit usage * fix windows build * fix code style
This commit is contained in:
		
							parent
							
								
									89f5b646c3
								
							
						
					
					
						commit
						c110b27ef5
					
				
					 3 changed files with 29 additions and 20 deletions
				
			
		
							
								
								
									
										20
									
								
								caddy.go
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								caddy.go
									
										
									
									
									
								
							| 
						 | 
					@ -21,8 +21,6 @@ import (
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
					 | 
				
			||||||
	"runtime"
 | 
					 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
| 
						 | 
					@ -725,24 +723,6 @@ func IsLoopback(addr string) bool {
 | 
				
			||||||
		strings.HasPrefix(host, "127.")
 | 
							strings.HasPrefix(host, "127.")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// checkFdlimit issues a warning if the OS limit for
 | 
					 | 
				
			||||||
// max file descriptors is below a recommended minimum.
 | 
					 | 
				
			||||||
func checkFdlimit() {
 | 
					 | 
				
			||||||
	const min = 8192
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Warn if ulimit is too low for production sites
 | 
					 | 
				
			||||||
	if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
 | 
					 | 
				
			||||||
		out, err := exec.Command("sh", "-c", "ulimit -n").Output() // use sh because ulimit isn't in Linux $PATH
 | 
					 | 
				
			||||||
		if err == nil {
 | 
					 | 
				
			||||||
			lim, err := strconv.Atoi(string(bytes.TrimSpace(out)))
 | 
					 | 
				
			||||||
			if err == nil && lim < min {
 | 
					 | 
				
			||||||
				fmt.Printf("WARNING: File descriptor limit %d is too low for production servers. "+
 | 
					 | 
				
			||||||
					"At least %d is recommended. Fix with \"ulimit -n %d\".\n", lim, min, min)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Upgrade re-launches the process, preserving the listeners
 | 
					// Upgrade re-launches the process, preserving the listeners
 | 
				
			||||||
// for a graceful restart. It does NOT load new configuration;
 | 
					// for a graceful restart. It does NOT load new configuration;
 | 
				
			||||||
// it only starts the process anew with a fresh binary.
 | 
					// it only starts the process anew with a fresh binary.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								rlimit_posix.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								rlimit_posix.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					// +build !windows
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"syscall"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// checkFdlimit issues a warning if the OS limit for
 | 
				
			||||||
 | 
					// max file descriptors is below a recommended minimum.
 | 
				
			||||||
 | 
					func checkFdlimit() {
 | 
				
			||||||
 | 
						const min = 8192
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Warn if ulimit is too low for production sites
 | 
				
			||||||
 | 
						rlimit := &syscall.Rlimit{}
 | 
				
			||||||
 | 
						err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, rlimit)
 | 
				
			||||||
 | 
						if err == nil && rlimit.Cur < min {
 | 
				
			||||||
 | 
							fmt.Printf("WARNING: File descriptor limit %d is too low for production servers. "+
 | 
				
			||||||
 | 
								"At least %d is recommended. Fix with \"ulimit -n %d\".\n", rlimit.Cur, min, min)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										6
									
								
								rlimit_windows.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								rlimit_windows.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// checkFdlimit issues a warning if the OS limit for
 | 
				
			||||||
 | 
					// max file descriptors is below a recommended minimum.
 | 
				
			||||||
 | 
					func checkFdlimit() {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue