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

refactor: restructure major crates part 1 #461

Merged
merged 9 commits into from
Dec 23, 2024
Merged

refactor: restructure major crates part 1 #461

merged 9 commits into from
Dec 23, 2024

Conversation

nyannyacha
Copy link
Collaborator

@nyannyacha nyannyacha commented Dec 17, 2024

What kind of change does this PR introduce?

Refactor

Description

This PR refactors the major crates. In part 1, the following points and aspects were considered.

  • Make the module name in the crate more clear
  • Review and rewrite the worker (user, main, event) termination routines triggered by the graceful exit routine.
  • Replace the functions and interfaces related to the creation of Worker with the builder pattern.
  • The WorkerHandler trait, which was previously added without meaning, is reimplemented to fit the purpose. (user, managed)
  • The http listener now supports shutdown.
    • The main worker automatically triggers shutdown by graceful exit, and other http middleware frameworks support shutdown through AbortController (AbortSignal).
  • Apart from the above, the codebase has been reviewed and polished overall.

Blocked-by: #458 #459

@nyannyacha nyannyacha marked this pull request as ready for review December 19, 2024 03:18
@nyannyacha nyannyacha requested a review from laktek December 19, 2024 03:30
@@ -495,6 +499,7 @@ globalThis.bootstrapSBEdge = (opts, ctx) => {
"beforeunload",
"unload",
"unhandledrejection",
"drain",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between beforeunload and drain event? When should a user function listen to drain over beforeunload?

Is this an event triggered in Deno runtime as well?

Copy link
Collaborator Author

@nyannyacha nyannyacha Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an event triggered in Deno runtime as well?

No, it's an edge-runtime specific event.

If the resource exceeds the soft limit, the worker is marked as the retired state, and the worker pool no longer relays new requests to this worker, but the request queue may still contain requests.

When all remaining requests have been processed, the drain event is triggered. This is used to safely remove the task of the http listener from the event loop. This is a different event from beforeunload.

@laktek

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected to be used by user functions? It feels more like an internal event if it only used for a specific purpose.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, basically it has a purpose to be used internally.

I could have replaced it with a direct function call instead of firing an event, but I just implemented it as an event firing, thought some users might use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned it may be confusing for users - when to use drain vs beforeunload and the order of them firing. Can an event handler on drain block the unload of the worker?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The threshold for the beforeunload trigger is specified by the cli flag. (The default is 90%)

drain indicates that there will be no more requests, and beforeunload indicates that the worker will soon be unloaded because the resource has exceeded the threshold specified by the cli flag.

Can an event handler on drain block the unload of the worker?

All events will be monitored by the supervisor to prevent cheating by user scripts.
Even if an event handler attempts to prevent termination, it will be defeated by a call to v8's terminate_execution API. This is true for all events triggered by the runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sounds good! We'll probably have to add docs later for different events to make it clear for users.

@@ -115,12 +122,15 @@ function serve(args1, args2) {

try {
for await (const requestEvent of currentHttpConn) {
ACTIVE_REQUESTS++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of tracking ACTIVER_REQUESTS? Where is it used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was used for debugging, but it seems that I didn't clear it.

I'll open another PR to erase this. (Several branches I'm working on directly depend on this PR's commits, so if I modify this PR, I will have to perform an unnecessary force push.)

@nyannyacha nyannyacha merged commit d9e0a26 into main Dec 23, 2024
3 checks passed
@nyannyacha nyannyacha deleted the refactor-dec-2 branch December 23, 2024 01:25
@nyannyacha nyannyacha added this to the Deno 2.1 LTS milestone Dec 23, 2024
Copy link

🎉 This PR is included in version 1.66.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants