Merge pull request #471 from ReneWerner87/master

🐛 flag.Parse not working when using Prefork on Fiber #469
pull/472/head
fenny 2020-06-15 15:37:29 +02:00 committed by GitHub
commit f20e66efee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 4 deletions

21
app.go
View File

@ -7,6 +7,7 @@ package fiber
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"flag"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -203,6 +204,18 @@ var (
defaultCompressedFileSuffix = ".fiber.gz" defaultCompressedFileSuffix = ".fiber.gz"
) )
var (
preforkFlag, childFlag = "-prefork", "-child"
prefork, child bool
)
func init() { //nolint:gochecknoinits
// Definition flag to not break the program when the user adds their own flags
// and runs `flag.Parse()`
flag.BoolVar(&prefork, childFlag[1:], false, "Is a child process")
flag.BoolVar(&child, preforkFlag[1:], false, "use prefork")
}
// New creates a new Fiber named instance. // New creates a new Fiber named instance.
// You can pass optional settings when creating a new instance. // You can pass optional settings when creating a new instance.
func New(settings ...*Settings) *App { func New(settings ...*Settings) *App {
@ -247,7 +260,7 @@ func New(settings ...*Settings) *App {
} }
if !app.Settings.Prefork { // Default to -prefork flag if false if !app.Settings.Prefork { // Default to -prefork flag if false
app.Settings.Prefork = utils.GetArgument("-prefork") app.Settings.Prefork = utils.GetArgument(preforkFlag)
} }
// Replace unsafe conversion functions // Replace unsafe conversion functions
if app.Settings.Immutable { if app.Settings.Immutable {
@ -407,7 +420,7 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
ln = tls.NewListener(ln, tlsconfig[0]) ln = tls.NewListener(ln, tlsconfig[0])
} }
// Print startup message // Print startup message
if !app.Settings.DisableStartupMessage && !utils.GetArgument("-child") { if !app.Settings.DisableStartupMessage && !utils.GetArgument(childFlag) {
startupMessage(ln) startupMessage(ln)
} }
@ -526,7 +539,7 @@ func (app *App) Routes() []*Route {
// Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ // Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
func (app *App) prefork(address string) (ln net.Listener, err error) { func (app *App) prefork(address string) (ln net.Listener, err error) {
// Master proc // Master proc
if !utils.GetArgument("-child") { if !utils.GetArgument(childFlag) {
addr, err := net.ResolveTCPAddr("tcp", address) addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil { if err != nil {
return ln, err return ln, err
@ -543,7 +556,7 @@ func (app *App) prefork(address string) (ln net.Listener, err error) {
childs := make([]*exec.Cmd, runtime.NumCPU()/2) childs := make([]*exec.Cmd, runtime.NumCPU()/2)
// #nosec G204 // #nosec G204
for i := range childs { for i := range childs {
childs[i] = exec.Command(os.Args[0], append(os.Args[1:], "-prefork", "-child")...) childs[i] = exec.Command(os.Args[0], append(os.Args[1:], preforkFlag, childFlag)...)
childs[i].Stdout = os.Stdout childs[i].Stdout = os.Stdout
childs[i].Stderr = os.Stderr childs[i].Stderr = os.Stderr
childs[i].ExtraFiles = files childs[i].ExtraFiles = files