refactor: custom HTTP endpoint registration#2380
Conversation
This commit introduces a new file `pkg/rpc/server/http.go` to provide a centralized location for registering custom, non-gRPC, plain HTTP endpoints. A new function `RegisterCustomHTTPEndpoints(mux *http.ServeMux)` is defined in this file. The existing `/health/live` endpoint has been moved into this function. The `NewServiceHandler` in `pkg/rpc/server/server.go` now calls `RegisterCustomHTTPEndpoints` to include these custom routes. This change helps to keep the `server.go` file cleaner and makes it easier to manage and add new custom HTTP endpoints in the future. A test file `pkg/rpc/server/http_test.go` has been added with `TestRegisterCustomHTTPEndpoints` to ensure that endpoints registered via this new structure are correctly served.
|
Warning Rate limit exceeded@tac0turtle has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 59 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (2)
WalkthroughA new HTTP GET endpoint Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant HTTPServer
participant ServiceHandler
Client->>HTTPServer: GET /health/live
HTTPServer->>ServiceHandler: Handle /health/live
ServiceHandler-->>HTTPServer: 200 OK, "OK"
HTTPServer-->>Client: 200 OK, "OK"
Assessment against linked issues
Assessment against linked issues: Out-of-scope changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
The latest Buf updates on your PR. Results from workflow CI and Release / buf-check (pull_request).
|
There was a problem hiding this comment.
Summary of Changes
Hello @tac0turtle, 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 refactors the RPC server's HTTP endpoint registration by creating a new, dedicated file for custom, non-gRPC HTTP routes. This change aims to improve code organization and maintainability within the server.go file, making it cleaner and easier to manage and add new custom HTTP endpoints in the future. The existing /health/live endpoint has been migrated to this new structure, and comprehensive tests have been added to ensure its continued functionality.
Highlights
- Centralized HTTP Endpoint Registration: A new file,
pkg/rpc/server/http.go, has been introduced to provide a dedicated and centralized location for registering custom, non-gRPC, plain HTTP endpoints. - Moved Health Endpoint: The existing
/health/liveendpoint definition has been moved frompkg/rpc/server/server.gointo the newRegisterCustomHTTPEndpointsfunction withinpkg/rpc/server/http.go. - Integration with Service Handler: The
NewServiceHandlerfunction inpkg/rpc/server/server.go(at line 241) now callsRegisterCustomHTTPEndpoints(mux)to ensure that all custom HTTP routes are properly included and served by the main HTTP server. - Enhanced Testing: A new test file,
pkg/rpc/server/http_test.go, has been added to specifically test theRegisterCustomHTTPEndpointsfunction. Additionally,pkg/rpc/server/server_test.gohas been updated (starting at line 227) withTestHealthLiveEndpointto verify the/health/liveendpoint's functionality through the full service handler.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and 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 to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
pkg/rpc/server/http.go (1)
11-15: Consider restricting HTTP methods for the health endpoint.The
/health/liveendpoint currently accepts all HTTP methods. For better REST semantics, it should only respond to GET requests.Apply this diff to restrict to GET method only:
mux.HandleFunc("/health/live", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "OK") })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
pkg/rpc/server/http.go(1 hunks)pkg/rpc/server/http_test.go(1 hunks)pkg/rpc/server/server.go(1 hunks)pkg/rpc/server/server_test.go(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: test / Run Integration Tests
- GitHub Check: test / Go Mod Tidy Check
- GitHub Check: test / Run Unit Tests
- GitHub Check: test / Build All Rollkit Binaries
- GitHub Check: test / Build Docker Image
- GitHub Check: lint / golangci-lint
- GitHub Check: lint / markdown-lint
- GitHub Check: Analyze (actions)
- GitHub Check: Analyze (go)
- GitHub Check: Summary
🔇 Additional comments (4)
pkg/rpc/server/server.go (1)
240-241: LGTM! Clean integration of centralized HTTP endpoint registration.The placement of
RegisterCustomHTTPEndpoints(mux)is ideal - after all gRPC service handlers are registered but before the h2c wrapper, ensuring custom HTTP endpoints are properly integrated into the service multiplexer.pkg/rpc/server/server_test.go (1)
227-255: Excellent integration test coverage.The test properly validates the
/health/liveendpoint through the full service handler integration, using appropriate mocks and verifying both HTTP status and response content. The test structure follows Go testing best practices.pkg/rpc/server/http_test.go (1)
12-37: Well-structured unit test for the registration function.This test effectively validates the
RegisterCustomHTTPEndpointsfunction in isolation, providing good coverage for the/health/liveendpoint behavior without requiring full service handler setup.pkg/rpc/server/http.go (1)
8-23: Excellent centralization approach with clear documentation.The function provides a clean, centralized location for custom HTTP endpoint registration. The documentation and example comments will help future developers understand how to add new endpoints properly.
There was a problem hiding this comment.
Code Review
This pull request centralizes the registration of custom HTTP endpoints, improving modularity and manageability. The changes include introducing pkg/rpc/server/http.go and the RegisterCustomHTTPEndpoints function, along with a new /health/live endpoint and associated tests. Suggestions include establishing robust error handling, clarifying mock roles in tests, and streamlining test assertions. Consider updating pkg/rpc/README.md to reflect the addition of plain HTTP endpoints.
| // RegisterCustomHTTPEndpoints is the designated place to add new, non-gRPC, plain HTTP handlers. | ||
| // Additional custom HTTP endpoints can be registered on the mux here. | ||
| func RegisterCustomHTTPEndpoints(mux *http.ServeMux) { | ||
| mux.HandleFunc("/health/live", func(w http.ResponseWriter, r *http.Request) { |
There was a problem hiding this comment.
What is the difference between this then: https://github.com/rollkit/rollkit/blob/2cc7e0b4fc6088a29381a719d80ac3286b0748ad/pkg/rpc/server/server.go#L200-L209?
There was a problem hiding this comment.
livez is a grpc/http post method, but most tooling does gets. this is a simple get method for existing devop tooling
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2380 +/- ##
==========================================
+ Coverage 71.72% 72.27% +0.54%
==========================================
Files 64 65 +1
Lines 6306 6315 +9
==========================================
+ Hits 4523 4564 +41
+ Misses 1381 1349 -32
Partials 402 402
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Overview
This commit introduces a new file
pkg/rpc/server/http.goto provide a centralized location for registering custom, non-gRPC, plain HTTP endpoints.A new function
RegisterCustomHTTPEndpoints(mux *http.ServeMux)is defined in this file. The existing/health/liveendpoint has been moved into this function. TheNewServiceHandlerinpkg/rpc/server/server.gonow callsRegisterCustomHTTPEndpointsto include these custom routes.This change helps to keep the
server.gofile cleaner and makes it easier to manage and add new custom HTTP endpoints in the future.A test file
pkg/rpc/server/http_test.gohas been added withTestRegisterCustomHTTPEndpointsto ensure that endpoints registered via this new structure are correctly served.closes #2375
Summary by CodeRabbit