From 2df4b4d3f1bb8d425149a453b3d704086bd03d96 Mon Sep 17 00:00:00 2001 From: Nicolas JUHEL Date: Thu, 18 Jun 2020 14:03:44 +0200 Subject: [PATCH 1/2] Add C code + Header C + CGo + Go lib to change rLimit of NOFILE in Windows OD --- njs-ioutils/fileDescriptor_ko.go | 32 +++++++++++++++++++++++++++++-- njs-ioutils/maxstdio/maxstdio.c | 10 ++++++++++ njs-ioutils/maxstdio/maxstdio.go | 16 ++++++++++++++++ njs-ioutils/maxstdio/maxstdio.h | 5 +++++ njs-ioutils/maxstdio/maxstdio.o | Bin 0 -> 1600 bytes test/test-win-max-nofile/main.go | 26 +++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 njs-ioutils/maxstdio/maxstdio.c create mode 100644 njs-ioutils/maxstdio/maxstdio.go create mode 100644 njs-ioutils/maxstdio/maxstdio.h create mode 100644 njs-ioutils/maxstdio/maxstdio.o create mode 100644 test/test-win-max-nofile/main.go diff --git a/njs-ioutils/fileDescriptor_ko.go b/njs-ioutils/fileDescriptor_ko.go index 088334be..143d39c0 100644 --- a/njs-ioutils/fileDescriptor_ko.go +++ b/njs-ioutils/fileDescriptor_ko.go @@ -27,8 +27,36 @@ package njs_ioutils -import "fmt" +import ( + "github.com/nabbar/golib/njs-ioutils/maxstdio" +) + +/* + * install package gcc-multilib gcc-mingw-w64 + * - i686-w64-mingw32 for 32-bit Windows; + * - x86_64-w64-mingw32 for 64-bit Windows. + * call go build with env var : + * - CC=i686-w64-mingw32-gcc for win32 + * - CC=x86_64-w64-mingw32-gcc for win64 + * build : + * all : cd /vendor/github/nabbar/golib/njs-ioutils/maxstdio && CC=x86_64-w64-mingw32 gcc -c maxstdio.c + * win64 : cd && CC=/usr/bin/x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v . + * win32 : cd && CC=/usr/bin/i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v . + */ func systemFileDescriptor(newValue int) (current int, max int, err error) { - return 0, 0, fmt.Errorf("rLimit is nor implemented in current system") + rLimit := maxstdio.GetMaxStdio() + + if rLimit < 0 { + // default windows value + rLimit = 512 + } + + if newValue > rLimit { + rLimit = int(maxstdio.SetMaxStdio(newValue)) + return SystemFileDescriptor(0) + } + + return rLimit, rLimit, nil + // return 0, 0, fmt.Errorf("rLimit is nor implemented in current system") } diff --git a/njs-ioutils/maxstdio/maxstdio.c b/njs-ioutils/maxstdio/maxstdio.c new file mode 100644 index 00000000..c723c1aa --- /dev/null +++ b/njs-ioutils/maxstdio/maxstdio.c @@ -0,0 +1,10 @@ +#include "maxstdio.h" +#include + +int CGetMaxSTDIO() { + return _getmaxstdio(); +} + +int CSetMaxSTDIO(int new_max) { + return _setmaxstdio(new_max); +} diff --git a/njs-ioutils/maxstdio/maxstdio.go b/njs-ioutils/maxstdio/maxstdio.go new file mode 100644 index 00000000..bdc82bfb --- /dev/null +++ b/njs-ioutils/maxstdio/maxstdio.go @@ -0,0 +1,16 @@ +// +build windows cgo + +package maxstdio + +// #cgo CFLAGS: -g -Wall +// #include +// #include "maxstdio.h" +import "C" + +func GetMaxStdio() int { + return int(C.CGetMaxSTDIO()) +} + +func SetMaxStdio(newMax int) int { + return int(C.CSetMaxSTDIO(C.int(newMax))) +} diff --git a/njs-ioutils/maxstdio/maxstdio.h b/njs-ioutils/maxstdio/maxstdio.h new file mode 100644 index 00000000..9b85e424 --- /dev/null +++ b/njs-ioutils/maxstdio/maxstdio.h @@ -0,0 +1,5 @@ +#ifndef _MAXSTDIO_H +#define _MAXSTDIO_H +int CGetMaxSTDIO(); +int CSetMaxSTDIO(int new_max); +#endif \ No newline at end of file diff --git a/njs-ioutils/maxstdio/maxstdio.o b/njs-ioutils/maxstdio/maxstdio.o new file mode 100644 index 0000000000000000000000000000000000000000..86f1d11fbb66d5418c64648a26a4ab88b183d8d3 GIT binary patch literal 1600 zcmbtTO=}ZT6upyZ>qnF*E|j{+N-EfA1`3uUGHFcGYBXRHKNd1f(y=Xe63Dzj6#M`| z2yR^ZGhDjwLi`tQ1Q#xJAE@`to0mQ&lZ{@Ockey-p8MWAGxMfWuVyTZv{`hEjxvoB zIawo)LWgp6n%bM=A77CF#Pak2`R2P{`SGjChstF9^{DpkfNJIPdSSWU8TRC`a6_z$ z;)*?tGW)f?CW@=VzN&D#fN^Zr%LinQ^43yr@x(_y*k#7>!17p1XY=nfJ^vV9bak&m78XNXjF(!@qREUhIn$Q>K> zi$HoE_Hv;7u8s!&z9*zVk|ef0>5=G!A&G832>c#ny}tBC?S6YDlwS8aiKyM*b)OBq zz}Nf!uP^Wbe0#|+!(j||O}Xis{5O}l0*#z$hYH?82)3efbdo)|_(xzDl_Tg3JozQ> z$MvL3?vL|f=D+f7=3=HkCx?;g{pa0ZpJSc7bsSS8_sL9*IN96a8n9TdX{Y8to+2vH z`f2zM2Wk3$PSGEKx#nyA)Aq*f-&okz;sfK~05{3UdrfN35G}|1KSW|!^8V+zmd5`c L^Pd=8gh~Ei>uG*Z literal 0 HcmV?d00001 diff --git a/test/test-win-max-nofile/main.go b/test/test-win-max-nofile/main.go new file mode 100644 index 00000000..e3ff5684 --- /dev/null +++ b/test/test-win-max-nofile/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "os" + + njs_ioutils "github.com/nabbar/golib/njs-ioutils" +) + +func main() { + println("test to print Max STDIO NOFILE capabilities !!") + c, _, e := njs_ioutils.SystemFileDescriptor(0) + println(fmt.Sprintf("Actual limit is : %v | err : %v", c, e)) + + if e != nil { + os.Exit(1) + } + + println("test to Change Max STDIO NOFILE capabilities !!") + c, _, e = njs_ioutils.SystemFileDescriptor(c + 512) + println(fmt.Sprintf("New limit is : %v | err : %v", c, e)) + + if e != nil { + os.Exit(1) + } +} From 5757ea66a7a29a57844a830652706d700cc74ae3 Mon Sep 17 00:00:00 2001 From: Nicolas JUHEL Date: Thu, 18 Jun 2020 14:29:48 +0200 Subject: [PATCH 2/2] Update comment, include build note in function description --- njs-ioutils/fileDescriptor.go | 19 +++++++++++++++++++ njs-ioutils/fileDescriptor_ko.go | 13 ------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/njs-ioutils/fileDescriptor.go b/njs-ioutils/fileDescriptor.go index eecf0a42..3fd44fc2 100644 --- a/njs-ioutils/fileDescriptor.go +++ b/njs-ioutils/fileDescriptor.go @@ -29,6 +29,25 @@ package njs_ioutils * SystemFileDescriptor is returning current Limit & max system limit for file descriptor (open file or I/O resource) currently set in the system * This function return the current setting (current number of file descriptor and the max value) if the newValue given is zero * Otherwise if the newValue is more than the current system limit, try to change the current limit in the system for this process only + * + * For Windows build, please follow this note : + * 1) install package gcc-multilib gcc-mingw-w64 to build C source with GCC + * you will having this binaries + * - i686-w64-mingw32* for 32-bit Windows; + * - x86_64-w64-mingw32* for 64-bit Windows. + * locate you binaries gcc mingw path and note it: + * - win32 : updatedb && locate i686-w64-mingw32-gcc + * - win64 : updatedb && locate x86_64-w64-mingw32-gcc + * 2) if you have an error in the build, or if the .o object file is not present in golib/njg-ioutils/maxstdio/, run this step + * - go to golib/njg-ioutils/maxstdio folder + * - gcc -c maxstdio.c + * 3) for Win32 use this env var in prefix of your go build command (recommend to use -a flag) : + * CC=/usr/bin/i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v ... + * 4) for win64 use this env var in prefix of your go build command (recommend to use -a flag) : + * CC=/usr/bin/x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v ... + * + * Normally no problem will be result in the build + * */ func SystemFileDescriptor(newValue int) (current int, max int, err error) { return systemFileDescriptor(newValue) diff --git a/njs-ioutils/fileDescriptor_ko.go b/njs-ioutils/fileDescriptor_ko.go index 143d39c0..b4f1f1a0 100644 --- a/njs-ioutils/fileDescriptor_ko.go +++ b/njs-ioutils/fileDescriptor_ko.go @@ -31,19 +31,6 @@ import ( "github.com/nabbar/golib/njs-ioutils/maxstdio" ) -/* - * install package gcc-multilib gcc-mingw-w64 - * - i686-w64-mingw32 for 32-bit Windows; - * - x86_64-w64-mingw32 for 64-bit Windows. - * call go build with env var : - * - CC=i686-w64-mingw32-gcc for win32 - * - CC=x86_64-w64-mingw32-gcc for win64 - * build : - * all : cd /vendor/github/nabbar/golib/njs-ioutils/maxstdio && CC=x86_64-w64-mingw32 gcc -c maxstdio.c - * win64 : cd && CC=/usr/bin/x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v . - * win32 : cd && CC=/usr/bin/i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v . - */ - func systemFileDescriptor(newValue int) (current int, max int, err error) { rLimit := maxstdio.GetMaxStdio()