🔥 feat: Add UNIX socket support#3535
Conversation
|
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
|
""" WalkthroughSupport for Unix Domain Sockets was added to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant FiberApp
participant ListenConfig
participant OS
User->>FiberApp: Start app with ListenConfig (ListenerNetwork="unix")
FiberApp->>ListenConfig: Read UnixSocketFileMode
FiberApp->>OS: Remove existing socket file (if any)
OS-->>FiberApp: Success or "file not exist"
FiberApp->>OS: Create Unix Domain Socket listener
FiberApp->>OS: Set socket file permissions (UnixSocketFileMode)
OS-->>FiberApp: Permissions set
FiberApp-->>User: App ready and listening on Unix socket
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (8)
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 (
|
There was a problem hiding this comment.
Summary of Changes
Hello @LaptopCat, 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 improves the robustness and configurability of Unix domain socket support within the application. It addresses common issues related to socket file persistence and provides greater control over file permissions, making the Unix socket listening process more reliable and user-friendly.
Highlights
- Enhanced Unix Socket Management: Implemented automatic removal of existing Unix domain socket files before attempting to listen on them, preventing 'address already in use' errors on restart.
- Configurable Unix Socket Permissions: Introduced a new
UnixSocketFileModeoption inListenConfigto allow users to specify the file permissions for Unix domain sockets. The default permission is0775. - Documentation Updates: Updated the
ListenConfigdocumentation to include the newUnixSocketFileModefield and clarified theListenerNetworkdescription to explicitly mention 'unix' as a supported network type.
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.
Code Review
This pull request enhances Unix domain socket support by cleaning up old sockets and allowing file mode configuration. The review focuses on improving the security of the default file mode and enhancing code maintainability by using constants for network types.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
listen.go (1)
291-314: Consider defining a constant for the "unix" network type.While the implementation is correct, using a string literal
"unix"in multiple places could benefit from a defined constant to avoid potential typos and improve maintainability.Consider adding this constant alongside the existing network constants:
const ( globalIpv4Addr = "0.0.0.0" + NetworkUnix = "unix" )Then use it in the conditional checks:
-if cfg.ListenerNetwork == "unix" { +if cfg.ListenerNetwork == NetworkUnix {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
docs/api/fiber.md(1 hunks)listen.go(5 hunks)
🔇 Additional comments (5)
docs/api/fiber.md (1)
116-117: Documentation accurately reflects the new Unix socket support.The documentation updates correctly describe the new Unix Domain Socket functionality. The
ListenerNetworkfield now properly lists"unix"as a valid option, and the newUnixSocketFileModefield is well-documented with its type and default value.listen.go (4)
75-78: Well-documented Unix socket file mode configuration.The new
UnixSocketFileModefield is properly documented and follows Go struct field conventions. The default value of0775provides reasonable permissions while remaining configurable for security-conscious deployments.
131-135: Proper default value handling for Unix socket file mode.The default configuration logic correctly initializes
UnixSocketFileModeto0o775. The octal notation is clear and the conditional check ensures the default is applied when the field is zero-valued.Also applies to: 143-145
291-296: Robust Unix socket file cleanup implementation.The pre-listen socket file removal correctly handles the common case where a previous socket file exists. The error handling properly ignores "file not exist" errors while propagating other unexpected errors. This prevents "address already in use" errors on server restart.
310-314: Correct file permission setting for Unix sockets.The implementation properly sets Unix socket file permissions after successful socket creation using
os.Chmod. The error handling provides descriptive messages with the attempted permissions and file path for debugging.
|
@LaptopCat Please add unit+tests for this functionality |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3535 +/- ##
==========================================
- Coverage 91.02% 91.00% -0.03%
==========================================
Files 110 110
Lines 10955 10989 +34
==========================================
+ Hits 9972 10000 +28
- Misses 731 735 +4
- Partials 252 254 +2
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:
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
listen_test.go(2 hunks)
🔇 Additional comments (1)
listen_test.go (1)
13-13: LGTM!The import of
path/filepathis appropriate for thefilepath.Joinfunction used in the Unix socket test case.
|
@LaptopCat Can you mark the fixes you have applied as "Resolved". Thanks! 💪 |
|
@LaptopCat can you extend the markdown ? |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
docs/whats_new.md (2)
238-238: Suggest more descriptive bullet for Unix socket support.Consider referencing the new config fields to help readers quickly locate the feature (e.g.,
ListenerNetworkandUnixSocketFileMode).For example:
- - Added Unix socket support + - Added support for Unix domain sockets via `ListenerNetwork` and `UnixSocketFileMode`
241-258: Add error handling to cleanup and listen calls.The examples omit error checks on
os.Remove,os.Chmod, andapp.Listen. Handling or explicitly ignoring errors prevents silent failures. For example:- os.Remove("app.sock") + _ = os.Remove("app.sock") // ignore if not exists - app.Hooks().OnListen(func(fiber.ListenData) error { - return os.Chmod("app.sock", 0777) - }) + app.Hooks().OnListen(func(_ fiber.ListenData) error { + return os.Chmod("app.sock", 0777) + }) - app.Listen("app.sock") + if err := app.Listen("app.sock"); err != nil { + log.Fatalf("failed to listen on unix socket: %v", err) + }And for v3:
- app.Listen("app.sock", fiber.ListenerConfig{ - ListenerNetwork: fiber.NetworkUnix, - UnixSocketFileMode: 0777, - }) + if err := app.Listen("app.sock", fiber.ListenerConfig{ + ListenerNetwork: fiber.NetworkUnix, + UnixSocketFileMode: 0777, + }); err != nil { + log.Fatalf("failed to listen on unix socket: %v", err) + }
|
@LaptopCat can you recheck the hints and then we can merge |
|
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
Description
This pull request improves support for listening on unix domain sockets:
Inspired by ListenAndServeUNIX function from fasthttp
Changes introduced
Type of change
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md