func checkAndSetUlimit() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("error getting rlimit: %s", err) } ipfsFileDescNum := int64(ipfsFileDescNum) var setting bool if rLimit.Cur < ipfsFileDescNum { if rLimit.Max < ipfsFileDescNum { log.Error("adjusting max") rLimit.Max = ipfsFileDescNum } fmt.Printf("Adjusting current ulimit to %d...\n", ipfsFileDescNum) rLimit.Cur = ipfsFileDescNum setting = true } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("error setting ulimit: %s", err) } if setting { fmt.Printf("Successfully raised file descriptor limit to %d.\n", ipfsFileDescNum) } return nil }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) os.Setenv("GOTRACEBACK", "crash") lim := syscall.Rlimit{} syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim) if lim.Cur < _MaxOpenfile || lim.Max < _MaxOpenfile { lim.Cur = _MaxOpenfile lim.Max = _MaxOpenfile syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim) } ln, err := net.Listen("tcp", ":1153") if err != nil { log.Fatal(err) } go TCPServer(ln, queryTypeDNS) ln, err = net.Listen("tcp", ":1154") if err != nil { log.Fatal(err) } go TCPServer(ln, queryTypeMYIP) http.HandleFunc("/myip", HTTPServerMYIP) http.HandleFunc("/dns", HTTPServerDNS) http.HandleFunc("/health", HTTPServerHealth) log.Fatal(http.ListenAndServe(":1053", nil)) }
// MaximizeOpenFileLimit tries to set the resource limit RLIMIT_NOFILE (number // of open file descriptors) to the max (hard limit), if the current (soft // limit) is below the max. Returns the new (though possibly unchanged) limit, // or an error if it could not be changed. func MaximizeOpenFileLimit() (int, error) { // Get the current limit on number of open files. var lim syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { return 0, err } // If we're already at max, there's no need to try to raise the limit. if lim.Cur >= lim.Max { return int(lim.Cur), nil } // Try to increase the limit to the max. oldLimit := lim.Cur lim.Cur = lim.Max if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { return int(oldLimit), err } // If the set succeeded, perform a new get to see what happened. We might // have gotten a value lower than the one in lim.Max, if lim.Max was // something that indicated "unlimited" (i.e. intmax). if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { // We don't really know the correct value here since Getrlimit // mysteriously failed after working once... Shouldn't ever happen, I // think. return 0, err } return int(lim.Cur), nil }
func TestMain(m *testing.M) { var lim syscall.Rlimit syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim) lim.Cur = 1024000 lim.Max = 1024000 syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim) }
func main() { var rlim syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("获取Rlimit报错 %s", err) os.Exit(1) } fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) rlim.Max = 65535 rlim.Cur = 65535 //err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("设置Rlimit报错 %s", err) os.Exit(1) } //fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("获取Rlimit报错 %s", err) os.Exit(1) } fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) }
// SetLimits raises some process limits to values which allow dcrd and // associated utilities to run. func SetLimits() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } if rLimit.Cur > fileLimitWant { return nil } if rLimit.Max < fileLimitMin { err = fmt.Errorf("need at least %v file descriptors", fileLimitMin) return err } if rLimit.Max < fileLimitWant { rLimit.Cur = rLimit.Max } else { rLimit.Cur = fileLimitWant } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { // try min value rLimit.Cur = fileLimitMin err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } } return nil }
// setOpenFilesLimit sets the open file limit in the kernel // cur is the soft limit, max is the ceiling (or hard limit) for that limit func (pm *ProcessManager) setOpenFilesLimit(cur, max uint64) error { var rLimit syscall.Rlimit // First check if the limits are already what we want err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } // If the current values are less than we want, set them if rLimit.Cur < cur || rLimit.Max < max { if cur > rLimit.Cur { rLimit.Cur = cur } if max > rLimit.Max { rLimit.Max = max } log.Infof("Setting open files limit (soft, hard) to (%v, %v)", rLimit.Cur, rLimit.Max) err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } } return nil }
func TestGetSoftFDLimitWithCurrent(t *testing.T) { requestedSoftFDLimit := uint64(1024) currentSoftFdLimit := uint64(2048) currentHardFdLimit := uint64(4096) limit := syscall.Rlimit{ Cur: currentSoftFdLimit, Max: currentHardFdLimit, } requiresUpdate, softFDLimit := getSoftFDLimit( requestedSoftFDLimit, limit, ) assert.False(t, requiresUpdate) limit.Cur = uint64(512) requiresUpdate, softFDLimit = getSoftFDLimit( requestedSoftFDLimit, limit, ) assert.True(t, requiresUpdate) assert.Equals(t, softFDLimit, requestedSoftFDLimit) }
// Set Max File Descriptor limits // // Background information: // // - SG docs // http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/os-level-tuning/max-file-descriptors/index.html // - Related SG issues // https://github.com/couchbase/sync_gateway/issues/1083 // - Hard limit vs Soft limit // http://unix.stackexchange.com/questions/29577/ulimit-difference-between-hard-and-soft-limits func SetMaxFileDescriptors(requestedSoftFDLimit uint64) (uint64, error) { var limits syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { return 0, err } requiresUpdate, recommendedSoftFDLimit := getSoftFDLimit( requestedSoftFDLimit, limits, ) // No call to Setrlimit required, because the requested soft limit is lower than current soft limit if !requiresUpdate { return 0, nil } // Update the soft limit (but don't bother updating the hard limit, since only root can do that, // and it's assumed that this process is not running as root) limits.Cur = recommendedSoftFDLimit err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits) if err == nil { Logf("Configured process to allow %d open file descriptors", recommendedSoftFDLimit) } return recommendedSoftFDLimit, err }
// upLimitToHard ups a resource limit identified // by the resource argument to it's hard limit // the returned error is either from syscall.(Get|Set)rlimit func upLimitToHard(resource int) error { var resLimit = new(syscall.Rlimit) if err := syscall.Getrlimit(resource, resLimit); err != nil { return err } resLimit.Cur = resLimit.Max return syscall.Setrlimit(resource, resLimit) }
func initLimit() { var rLimit syscall.Rlimit rLimit.Max = 10000 rLimit.Cur = 10000 err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Setting Rlimit ", err) } }
func main() { log.Println("livesyncd running") log.Println("Stop with [CTRL] + [c]") log.Println("Ignore: ", config.Ignore) rlimit := new(syscall.Rlimit) syscall.Getrlimit(syscall.RLIMIT_NOFILE, rlimit) rlimit.Cur = rlimit.Max err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, rlimit) if err != nil { log.Panicf("Could not change Rlimit: %q", err) } watcher, err := fsnotify.NewWatcher() if err != nil { log.Panicln(err) } events := make(chan *SyncEvent) quitSync := make(chan bool) quitWatcher := make(chan bool) sigInt := make(chan os.Signal) signal.Notify(sigInt, os.Interrupt) StartSFTPSync(events, quitSync, config) startWatchLoop(events, quitWatcher, watcher) watched := addWatchesRecursive(root, watcher) log.Printf("Found %d directories to watch\n", watched) select { case <-sigInt: log.Println("Stopping to watch...") // Wait until the watcher has finished quitting quitWatcher <- true log.Println("Done") // Close all file handles, opened by the watcher watcher.Close() log.Println("Stopping Sync Backend...") quitSync <- true <-quitSync log.Println("Done") return } }
func init() { var rLimit syscall.Rlimit rLimit.Max = 4096 rLimit.Cur = 4096 err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Println("Warning: setrlimit:", err) } }
// 提升程序可打开的文件描述符上限 func init() { var rlim syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { panic(err.Error()) } rlim.Cur = 1000000 rlim.Max = 1000000 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { panic(err.Error()) } }
// For all unixes we need to bump allowed number of open files to a // higher value than its usual default of '1024'. The reasoning is // that this value is too small for a server. func setMaxOpenFiles() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } // Set the current limit to Max, it is usually around 4096. // TO increase this limit further user has to manually edit // `/etc/security/limits.conf` rLimit.Cur = rLimit.Max return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) }
func main() { logging.SetFormatter(logging.MustStringFormatter(format)) logging.SetLevel(logging.INFO, "gogorobot") //logging.SetLevel(logging.DEBUG, "gogorobot") // // Set the file descriptor limit higher if we've permission var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Info(fmt.Sprintf("Error geting rlimit: %s", err)) } rLimit.Max = 65536 rLimit.Cur = 65536 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Info(fmt.Sprintf("Error setting rlimit: %s", err)) } // fetchPipeline := make(chan FetchRequest) savePipeline := make(chan RobotResponse) // NOTE: Important to pass via pointer // Otherwise, a new WaitGroup is created var fetchGroup sync.WaitGroup var saveGroup sync.WaitGroup saveGroup.Add(1) go saveRobots(savePipeline, &saveGroup) for i := 0; i < 50; i++ { fetchGroup.Add(1) go fetchRobot(fetchPipeline, savePipeline, &fetchGroup) } // Insert entries reader := bufio.NewReader(os.Stdin) for { domain, err := reader.ReadString('\n') if err != nil { break } domain = strings.TrimRight(domain, "\r\n") log.Debug(fmt.Sprintf("MAIN: Providing %s to fetch pipeline", domain)) fetchPipeline <- FetchRequest{domain, 0} } // TODO: Temporary fix to allow time for the pipeline to clear (see TODO in fetcher's error area) time.Sleep(60 * time.Second) close(fetchPipeline) log.Notice("Fetching pipeline closed -- waiting for pending fetches to complete") // fetchGroup.Wait() close(savePipeline) saveGroup.Wait() }
func NewServer() *Server { s := &Server{ port: common.GetConfInt("server", "port", 8080), maxClients: common.GetConfInt("server", "max_clients", 100000), clientNum: 0, acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5), connTimeout: common.GetConfSecond("server", "connection_timeout", 60*3), headerLen: common.GetConfInt("server", "header_length", 10), maxBodyLen: common.GetConfInt("server", "max_body_length", 102400), running: false, heartbeat: time.Now().Unix(), workerNum: common.WorkerNum, currentWorker: -1, slowread: common.GetConfSecond("server", "slow_read", 0), logger: common.NewLogger("server"), } if s == nil { fmt.Println("new server failed") return nil } if s.logger == nil { fmt.Println("new server logger failed") return nil } ip, err := common.Conf.String("server", "bind") if err != nil { ip = "" } s.ip = ip max_clients := uint64(s.maxClients) if max_clients > 1024 { var rlim syscall.Rlimit err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Println("server get rlimit error: " + err.Error()) return nil } rlim.Cur = max_clients rlim.Max = max_clients err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Println("server set rlimit error: " + err.Error()) return nil } s.logger.Info("set fd limit to %d", s.maxClients) } return s }
func getMaxOpenFiles() int { var rlim syscall.Rlimit var err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err == nil && rlim.Cur < rlim.Max { rlim.Cur = rlim.Max syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) } var maxOpenFiles = 1024 if maxOpenFiles < int(rlim.Cur) { maxOpenFiles = int(rlim.Cur) } return maxOpenFiles }
func init() { // Parse args flag.StringVar(&Dirname, "directory", "outfiles", "Output directory for downloaded files") flag.StringVar(&Artist, "artist", "", "Artist name if songs should be tagged by artist") flag.StringVar(&Album, "album", "", "Album name if songs should be tagged by album") flag.Parse() // Check for command-line dependencies for _, dependency := range []string{youtubeDl, ffmpeg} { if _, err := exec.LookPath(dependency); err != nil { log.Fatalf("Must have %s in your PATH", dependency) } } // Make output directory if err := os.Mkdir(Dirname, 0777); err != nil && !os.IsExist(err) { log.Fatal("Could not make directory", Dirname, "for output files") } // @hack - Increase file descriptor limt const PLAYLIST_SIZE_LIMIT, SUBPROCS_PER_VIDEO = 200, 2 // max size of YouTubePlaylist, subprocesses needed to download and convert video fdLimit := new(syscall.Rlimit) err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, fdLimit) fdLimit.Cur += PLAYLIST_SIZE_LIMIT * SUBPROCS_PER_VIDEO err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, fdLimit) if err != nil { log.Fatal(err.Error()) } // Unpack configuration type config struct { GoogleAPIKey string `json:"google_api_key"` } configJson, err := ioutil.ReadFile("config.json") if err != nil { // config.json missing log.Fatal("Must have config.json file to run") } conf := new(config) err = json.Unmarshal(configJson, conf) if err != nil { // unmarshal error log.Fatal("config.json is formatted incorrectly") } if googleAPIKey = conf.GoogleAPIKey; googleAPIKey == "" { // google_api_key missing from config log.Fatal("Google API Key is missing from config.json") } }
func init() { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Getting Rlimit ", err) } rLimit.Max = 999999 rLimit.Cur = 999999 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Setting Rlimit ", err) } }
func setHardRLimit(label string, rLimitId int, rLimitMax uint64) error { var rlimit syscall.Rlimit if err := syscall.Getrlimit(rLimitId, &rlimit); err != nil { return fmt.Errorf("container_daemon: getting system limit %s: %s", label, err) } rlimit.Max = rLimitMax if err := syscall.Setrlimit(rLimitId, &rlimit); err != nil { return fmt.Errorf("container_daemon: setting hard system limit %s: %s", label, err) } return nil }
func SetRlimit() { var rLimit syscall.Rlimit rLimit.Max = 999999 rLimit.Cur = 999999 err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { panic(fmt.Sprintf("Error Setting Rlimit '%s'", err)) } err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { panic(fmt.Sprintf("Error Getting Rlimit '%s'", err)) } fmt.Printf("final: rlimit.Cur = %d, rlimit.Max = %d\n", rLimit.Cur, rLimit.Max) }
func MaybeBecomeChildProcess() { lrs := os.Getenv("_RUNSIT_LAUNCH_INFO") if lrs == "" { return } defer os.Exit(2) // should never make it this far, though lr := new(LaunchRequest) d := gob.NewDecoder(base64.NewDecoder(base64.StdEncoding, strings.NewReader(lrs))) err := d.Decode(lr) if err != nil { log.Fatalf("Failed to decode LaunchRequest in child: %v", err) } if lr.NumFiles != 0 { var lim syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { log.Fatalf("failed to get NOFILE rlimit: %v", err) } noFile := rlim_t(lr.NumFiles) lim.Cur = noFile lim.Max = noFile if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { log.Fatalf("failed to set NOFILE rlimit: %v", err) } } if lr.Gid != 0 { if err := syscall.Setgid(lr.Gid); err != nil { log.Fatalf("failed to Setgid(%d): %v", lr.Gid, err) } } if len(lr.Gids) != 0 { if err := syscall.Setgroups(lr.Gids); err != nil { log.Printf("setgroups: %v", err) } } if lr.Uid != 0 { if err := syscall.Setuid(lr.Uid); err != nil { log.Fatalf("failed to Setuid(%d): %v", lr.Uid, err) } } if lr.Path != "" { err = os.Chdir(lr.Dir) if err != nil { log.Fatalf("failed to chdir to %q: %v", lr.Dir, err) } } err = syscall.Exec(lr.Path, lr.Argv, lr.Env) log.Fatalf("failed to exec %q: %v", lr.Path, err) }
func checkLimits() error { var l syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &l); err != nil { return err } if l.Cur <= minRlimit { logrus.WithFields(logrus.Fields{ "current": l.Cur, "max": l.Max, }).Warn("containerd: low RLIMIT_NOFILE changing to max") l.Cur = l.Max return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &l) } return nil }
func SetMaxFileDescriptors(maxFDs uint64) (uint64, error) { var limits syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { return maxFDs, err } if maxFDs > limits.Max { maxFDs = limits.Max } if limits.Cur == maxFDs { return maxFDs, nil } limits.Cur = maxFDs limits.Max = maxFDs return maxFDs, syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits) }
func tweakLimit() { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { jww.ERROR.Println("Unable to obtain rLimit", err) } if rLimit.Cur < rLimit.Max { rLimit.Max = 999999 rLimit.Cur = 999999 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { jww.ERROR.Println("Unable to increase number of open files limit", err) } } }
func setRlimit() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("could not get Rlimit: %s", err) } rLimit.Max = 999999 rLimit.Cur = 999999 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("could not set Rlimit: %s", err) } return nil }
// raiseFdLimit tries to maximize the file descriptor allowance of this process // to the maximum hard-limit allowed by the OS. func raiseFdLimit(max uint64) error { // Get the current limit var limit syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { return err } // Try to update the limit to the max allowance limit.Cur = limit.Max if limit.Cur > max { limit.Cur = max } if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { return err } return nil }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) os.Setenv("GOTRACEBACK", "crash") _BackendAddrCache.Store(make(backendAddrMap)) var lim syscall.Rlimit syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim) if lim.Cur < _MaxOpenfile || lim.Max < _MaxOpenfile { lim.Cur = _MaxOpenfile lim.Max = _MaxOpenfile syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim) } _SecretPassphase = []byte(os.Getenv("SECRET")) mhs, err := strconv.Atoi(os.Getenv("MAX_HTTP_HEADER_SIZE")) if err == nil && mhs > _minHTTPHeaderSize { _maxHTTPHeaderSize = mhs } bt, err := strconv.Atoi(os.Getenv("BACKEND_TIMEOUT")) if err == nil && bt > 0 { _BackendDialTimeout = bt } connReadTimeout, err := strconv.Atoi(os.Getenv("CONN_READ_TIMEOUT")) if err == nil && connReadTimeout >= 0 { _ConnReadTimeout = time.Second * time.Duration(connReadTimeout) } listenPort, err := strconv.Atoi(os.Getenv("LISTEN_PORT")) if err == nil && listenPort > 0 && listenPort <= 65535 { _DefaultPort = listenPort } pprofPort, err := strconv.Atoi(os.Getenv("PPROF_PORT")) if err == nil && pprofPort > 0 && pprofPort <= 65535 { go func() { log.Println(http.ListenAndServe(":"+strconv.Itoa(pprofPort), nil)) }() } listenAndServe() log.Println("Exiting") }
func setUlimit(cfg *Config) error { var rLimit syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { return err } if cfg.NoFiles == 0 { rLimit.Max = 1000000 } else { rLimit.Max = cfg.NoFiles } rLimit.Cur = rLimit.Max return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) }