-
Notifications
You must be signed in to change notification settings - Fork 340
Replace http.Server
by a generic ListenAndServer
#200
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?
Conversation
WalkthroughThis change introduces a new interface, Changes
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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:
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
server/sse.go (1)
54-58
: Consider a more idiomatic interface name.The interface name
ListenAndServer
seems to be derived from the method nameListenAndServe
plus "Server", which is unusual in Go. Typically, interfaces are named after behaviors or roles, not method names. Consider renaming to something likeHTTPServer
or simplyServer
to better represent its role.Also, the comment could be more precise - an interface doesn't implement methods, it defines them. Consider rewording to "defines a server that can listen for requests and be shut down."
-// ListenAndServer is a type that implements ListenAndServe and Shutdown -type ListenAndServer interface { +// Server defines a server that can listen for requests and be shut down +type Server interface { ListenAndServe() error Shutdown(ctx context.Context) error }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
server/sse.go
(3 hunks)
🔇 Additional comments (3)
server/sse.go (3)
70-70
: Good abstraction through interface usage.Changing the
srv
field from a concrete*http.Server
type to the new interface type is a good application of dependency inversion. This enables greater flexibility by allowing any server implementation that satisfies the interface to be used.
140-140
: Consistent interface usage.Updating the
WithHTTPServer
function to accept the interface type is consistent with the field type change and completes the abstraction. This allows clients to provide any compatible server implementation, not justhttp.Server
.
198-205
:✅ Verification successful
Verify compatibility with non-http.Server implementations.
The
Start
method creates a new*http.Server
instance by default, which works with the new interface. However, there might be assumptions elsewhere in the code thatsrv
is specifically an*http.Server
. Let's verify there are no such assumptions.
🏁 Script executed:
#!/bin/bash # Check if there are any type assertions or specific http.Server method calls that might break with the interface # Search for any type assertions to *http.Server echo "Checking for type assertions to *http.Server:" rg "\.\\(\*http\\.Server\\)" --type go # Search for any method calls on srv that aren't part of the ListenAndServer interface echo -e "\nChecking for method calls on srv that aren't in the interface:" rg "srv\\.[^LS]" --type go | grep -v "ListenAndServe" | grep -v "Shutdown"Length of output: 1495
🏁 Script executed:
#!/bin/bash # Search for any direct s.srv.* method invocations across Go files echo "Searching for s.srv.* method calls across the repository:" rg "s\\.srv\\.[A-Za-z]\\w*" --type go -n # Show the Start method in server/sse.go for context echo -e "\n--- Start method in server/sse.go ---" rg -n 'func (s \*Server) Start' -A5 -B5 server/sse.go # Show the Stop (or Shutdown) method in server/sse.go for context echo -e "\n--- Stop method in server/sse.go ---" rg -n 'func (s \*Server) Stop' -A5 -B5 server/sse.goLength of output: 504
No http.Server‐specific usage detected
- No type assertions to
*http.Server
were found in the codebase.- The only call on
s.srv
isListenAndServe()
, which aligns with theListenAndServer
interface.No further changes are needed.
519b6b3
to
784ee20
Compare
Signed-off-by: Alvaro Saurin <[email protected]>
784ee20
to
fc34a1c
Compare
I think the SSEServer should have the two method handleMessage/handleSSE changed to public, so that they can easily be plugged into other frameworks like gin, so that we can use many existing middlewares and other Tracing/Logging/Metric tools。 So,the messes about using a native http.Server could be seperated from sse.go, maybe the current SSEServer should be SSEHandlers with another new SSEServer only do the work of http serving. Then users can implement their own GinSSEServer or something else. |
Replace
http.Server
by a genericListenAndServer
, so we do not enforce a specific implementation of the server.Summary by CodeRabbit