From 55e22afe32ba5c4310bc76102800beeaf74b93c2 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Tue, 4 Mar 2014 11:46:05 +0100 Subject: [PATCH] Added configration for maxfd and automatically use the numer of cpus for GOMAXPROCS per default. --- debian/changelog | 4 ++- server.conf.in | 1 + spreed-speakfreely-server | 2 +- src/app/spreed-speakfreely-server/main.go | 37 +++++++++++++++-------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/debian/changelog b/debian/changelog index 25892310..619ae1a2 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,8 +2,10 @@ spreed-speakfreely-server (0.15.1) precise; urgency=low * Changed Makefile to allow tarball and release builds with local third party sources in ./vendor too. + * Added configration for maxfd and automatically use the + numer of cpus for GOMAXPROCS per default. - -- Simon Eisenmann Tue, 04 Mar 2014 11:10:31 +0100 + -- Simon Eisenmann Tue, 04 Mar 2014 11:45:29 +0100 spreed-speakfreely-server (0.15.0) precise; urgency=low diff --git a/server.conf.in b/server.conf.in index 5e054a34..fd3bb9f2 100644 --- a/server.conf.in +++ b/server.conf.in @@ -6,6 +6,7 @@ listen = 127.0.0.1:8080 #readtimeout = 10 #writetimeout = 10 #basePath = /some/sub/path/ # Set this when running behind a web server under a sub path. +#maxfd = 32768 # Try to set max open files limit on start (works only when run as root). [app] #title = Spreed Speak Freely diff --git a/spreed-speakfreely-server b/spreed-speakfreely-server index 1c0f5989..678774b1 100755 --- a/spreed-speakfreely-server +++ b/spreed-speakfreely-server @@ -1,2 +1,2 @@ #!/bin/bash -GOMAXPROCS=8 exec ./bin/spreed-speakfreely-server $* +exec ./bin/spreed-speakfreely-server $* diff --git a/src/app/spreed-speakfreely-server/main.go b/src/app/spreed-speakfreely-server/main.go index 0a1ad15b..b7ad91ee 100644 --- a/src/app/spreed-speakfreely-server/main.go +++ b/src/app/spreed-speakfreely-server/main.go @@ -32,15 +32,12 @@ import ( "net/http" "os" "path" + goruntime "runtime" "strings" "syscall" "time" ) -const ( - RLIMIT_NO_FILE = 32768 -) - var version = "unreleased" var defaultConfig = "./server.conf" @@ -256,19 +253,33 @@ func runner(runtime phoenix.Runtime) error { // Create our hub instance. hub := NewHub(runtimeVersion, config, sessionSecret, turnSecret) - // Try to increase number of file open files. This only works as root. + // Set number of go routines if it is 1 + if goruntime.GOMAXPROCS(0) == 1 { + nCPU := goruntime.NumCPU() + goruntime.GOMAXPROCS(nCPU) + log.Printf("Using the number of CPU's (%d) as GOMAXPROCS\n", nCPU) + } + + // Get current number of max open files. var rLimit syscall.Rlimit err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { - log.Println("Error getting rlimit numer of files", err) - } - rLimit.Max = RLIMIT_NO_FILE - rLimit.Cur = RLIMIT_NO_FILE - err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - log.Println("Error setting rlimit", err) + log.Println("Error getting max numer of open files", err) } else { - log.Printf("Set rlimit successfully to %d\n", RLIMIT_NO_FILE) + log.Printf("Max open files are %d\n", rLimit.Max) + } + + // Try to increase number of file open files. This only works as root. + maxfd, err := runtime.GetInt("http", "maxfd") + if err == nil { + rLimit.Max = uint64(maxfd) + rLimit.Cur = uint64(maxfd) + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + log.Println("Error setting max open files", err) + } else { + log.Printf("Set max open files successfully to %d\n", uint64(maxfd)) + } } // Create router.