-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathprefork.go
More file actions
130 lines (111 loc) · 3.34 KB
/
prefork.go
File metadata and controls
130 lines (111 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package fiber
import (
"crypto/tls"
"fmt"
"net"
"os"
"os/exec"
"runtime"
"sync/atomic"
"github.com/valyala/fasthttp/prefork"
)
// Test seams for prefork testing - allows injecting dummy commands
var (
testPreforkMaster = false
testOnPrefork = false
dummyPid = 1
dummyChildCmd atomic.Value
)
// IsChild determines if the current process is a child of Prefork
func IsChild() bool {
return prefork.IsChild()
}
// prefork manages child processes to make use of the OS REUSEPORT feature.
// It delegates to fasthttp's prefork package to avoid duplicating process management logic.
func (app *App) prefork(addr string, tlsConfig *tls.Config, cfg *ListenConfig) error {
if cfg == nil {
cfg = &ListenConfig{}
}
// Determine RecoverThreshold
recoverThreshold := cfg.PreforkRecoverThreshold
if recoverThreshold == 0 {
recoverThreshold = max(1, runtime.GOMAXPROCS(0)/2)
}
// Use configured logger or default to Fiber's log package
var logger prefork.Logger
if cfg.PreforkLogger != nil {
logger = cfg.PreforkLogger
} else {
logger = preforkLogger{}
}
p := &prefork.Prefork{
Network: cfg.ListenerNetwork,
Reuseport: true,
RecoverThreshold: recoverThreshold,
Logger: logger,
OnMasterDeath: func() { os.Exit(1) }, //nolint:revive // Exiting child process is intentional
}
// Use test command producer if in test mode
if testPreforkMaster {
p.CommandProducer = func(_ []*os.File) (*exec.Cmd, error) {
cmd := dummyCmd()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return cmd, fmt.Errorf("prefork: failed to start test command: %w", err)
}
return cmd, nil
}
}
// Child process: serve function wraps TLS, starts up process, etc.
p.ServeFunc = func(ln net.Listener) error {
// wrap a tls config around the listener if provided
if tlsConfig != nil {
ln = tls.NewListener(ln, tlsConfig)
}
// prepare the server for the start
app.startupProcess()
if cfg.ListenerAddrFunc != nil {
cfg.ListenerAddrFunc(ln.Addr())
}
// listen for incoming connections
return app.server.Serve(ln)
}
// Master callback: child spawned → execute OnFork hooks
p.OnChildSpawn = func(pid int) error {
if app.hooks != nil {
if testOnPrefork {
app.hooks.executeOnForkHooks(dummyPid)
} else {
app.hooks.executeOnForkHooks(pid)
}
}
return nil
}
// Master callback: all children spawned → startup message & OnListen hooks
p.OnMasterReady = func(childPIDs []int) error {
listenData := app.prepareListenData(addr, tlsConfig != nil, cfg, childPIDs)
app.runOnListenHooks(listenData)
app.printMessages(cfg, listenData)
return nil
}
// Master callback: child recovered after crash
p.OnChildRecover = func(oldPID, newPID int) {
logger.Printf("prefork: child %d crashed, recovered with new PID %d", oldPID, newPID)
}
if err := p.ListenAndServe(addr); err != nil {
return fmt.Errorf("prefork: %w", err)
}
return nil
}
// dummyCmd is for internal prefork testing
func dummyCmd() *exec.Cmd {
command := "go"
if storeCommand := dummyChildCmd.Load(); storeCommand != nil && storeCommand != "" {
command = storeCommand.(string) //nolint:forcetypeassert,errcheck // We always store a string in here
}
if runtime.GOOS == windowsOS {
return exec.Command("cmd", "/C", command, "version")
}
return exec.Command(command, "version")
}