honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.56k stars 600 forks source link

Can hono run on multiple cores? #470

Closed c608345 closed 2 years ago

c608345 commented 2 years ago

I run benchmark, but there is only one deno process.

yusukebe commented 2 years ago

Hi @c608345 !

Deno does not support multi thread. All code on Deno will run in only single threads. It's the same as Node.js.

Wondering if there is a way to use the module like the cluster, but I don't know how on Deno.

c608345 commented 2 years ago

Deno has service workers. https://deno.land/manual/runtime/workers We should have this feature in the framework.

yusukebe commented 2 years ago

Thank you.

I've tried that instruction, and Web Worker with Hono works.

But, I think Hono should not have the feature to support multiple cores. Hono is just a web framework. If the user wants to take advantage of multi-core CPUs, just he/she does write the script to kick the app by Web Worker. It is not Hono matter.

c608345 commented 2 years ago

Hono may run in cloud function environments (like deno deploy, cloudflare workers) or Servers(VPS, AWS EC2 Instances ).We can detect environments, when run in servers we support multi-core by default.

c608345 commented 2 years ago

There are many popular web frameworks support multi-core. Why not hono , what's your concern?

sifrr https://sifrr.github.io/sifrr/#/./packages/server/sifrr-server/?id=createcluster activej https://activej.io/boot/workers#basic-worker-pool-example drogon https://github.com/drogonframework/drogon#:~:text=Below%20is%20the%20main%20program%20of%20a%20typical%20drogon%20application%3A actix-web https://actix.rs/docs/server/#:~:text=.await%0A%7D-,Multi%2DThreading,-HttpServer%20automatically%20starts openswoole https://openswoole.com/docs/modules/swoole-multiprocessing

yusukebe commented 2 years ago

Hono is not for Node.js only for CF Workers, Compute@Edge, Deno, and Bun. Cloudflare Workers and Deno Deploy does not support multi thread. And Hono does not work on VPC, EC2 and others. It doesn't make sense to do it. And we can not. Hono is only composed by Web Standard API not depend on other libraries. There is no concept of multi thread.

c608345 commented 2 years ago

Service worker is web version multi-thread, deno has workers built-in support. There is no external library needed.

yusukebe commented 2 years ago

Sorry, Hono really does not support multi thread. If you are looking for a framework that supports multi-threading, look somewhere else.

phtdacosta commented 1 year ago

I am struggling HARD to get Hono to work with Web Workers, @yusukebe would you have any working example of how could it be done under Bun?

It feels like Hono goes straight to the bottom of the code and crashes, it does not not even wait for the worker to do whatever it does.

The error I get everytime:

236 |     return (async () => {
237 |       try {
238 |         const tmp = composed(c);
239 |         const context = tmp.constructor.name === "Promise" ? await tmp : tmp;
240 |         if (!context.finalized) {
241 |           throw new Error(
                    ^
error: Context is not finalized. You may forget returning Response object or `await next()`
      at /mnt/c/Users/user/Desktop/project/js-api/node_modules/hono/dist/hono-base.js:241:16

      at errorHandler (/mnt/c/Users/user/Desktop/project/js-api/node_modules/hono/dist/hono-base.js:18:2)
yusukebe commented 1 year ago

@phtdacosta

Do you write a return?

app.get('/', (c) => {
  return c.text('foo') // <---
})
phtdacosta commented 1 year ago

Yes, twice. Here is the last part of the endpoint function:

    w.postMessage({ jobs: validJobs })

    w.onmessage = async e => {
        for (const article of e.data.items) {
            feed.insertArticle(user, article)
            updates.push(article)
        }

        for (const source of e.data.failures) {
            try {
                feed.updateSource(user, source, { error: 1 })
            }
            catch (error) { console.log(error) }
        }

        return c.json(updates)
    }

    w.onerror = e => {
        console.error(e.error)
        c.status(500)
        return c.text('Unexpected error')
    }
}
yusukebe commented 1 year ago

@phtdacosta

Ah, Hono is not for Web Workers. That code never work.

phtdacosta commented 1 year ago

Since you are much more knowledgeable than me in that cause (given you are the creator of Hono), do you know any other library for Bun (or Deno, by extent) that works with Web Workers?

yusukebe commented 1 year ago

@phtdacosta

do you know any other library for Bun (or Deno, by extent) that works with Web Workers?

Hmm, I've never heard about a framework for Web Workers.

phtdacosta commented 1 year ago

I mean, not for Web Workers, but that at least works with it. I am literally translating this API from Node/tinyhttp and this code worked fine with Node/tinyhttp. Feels weird that it simply does not with Hono. But maybe in Hono's case this is actually a feature rather than a bug. In either case, thank you so much @yusukebe! Hono is a great project nonetheless.