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(webhook): change the way to get webhook handler #743

Merged
merged 8 commits into from
Jan 20, 2025

Conversation

shevernitskiy
Copy link
Contributor

@shevernitskiy shevernitskiy commented Jan 16, 2025

PR aiming to close #695 and #696

I'm not good at typing, so I'm requesting help. There are couple "as" in a commit :)

I created a factory of factories to dynamically generate object with creators of callback handlers. I do not know how to document every object property in this case. If it is not necessary, we can just describe the main object.

Regarding the "return" type, I decided to rename it to "ignore" because it does not make any calls and leads to a safe logic branch. The user can pass an empty callback as an argument, and it will work pretty much the same, but with an unnecessary extra call.

So, if we want to slim down and simplify the API, we need to remove "ignore." If we want to gain a nano performance benefit, we should keep it.

@@ -390,14 +386,14 @@ const hono: HonoAdapter = (c) => {
update: c.req.json(),
header: c.req.header(SECRET_HEADER),
end: () => {
resolveResponse(c.body(null));
resolveResponse(c.body(""));
Copy link
Member

Choose a reason for hiding this comment

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

What does this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copy hono part from main branch to fix broken test

@shevernitskiy
Copy link
Contributor Author

shevernitskiy commented Jan 17, 2025

I realized, that in my implementation we have runtime iteration, so i decided to measure perf.

const bot = new Bot("fake-token", { me: {} as unknown as UserFromGetMe });
Deno.bench(function callback() {
    const handler = webhookCallback(bot, "stdHttp", "ignore", 10000, "qweqwe");
});

Deno.bench(function adapters() {
    const handler = webhookAdapters.stdHttp(bot);
});
benchmark   time/iter (avg)        iter/s      (min … max)           p75      p99     p995    
----------- ----------------------------- --------------------- --------------------------    
callback            30.1 ns    33,220,000 ( 24.6 ns … 197.3 ns)  28.2 ns  64.5 ns  89.0 ns
adapters            36.4 ns    27,470,000 ( 30.7 ns … 263.8 ns)  33.9 ns  94.1 ns  99.3 ns

@winstxnhdw
Copy link
Member

Why not just unroll the loop? It doesn’t seem to require a runtime.

@shevernitskiy
Copy link
Contributor Author

Why not just unroll the loop? It doesn’t seem to require a runtime.

You mean make static object with all adapters manually? Sure I can do if it your request to change. My initial intention was more generic solution.

@winstxnhdw
Copy link
Member

winstxnhdw commented Jan 19, 2025

Someone who has to add support for a new adapter will have to create the new object anyways. A little more wouldn’t hurt. Also, CPU time is precious on a lambda.

@shevernitskiy
Copy link
Contributor Author

Someone who has to add support for a new adapter will have to create the new object anyways. A little more wouldn’t hurt. Also, CPU time is precious on a lambda.

Got it, will commit in couple hours

@shevernitskiy
Copy link
Contributor Author

Ok, now we have ability to make an example for each adapter. I leave as todo for now.

@winstxnhdw
Copy link
Member

Since we are changing the API entirely, could we have a proper onTimeout argument? Currently, we are checking if it is a function or object. We can do away with that if we just have another argument for its own type.

@shevernitskiy
Copy link
Contributor Author

Since we are changing the API entirely, could we have a proper onTimeout argument? Currently, we are checking if it is a function or object. We can do away with that if we just have another argument for its own type.

I though about that. It will be simple for use and user just have options object as an argument.
Then we can drop overloading thing.

@shevernitskiy
Copy link
Contributor Author

shevernitskiy commented Jan 20, 2025

it looks not so complicated now:)

Copy link
Member

@KnorpelSenf KnorpelSenf left a comment

Choose a reason for hiding this comment

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

Rubber-stamp LGTM. We can fix things later if we discover oddities. Long live the liberty of breaking changes!

@winstxnhdw
Copy link
Member

Rubber-stamp LGTM. We can fix things later if we discover oddities. Long live the liberty of breaking changes!

Can we still make breaking changes?

@KnorpelSenf
Copy link
Member

KnorpelSenf commented Jan 20, 2025

We can make breaking changes until v2 is released. This will only happen when we no longer want to make breaking changes.

@winstxnhdw
Copy link
Member

Nice! I am still not too happy with this API yet. I'll think of something.

@KnorpelSenf
Copy link
Member

Looking forward to that!

@shevernitskiy shevernitskiy deleted the refactor-webhook-handlers branch January 29, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants