Skip to content

Commit

Permalink
Getrlimit not supported by windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ochinchina committed Mar 2, 2020
1 parent 5d63540 commit 608fedb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 52 deletions.
57 changes: 57 additions & 0 deletions rlimit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// +build !windows

package main

import (
"fmt"
"syscall"
)

func (s *Supervisor) checkRequiredResources() error {
if minfds, vErr := s.getMinRequiredRes("minfds"); vErr == nil {
return s.checkMinLimit(syscall.RLIMIT_NOFILE, "NOFILE", minfds)
}
if minprocs, vErr := s.getMinRequiredRes("minprocs"); vErr == nil {
//RPROC = 6
return s.checkMinLimit(6, "NPROC", minprocs)
}
return nil

}

func (s *Supervisor) getMinRequiredRes(resourceName string) (uint64, error) {
if entry, ok := s.config.GetSupervisord(); ok {
value := uint64(entry.GetInt(resourceName, 0))
if value > 0 {
return value, nil
} else {
return 0, fmt.Errorf("No such key %s", resourceName)
}
} else {
return 0, fmt.Errorf("No supervisord section")
}

}


func (s *Supervisor) checkMinLimit(resource int, resourceName string, minRequiredSource uint64) error {
var limit syscall.Rlimit

if syscall.Getrlimit(resource, &limit) != nil {
return fmt.Errorf("fail to get the %s limit", resourceName)
}

if minRequiredSource > limit.Max {
return fmt.Errorf("%s %d is greater than Hard limit %d", resourceName, minRequiredSource, limit.Max)
}

if limit.Cur >= minRequiredSource {
return nil
}

limit.Cur = limit.Max
if syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit) != nil {
return fmt.Errorf(fmt.Sprintf("fail to set the %s to %d", resourceName, limit.Cur))
}
return nil
}
7 changes: 7 additions & 0 deletions rlimit_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows

package main

func (s *Supervisor) checkRequiredResources() error {
return nil
}
52 changes: 0 additions & 52 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"github.com/ochinchina/supervisord/config"
"net/http"
"os"
"runtime"
"strings"
"sync"
"syscall"
"time"

"github.com/ochinchina/supervisord/events"
Expand Down Expand Up @@ -549,56 +547,6 @@ func (s *Supervisor) startHTTPServer() {

}

func (s *Supervisor) getMinRequiredRes(resourceName string) (uint64, error) {
if entry, ok := s.config.GetSupervisord(); ok {
value := uint64(entry.GetInt(resourceName, 0))
if value > 0 {
return value, nil
} else {
return 0, fmt.Errorf("No such key %s", resourceName)
}
} else {
return 0, fmt.Errorf("No supervisord section")
}

}
func (s *Supervisor) checkMinLimit(resource int, resourceName string, minRequiredSource uint64) error {
if runtime.GOOS == "windows" {
return nil
}

var limit syscall.Rlimit

if syscall.Getrlimit(resource, &limit) != nil {
return fmt.Errorf("fail to get the %s limit", resourceName)
}

if minRequiredSource > limit.Max {
return fmt.Errorf("%s %d is greater than Hard limit %d", resourceName, minRequiredSource, limit.Max)
}

if limit.Cur >= minRequiredSource {
return nil
}

limit.Cur = limit.Max
if syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit) != nil {
return fmt.Errorf(fmt.Sprintf("fail to set the %s to %d", resourceName, limit.Cur))
}
return nil
}

func (s *Supervisor) checkRequiredResources() error {
if minfds, vErr := s.getMinRequiredRes("minfds"); vErr == nil {
return s.checkMinLimit(syscall.RLIMIT_NOFILE, "NOFILE", minfds)
}
if minprocs, vErr := s.getMinRequiredRes("minprocs"); vErr == nil {
//RPROC = 6
return s.checkMinLimit(6, "NPROC", minprocs)
}
return nil

}
func (s *Supervisor) setSupervisordInfo() {
supervisordConf, ok := s.config.GetSupervisord()
if ok {
Expand Down

0 comments on commit 608fedb

Please sign in to comment.