-
-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
fix: memory leak #4431
base: dev
Are you sure you want to change the base?
fix: memory leak #4431
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ func (d *Dispatcher) readLoop() { | |
m, err := ReadMsg(d.rw) | ||
if err != nil { | ||
close(d.doneCh) | ||
close(d.sendCh) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sendCh does not need to be actively closed; it should be automatically reclaimed after all senders have exited. As I commented in transport/message.go, I prefer to use a reasonable mechanism to ensure exit on the sender's side. |
||
return | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,15 +35,15 @@ type MessageTransporter interface { | |
DispatchWithType(m msg.Message, msgType, laneKey string) bool | ||
} | ||
|
||
func NewMessageTransporter(sendCh chan msg.Message) MessageTransporter { | ||
func NewMessageTransporter(sendFn func(msg.Message) error) MessageTransporter { | ||
return &transporterImpl{ | ||
sendCh: sendCh, | ||
sendFn: sendFn, | ||
registry: make(map[string]map[string]chan msg.Message), | ||
} | ||
} | ||
|
||
type transporterImpl struct { | ||
sendCh chan msg.Message | ||
sendFn func(msg.Message) error | ||
|
||
// First key is message type and second key is lane key. | ||
// Dispatch will dispatch message to related channel by its message type | ||
|
@@ -54,7 +54,7 @@ type transporterImpl struct { | |
|
||
func (impl *transporterImpl) Send(m msg.Message) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intend to add a context parameter similar to the Do function to allow cancellation of sending. One case is when the user exits manually, which can be canceled through this mechanism, and another case is for potential scenarios where a timeout might be set later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding context parameters would be a better solution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally, a TrySend function can be added, which returns a specific error when sendCh is full, instead of blocking. This function is more suitable for certain specific scenarios. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes,Adding TrySend is indeed a way to make it clearer. I didn't read the code thoroughly and may not fully understand the this function. |
||
return errors.PanicToError(func() { | ||
impl.sendCh <- m | ||
impl.sendFn(m) | ||
}) | ||
} | ||
|
||
|
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.
I hope to keep it as it is.