MCPcopy
hub / github.com/gofiber/fiber / prefork

Method prefork

prefork.go:30–120  ·  view source on GitHub ↗

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.

(addr string, tlsConfig *tls.Config, cfg *ListenConfig)

Source from the content-addressed store, hash-verified

28// prefork manages child processes to make use of the OS REUSEPORT feature.
29// It delegates to fasthttp's prefork package to avoid duplicating process management logic.
30func (app *App) prefork(addr string, tlsConfig *tls.Config, cfg *ListenConfig) error {
31 if cfg == nil {
32 cfg = &ListenConfig{}
33 }
34
35 // Determine RecoverThreshold
36 recoverThreshold := cfg.PreforkRecoverThreshold
37 if recoverThreshold == 0 {
38 recoverThreshold = max(1, runtime.GOMAXPROCS(0)/2)
39 }
40
41 // Use configured logger or default to Fiber's log package
42 var logger prefork.Logger
43 if cfg.PreforkLogger != nil {
44 logger = cfg.PreforkLogger
45 } else {
46 logger = preforkLogger{}
47 }
48
49 p := &prefork.Prefork{
50 Network: cfg.ListenerNetwork,
51 Reuseport: true,
52 RecoverThreshold: recoverThreshold,
53 RecoverInterval: cfg.PreforkRecoverInterval,
54 ShutdownGracePeriod: cfg.PreforkShutdownGracePeriod,
55 Logger: logger,
56 OnMasterDeath: func() { os.Exit(1) }, //nolint:revive // Exiting child process is intentional
57 }
58
59 // Use test command producer if in test mode
60 if testPreforkMaster {
61 p.CommandProducer = func(_ []*os.File) (*exec.Cmd, error) {
62 cmd := dummyCmd()
63 cmd.Stdout = os.Stdout
64 cmd.Stderr = os.Stderr
65 if err := cmd.Start(); err != nil {
66 return cmd, fmt.Errorf("prefork: failed to start test command: %w", err)
67 }
68 return cmd, nil
69 }
70 }
71
72 // Child process: serve function wraps TLS, starts up process, etc.
73 p.ServeFunc = func(ln net.Listener) error {
74 // wrap a tls config around the listener if provided
75 if tlsConfig != nil {
76 ln = tls.NewListener(ln, tlsConfig)
77 }
78
79 // prepare the server for the start
80 app.startupProcess()
81
82 if cfg.ListenerAddrFunc != nil {
83 cfg.ListenerAddrFunc(ln.Addr())
84 }
85
86 // listen for incoming connections
87 return app.server.Serve(ln)

Callers 4

ListenMethod · 0.95
Test_Hook_OnHookFunction · 0.80

Calls 10

startupProcessMethod · 0.95
prepareListenDataMethod · 0.95
runOnListenHooksMethod · 0.95
printMessagesMethod · 0.95
dummyCmdFunction · 0.85
AddrMethod · 0.80
executeOnForkHooksMethod · 0.80
StartMethod · 0.65
ErrorfMethod · 0.65
PrintfMethod · 0.65

Tested by 3

Test_Hook_OnHookFunction · 0.64