本文整理汇总了Golang中syscall.Getrlimit函数的典型用法代码示例。如果您正苦于以下问题:Golang Getrlimit函数的具体用法?Golang Getrlimit怎么用?Golang Getrlimit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getrlimit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleStats
func (bp *Backplane) handleStats(w http.ResponseWriter, req *http.Request) {
tr := trace.New("backend.internalstats", req.RequestURI)
defer tr.Finish()
hostname, err := os.Hostname()
if err != nil {
glog.Error("Unable to obtain hostname: ", err)
tr.LazyPrintf("Unable to obtain hostname: ", err)
}
var data = struct {
Backends []*Backend
Frontends []*Frontend
Pid int
Hostname string
Uptime time.Duration
LimitAs, LimitFsize, LimitNofile syscall.Rlimit
}{
Backends: bp.Backends,
Frontends: bp.Frontends,
Pid: os.Getpid(),
Hostname: hostname,
Uptime: time.Since(starttime),
}
syscall.Getrlimit(syscall.RLIMIT_AS, &data.LimitAs)
syscall.Getrlimit(syscall.RLIMIT_FSIZE, &data.LimitFsize)
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &data.LimitNofile)
err = StatsTemplate().Execute(w, &data)
if err != nil {
glog.Errorf("unable to execute template: %s", err)
tr.LazyPrintf("unable to execute template: %s", err)
}
}
示例2: MaximizeOpenFileLimit
// 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
}
示例3: main
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)
}
示例4: TestRlimit
func TestRlimit(t *testing.T) {
var rlimit, zero syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
if err != nil {
t.Fatalf("Getrlimit: save failed: %v", err)
}
if zero == rlimit {
t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
}
set := rlimit
set.Cur = set.Max - 1
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set)
if err != nil {
t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
}
var get syscall.Rlimit
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get)
if err != nil {
t.Fatalf("Getrlimit: get failed: %v", err)
}
set = rlimit
set.Cur = set.Max - 1
if set != get {
t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
}
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
if err != nil {
t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
}
}
示例5: main
func main() {
lim := &syscall.Rlimit{}
syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
lim.Cur = 15
syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim)
fmt.Println(lim)
var s []*os.File
for i := 0; i < int(lim.Max+10); i++ {
f, err := os.Open("/tmp/whj.txt")
fmt.Println(f, err)
s = append(s, f)
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
fmt.Println(lim, err)
}
err := syscall.Getrlimit(syscall.RLIMIT_CORE, lim)
fmt.Println(lim, err)
rusage := &syscall.Rusage{}
err = syscall.Getrusage(0, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(1, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(2, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(3, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(4, rusage)
fmt.Println(rusage, err)
}
示例6: fdlimit
func fdlimit() int {
var nofile syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &nofile); err != nil {
return 0
}
return int(nofile.Cur)
}
示例7: getFdLimit
func getFdLimit() uint64 {
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
panic(err)
}
return rlim.Max
}
示例8: server_func
func server_func(port *string) {
// check for max_allowed ?
var rlimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
if err != nil {
log.Fatal("syscall.Getrlimit | ", err)
}
// if server run the server
l := start_listener(*port)
defer l.Close()
log.Println("the listener started on port " + *port)
// now we need to accept the comming req
// but B4 that we need to check for openfd's
for {
// refresh the number of openfd's
nofile := check_open_fd()
// log.Printf("the new number of openfd's %d\n", nofile)
// check them ?
if rlimit.Cur > nofile {
// if no problem accept the new connection
c, err := l.Accept()
if err != nil {
log.Fatal("l.Accept ", err)
}
// defer c.Close()
go handleConn(c)
}
}
}
示例9: maxOpenFiles
func maxOpenFiles() int {
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return defaultMaxOpenFiles
}
return int(rlim.Cur)
}
示例10: Attach
func (fs *Fs) Attach() error {
fs.fidLock.Lock()
defer fs.fidLock.Unlock()
// Load the root.
var err error
fs.root, err = fs.lookup("/")
if err != nil {
return err
}
if fs.Fdlimit == 0 {
// Figure out our active limit (1/2 open limit).
// We use 1/2 because control connections, tap devices,
// disks, etc. all need file descriptors. Note that we
// also explicitly handle running out of file descriptors,
// but this gives us an open bound to leave room for the
// rest of the system (because pieces don't always handle
// an EMFILE or ENFILE appropriately).
var rlim syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
return err
}
fs.Fdlimit = uint(rlim.Cur) / 2
}
return nil
}
示例11: maxFD
func maxFD() (uint64, error) {
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return 0, fmt.Errorf("ulimit error: %v", err)
}
return rlim.Cur, nil
}
示例12: main
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))
}
示例13: newHerd
func newHerd(imageServerAddress string, logger *log.Logger) *Herd {
var herd Herd
herd.imageServerAddress = imageServerAddress
herd.logger = logger
herd.subsByName = make(map[string]*Sub)
herd.imagesByName = make(map[string]*image.Image)
herd.missingImages = make(map[string]time.Time)
// Limit concurrent connection attempts so that the file descriptor limit is
// not exceeded.
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
panic(err)
}
maxConnAttempts := rlim.Cur - 50
maxConnAttempts = (maxConnAttempts / 100)
if maxConnAttempts < 1 {
maxConnAttempts = 1
} else {
maxConnAttempts *= 100
}
herd.connectionSemaphore = make(chan bool, maxConnAttempts)
herd.pollSemaphore = make(chan bool, runtime.NumCPU()*2)
herd.currentScanStartTime = time.Now()
return &herd
}
示例14: SetMaxFileDescriptors
// 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
}
示例15: checkAndSetUlimit
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
}