golang-too_many_open_files-Workaround

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This is the system resource limit, usually single process can not exceed 1024, I use CGO to set, the code is as follows:

package main/*#include <stdio.h>#include <sys/time.h>#include <sys/resource.h>int rlimit_init() {    printf("setting rlimit\n");    struct rlimit limit;    if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {    printf("getrlimit error\n");    return 1;    }    limit.rlim_cur = limit.rlim_max = 50000;    if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {    printf("setrlimit error\n");    return 1;    }    printf("set limit ok\n");    return 0;}*/import "C"func main() {    C.rlimit_init()}

Or use the Syscall package

var rlim syscall.Rlimiterr := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)if err != nil {    fmt.Println("get rlimit error: " + err.Error())    os.Exit(1)}rlim.Cur = 50000rlim.Max = 50000err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)if err != nil {    fmt.Println("set rlimit error: " + err.Error())    os.Exit(1)}

After compiling with go build, you need to run with the root permission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.