-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[router] add WASM support for middleware #12471
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
base: main
Are you sure you want to change the base?
[router] add WASM support for middleware #12471
Conversation
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Summary of ChangesHello @tonyluj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Signed-off-by: Tony Lu <[email protected]>
2911de1 to
f06aade
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This is a comprehensive and well-structured pull request that adds significant new functionality with WASM-based middleware. The implementation covers runtime integration, a management API, security sandboxing, and helpful examples. My feedback focuses on improving robustness by handling potential panics during startup, enhancing maintainability by suggesting refactoring to reduce code duplication, and promoting safer coding practices in the example modules by avoiding unsafe code. I have also identified some potentially unused code that could be removed.
Signed-off-by: Tony Lu <[email protected]>
|
|
||
| This middleware validates API keys for requests to `/api` and `/v1` paths: | ||
|
|
||
| - Supports `Authorization: Bearer <key>` header |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api key is already supported
perhaps we can leave example in a different PR?
for example, write an example using other languages
| types::{WasmComponentInput, WasmComponentOutput}, | ||
| }; | ||
|
|
||
| pub struct WasmModuleManager { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use workflow for this?
i had similar manager for worker
and it just kept on growing eventually i had to write a simple generic workflow
perhaps wasm can leverage the same workflow
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Signed-off-by: Tony Lu <[email protected]>
Motivation
This PR implements WebAssembly-based extensibility for sgl-router, enabling dynamic, safe, and portable middleware execution without requiring router restarts or recompilation. This addresses the feature request in #10902 .
Modifications
Core Infrastructure
wasmtimeas the primary runtime with async support and WebAssembly Component Modelwit/spec.wit)/wasmfor dynamic module management:POST /wasm- Deploy modulesGET /wasm- List all modules and metricsDELETE /wasm/{uuid}- Remove a moduleMiddleware Support
OnRequestandOnResponselifecycle hooksContinue- Proceed to next middleware/upstreamReject(status)- Return error response immediatelyModify- Modify headers, body, or status codeSecurity & Resource Management
Configuration
enable_wasmflag toRouterConfigRouterConfigBuilder::enable_wasm(bool)methodExamples
Three complete example implementations:
wasm-guest-auth: API key authentication middleware
Authorizationheader orx-api-key401 Unauthorizedfor invalid/missing keyswasm-guest-logging: Request tracking and status conversion
x-request-id,x-wasm-processed, etc.)500errors to503wasm-guest-ratelimit: Rate limiting middleware
429 Too Many Requestswhen exceededMetrics
/wasmendpoint:Implementation Details
Architecture
WIT Interface
Uses WebAssembly Component Model with WIT for type-safe communication:
middleware-on-request::on-request(req: Request) -> Actionmiddleware-on-response::on-response(resp: Response) -> ActionExecution Flow
OnRequestAction(Continue/Reject/Modify)OnResponseprocess responseUsage
Enable WASM Support
Deploy a Module
List Modules
Testing
Checklist