Documentation
¶
Overview ¶
Package prefork provides a way to prefork a fasthttp server.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOverRecovery is returned when the times of starting over child prefork processes exceed // the threshold. ErrOverRecovery = errors.New("exceeding the value of RecoverThreshold") // ErrOnlyReuseportOnWindows is returned when Reuseport is false. ErrOnlyReuseportOnWindows = errors.New("windows only supports Reuseport = true") )
Functions ¶
Types ¶
type Logger ¶
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...any)
}
Logger is used for logging formatted messages.
type Prefork ¶
type Prefork struct {
// By default standard logger from log package is used.
Logger Logger
ServeFunc func(ln net.Listener) error
ServeTLSFunc func(ln net.Listener, certFile, keyFile string) error
ServeTLSEmbedFunc func(ln net.Listener, certData, keyData []byte) error
// The network must be "tcp", "tcp4" or "tcp6".
//
// By default is "tcp4"
Network string
// Child prefork processes may exit with failure and will be started over until the times reach
// the value of RecoverThreshold, then it will return and terminate the server.
RecoverThreshold int
// Flag to use a listener with reuseport, if not a file Listener will be used
// See: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
//
// It's disabled by default
Reuseport bool
// OnMasterDeath, when non-nil, enables monitoring of the master process
// in child processes. If the master process dies unexpectedly, this
// callback is invoked. This allows custom cleanup before shutdown.
//
// It is recommended to set this to func() { os.Exit(1) } if no custom
// cleanup is needed.
OnMasterDeath func()
// OnChildSpawn is called in the master process whenever a new child process is spawned.
// It receives the PID of the newly spawned child process.
//
// If this callback returns an error, all child processes are killed and the
// prefork operation returns that error.
OnChildSpawn func(pid int) error
// OnMasterReady is called in the master process after all child processes have been spawned.
// It receives a slice of all child process PIDs.
//
// If this callback returns an error, the prefork operation will be aborted.
OnMasterReady func(childPIDs []int) error
// OnChildRecover is called in the master process when a crashed child process
// has been replaced by a new one. It receives the PID of the old (crashed)
// process and the PID of the newly spawned replacement.
OnChildRecover func(oldPid, newPid int)
// CommandProducer creates and starts a child process command.
// If nil, the default implementation re-executes the current binary
// with FASTHTTP_PREFORK_CHILD=1 in the environment, stdout/stderr
// inherited from the parent, and the given files as ExtraFiles.
//
// A custom producer must:
// - Set FASTHTTP_PREFORK_CHILD=1 in the child's environment
// (otherwise IsChild() returns false and the child won't serve)
// - Call cmd.Start() before returning (the returned command must be started)
// - Pass the provided files as cmd.ExtraFiles when Reuseport is false
//
// This is primarily useful for testing (injecting dummy commands)
// or for frameworks that need custom child process setup.
CommandProducer func(files []*os.File) (*exec.Cmd, error)
// contains filtered or unexported fields
}
Prefork implements fasthttp server prefork.
Preforks master process (with all cores) between several child processes increases performance significantly, because Go doesn't have to share and manage memory between cores.
WARNING: using prefork prevents the use of any global state! Things like in-memory caches won't work.
func (*Prefork) ListenAndServe ¶
ListenAndServe serves HTTP requests from the given TCP addr.
func (*Prefork) ListenAndServeTLS ¶
ListenAndServeTLS serves HTTPS requests from the given TCP addr.
certKey is the path to the TLS private key file. certFile is the path to the TLS certificate file.
Note: parameter order is (addr, certKey, certFile) — key before cert. Internally forwards to ServeTLSFunc as (certFile, certKey).