Prefork Example
This project demonstrates how to use the Prefork feature in a Go application using the Fiber framework. Preforking can improve performance by utilizing multiple CPU cores.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/prefork -
Install dependencies:
go get
Running the Application
- Start the application:
go run main.go
Example
Here is an example of how to set up the Prefork feature in a Fiber application:
package main
import (
"fmt"
"log"
"os"
"github.com/gofiber/fiber/v3"
)
func main() {
// Print current process
if fiber.IsChild() {
fmt.Printf("[%d] Child\n", os.Getppid())
} else {
fmt.Printf("[%d] Master\n", os.Getppid())
}
// Fiber instance
app := fiber.New()
// Routes
app.Get("/", hello)
// Start server with prefork enabled
log.Fatal(app.Listen(":3000", fiber.ListenConfig{EnablePrefork: true}))
}
// Handler
func hello(c fiber.Ctx) error {
return c.SendString("Hello, World!")
}