-
Notifications
You must be signed in to change notification settings - Fork 2
feat: change option construstor module instead of moduleparam #333
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
Conversation
Summary by CodeRabbit
WalkthroughThe TCP server constructor now accepts a core.Module instance instead of a factory/function. All affected tests were updated to pass a concrete module via appModule() or serverModule(); one test passes nil. Imports were adjusted accordingly. No other functional paths were modified. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Test as Test
participant TCP as tcp.New
participant Server as TCPServer
Test->>TCP: New(moduleInstance, opts)
note over Test,TCP: Module is already constructed (no factory)
TCP->>Server: Initialize with provided module
Server-->>Test: Service instance
sequenceDiagram
autonumber
actor Test as Test (Old)
participant TCP as tcp.New (Old)
participant Factory as moduleFactory()
participant Server as TCPServer
Test->>TCP: New(moduleFactory, opts)
TCP->>Factory: Invoke to get module
Factory-->>TCP: core.Module
TCP->>Server: Initialize with module
Server-->>Test: Service instance
note over diagram: Old flow (factory) replaced by direct module instance
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
microservices/tcp/server.go (2)
62-70: Panic early if Module is nil.Avoid a nil-interface method call in
Listen()whensvc.Moduleis nil.Apply:
func (svc *Server) Listen() { + if svc.Module == nil { + panic("module is nil; pass a module to tcp.New or call (*Server).Create before Listen") + } listener, err := net.Listen("tcp", svc.Addr) if err != nil { panic(err) }
3-15: Do not share the same net.Listener between http.Serve and manual Accept.
http.Serve(listener, nil)races with your ownAccept()loop on the same listener; connections will be unpredictably consumed by one or the other.Apply:
import ( "bufio" "fmt" "net" - "net/http" "reflect" "slices" "strings" @@ - go http.Serve(listener, nil)Also applies to: 72-72
microservices/ctx_test.go (1)
66-72: Fix assertions: using payload instead of payload2 (and wrong expected values).The second parse checks
payloadinstead ofpayload2, and expected values should match the second message.Apply:
- err = ctx2.PayloadParser(&payload2) - require.Nil(t, err) - require.Equal(t, "[email protected]", payload.Email) - require.Equal(t, "12345678@Tc", payload.Password) + err = ctx2.PayloadParser(&payload2) + require.Nil(t, err) + require.Equal(t, "[email protected]", payload2.Email) + require.Equal(t, "12345678", payload2.Password)
🧹 Nitpick comments (4)
microservices/tcp/server.go (2)
23-26: Constructor change is fine; consider a compatibility helper and nil-module guard.
- Add a helper to ease migration from factory style:
NewFromFactory(fn core.ModuleParam, opts ...Options) microservices.Service { return New(fn(), opts...) }.- Also, if
modulecan be nil in some call sites/tests, guard inListen()to fail fast.Apply:
func New(module core.Module, opts ...Options) microservices.Service { svc := &Server{ - Module: module, + Module: module, config: microservices.DefaultConfig(), } ... } + +// NewFromFactory provides a transitional API for callers still using a factory. +func NewFromFactory(factory core.ModuleParam, opts ...Options) microservices.Service { + return New(factory(), opts...) +}
117-123: Wildcard match should check suffix, not “contains any”.
strings.ContainsAny(msg.Event, "*")matches any asterisk anywhere, but you then trim only a trailing*. UseHasSuffixfor intent clarity.Apply:
- } else if strings.ContainsAny(msg.Event, "*") { + } else if strings.HasSuffix(msg.Event, "*") { prefix := strings.TrimSuffix(msg.Event, "*")microservices/handler_test.go (1)
50-51: Avoid fixed sleeps; prefer readiness probe.
time.Sleep(1000 * time.Millisecond)makes the test slow and flaky across environments. Poll for port readiness with a timeout instead.I can provide a tiny helper to wait for
addrto accept connections if useful.microservices/ctx_test.go (1)
18-18: Don’t pass a nil Module; use tcp.Open for config-only service.
tcp.New(nil, ...)risks nil-method calls if code evolves. Usetcp.Open(tcp.Options{...})or supply a minimal module.Apply:
- svc := tcp.New(nil, tcp.Options{Addr: "127.0.0.1:5173"}) + svc := tcp.Open(tcp.Options{Addr: "127.0.0.1:5173"})
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
microservices/ctx_test.go(1 hunks)microservices/exception_test.go(1 hunks)microservices/guard_test.go(1 hunks)microservices/handler_test.go(1 hunks)microservices/middleware_test.go(1 hunks)microservices/pipe_test.go(1 hunks)microservices/tcp/error_test.go(3 hunks)microservices/tcp/server.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (8)
microservices/guard_test.go (3)
microservices/tcp/server.go (1)
New(23-39)microservices/kafka/connect.go (2)
New(70-90)Options(13-17)microservices/nats/connect.go (2)
New(82-102)Options(15-19)
microservices/tcp/error_test.go (2)
microservices/tcp/server.go (1)
New(23-39)microservices/tcp/client.go (1)
Options(10-14)
microservices/tcp/server.go (5)
microservices/mqtt/connect.go (2)
New(96-113)Options(21-24)microservices/kafka/connect.go (2)
New(70-90)Options(13-17)microservices/nats/connect.go (2)
New(82-102)Options(15-19)microservices/redis/connect.go (2)
New(90-112)Options(15-18)microservices/amqlib/connect.go (2)
New(166-187)Options(14-17)
microservices/exception_test.go (2)
microservices/tcp/server.go (1)
New(23-39)microservices/tcp/client.go (1)
Options(10-14)
microservices/ctx_test.go (2)
microservices/tcp/server.go (1)
New(23-39)microservices/tcp/client.go (1)
Options(10-14)
microservices/middleware_test.go (1)
microservices/tcp/server.go (1)
New(23-39)
microservices/pipe_test.go (2)
microservices/tcp/server.go (1)
New(23-39)microservices/kafka/connect.go (2)
New(70-90)Options(13-17)
microservices/handler_test.go (1)
microservices/tcp/server.go (1)
New(23-39)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (go)
🔇 Additional comments (8)
microservices/pipe_test.go (1)
77-82: Updated tcp.New usage looks correct.Passing
appModule()(a core.Module) matches the new constructor signature. No further action here.microservices/guard_test.go (1)
41-43: LGTM on switching to module value.Invocation now matches
tcp.New(core.Module, ...Options).microservices/exception_test.go (1)
38-40: LGTM on constructor change.
appModule()passed correctly to the newtcp.Newsignature.microservices/handler_test.go (1)
94-96: LGTM on updated call site.Passing
appModule()aligns with the new API.microservices/middleware_test.go (1)
46-48: LGTM on switching to module instance.Constructor usage matches new signature.
microservices/tcp/error_test.go (3)
43-45: LGTM: updated to pass serverModule().Matches new
tcp.Newsignature.
103-105: LGTM: constructor updated in first panic case.
114-116: LGTM: constructor updated in second panic case.
No description provided.