Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”₯ Add OnPrefork Hooks so you can get the PID of the child process. #1974

Merged
merged 1 commit into from Aug 1, 2022

Conversation

tohutohu
Copy link
Contributor

Add Hooks that can get the PID of a child process forked from the master process.
Currently, in the Prefork mode of fiber, the PID of the child process forked from the master process cannot be obtained, so CPU Affinity cannot be set automatically using commands such as taskset.
To maximize performance in Prefork mode, each child process should be pinned to a specific CPU core.
To make this possible, I will add a new Hooks.

Usage

func main() {
	app := fiber.New(fiber.Config{
		Prefork: true,
	})

	app.Hooks().OnFork(func(pid int) error {
		fmt.Printf("Forked child process %d\n", pid)
		return nil
	})

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World πŸ‘‹!")
	})

	app.Listen(":3000")
}

output

Forked child process 18331
Forked child process 18332
Forked child process 18333
Forked child process 18334
Forked child process 18336
Forked child process 18338
Forked child process 18341
Forked child process 18350
Forked child process 18355
Forked child process 18365
Forked child process 18383
Forked child process 18388

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚                   Fiber v2.35.0                   β”‚  β”‚ Child PIDs ... 18331, 18332, 18333, 18334, 18336  β”‚
 β”‚               http://127.0.0.1:3000               β”‚  β”‚ 18338, 18341, 18350, 18355, 18365, 18383, 18388   β”‚
 β”‚       (bound on host 0.0.0.0 and port 3000)       β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 β”‚                                                   β”‚
 β”‚ Handlers ............. 2  Processes .......... 12 β”‚
 β”‚ Prefork ........ Enabled  PID ............. 18325 β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

@welcome
Copy link

welcome bot commented Jul 10, 2022

Thanks for opening this pull request! πŸŽ‰ Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@tohutohu tohutohu force-pushed the feature/add.OnPrefork.hooks branch from fa0496a to 2310639 Compare July 10, 2022 16:36
@tohutohu
Copy link
Contributor Author

The commit above is for triggering GitHub Actions again.
The previous error doesn't appear to be caused by this PR’s change, do you know why?

@efectn
Copy link
Member

efectn commented Jul 10, 2022

The commit above is for triggering GitHub Actions again. The previous error doesn't appear to be caused by this PR’s change, do you know why?

Some timeout tests are buggy.

@tohutohu
Copy link
Contributor Author

Thank you very much. It would be helpful if you could approve Workflow.

@tohutohu tohutohu closed this Jul 11, 2022
@tohutohu tohutohu reopened this Jul 11, 2022
@ReneWerner87
Copy link
Member

ReneWerner87 commented Jul 11, 2022

@tohutohu

@tohutohu
Copy link
Contributor Author

Sorry for the late reaction. I've added documentation and submitted a PR.

@ReneWerner87 ReneWerner87 merged commit 43133be into gofiber:master Aug 1, 2022
@welcome
Copy link

welcome bot commented Aug 1, 2022

Congrats on merging your first pull request! πŸŽ‰ We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants