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

Fiber/fasthttp support #18

Open
pjebs opened this issue Jan 29, 2025 · 12 comments
Open

Fiber/fasthttp support #18

pjebs opened this issue Jan 29, 2025 · 12 comments
Labels
enhancement New feature or request

Comments

@pjebs
Copy link

pjebs commented Jan 29, 2025

Fasthttp is an alternative http server package that is substantially faster.

This would be great if it supports that.

@pjebs pjebs added the enhancement New feature or request label Jan 29, 2025
@pjebs
Copy link
Author

pjebs commented Jan 31, 2025

@nlepage
Copy link
Owner

nlepage commented Jan 31, 2025

Hi @pjebs

This would be a great improvement indeed.

fasthttp uses a completely different API from net/http so this would need some work.
I’m sorry to say I won’t have the time to do this before several months.

However here are some thoughts after having looked at fasthttp’s API:

@pjebs
Copy link
Author

pjebs commented Jan 31, 2025

You don't seem to be using net/http/httptest anymore. In your introduction video you used that.

@nlepage
Copy link
Owner

nlepage commented Feb 4, 2025

You don't seem to be using net/http/httptest anymore. In your introduction video you used that.

Yes, the dependency to net/http/httptest was removed to allow the compatibility with TinyGo.

@pjebs
Copy link
Author

pjebs commented Feb 4, 2025

I wonder if there was a way to run wasm server on a web worker and have something else simpler intercept the fetch on the service worker and communicate with web worker.

Web workers are better suited for long running tasks.

@gedw99
Copy link

gedw99 commented Feb 17, 2025

Maybe we should think about this in relation to Cloudflare pages too. The golang wasm for that has its own http driver too.

https://github.com/syumai/workers

Uses compiler tags ..

https://github.com/syumai/workers/blob/main/handler_js.go being the tag for wasm.

https://github.com/syumai/workers/blob/main/handler.go being the tag for normal non wasm.

—-

As far as browser web workers go yes it’s 100% possible.

https://github.com/magodo/go-wasmww/tree/main/examples/dedicated
Is a working example of this.

I need to fork and make changes for go 1.24. Will use build tags to keep it working on previous go versions

@pjebs
Copy link
Author

pjebs commented Feb 17, 2025

@gedw99 cloudflare uses standard golang net/http

@gedw99
Copy link

gedw99 commented Feb 17, 2025

@gedw99 cloudflare uses standard golang net/http

Seems its using this for requests:

https://github.com/syumai/workers/blob/main/internal/jshttp/request.go for the wasm which does import the “http” , but has a few customisations for Cloud flare.

@pjebs
Copy link
Author

pjebs commented Feb 17, 2025

That is just a function to convert JS request objects to net/http request objects and vice-versa

@nlepage
Copy link
Owner

nlepage commented Feb 17, 2025

I wonder if there was a way to run wasm server on a web worker and have something else simpler intercept the fetch on the service worker and communicate with web worker.

Web workers are better suited for long running tasks.

go-wasm-http-server’s primary goal is to hook Go’s HTTP framework to the Service Worker’s fetch events.

As I understand it, Web Workers are useful when there is the need to run CPU heavy tasks without impacting the user interface, or in other words the run loop, which is slightly different.

HTTP requests may trigger heavy tasks, but I don’t think this should fall under go-wasm-http-server’s scope, as it is not generally the case.

If one needs to run heavy tasks in a Go WASM binary, I think a better suited approach would be to run a goroutine in a Web Worker, and right some code or even a library to achieve this.

@pjebs
Copy link
Author

pjebs commented Feb 17, 2025

I wonder if there was a way to run wasm server on a web worker and have something else simpler intercept the fetch on the service worker and communicate with web worker.
Web workers are better suited for long running tasks.

go-wasm-http-server’s primary goal is to hook Go’s HTTP framework to the Service Worker’s fetch events.

As I understand it, Web Workers are useful when there is the need to run CPU heavy tasks without impacting the user interface, or in other words the run loop, which is slightly different.

HTTP requests may trigger heavy tasks, but I don’t think this should fall under go-wasm-http-server’s scope, as it is not generally the case.

If one needs to run heavy tasks in a Go WASM binary, I think a better suited approach would be to run a goroutine in a Web Worker, and right some code or even a library to achieve this.

@nlepage I was thinking more along the lines of WASM running on web worker being long-lasting and not getting shut-down or suspended like service workers are prone to: https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle and mswjs/msw#2115

I actually got it to work as a proof-of-concept. You need 2 MessageChannel to pass data from the Service Worker <---> Main Thread <---> Web Worker. It runs smoothly.

Unfortunately there is a limit on the type of data that can pass those boundaries. You can't send a JS Request or Response object past those boundaries because they are either not "transportable" or don't satisfy the structuredClone algorithm. But I just created my own JS object with the url, method, body and headers. Not a big deal. Same deal for Response.

In your code, since the WASM server and Service worker are within the same context, the server can stream data etc very easily. That would be much harder to do when the WASM server is not in the same context as the service worker.

It can be done however: https://github.com/whatwg/streams/blob/main/transferable-streams-explainer.md and https://github.com/MattiasBuelens/remote-web-streams

@pjebs
Copy link
Author

pjebs commented Feb 18, 2025

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

No branches or pull requests

3 participants