-
Notifications
You must be signed in to change notification settings - Fork 150
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
feat: redesign controller-replica communication #1246
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -9,20 +9,45 @@ import ( | |
"github.com/longhorn/longhorn-engine/pkg/types" | ||
) | ||
|
||
const ( | ||
threadCount = 256 | ||
) | ||
|
||
type Server struct { | ||
wire *Wire | ||
requests chan *Message | ||
responses chan *Message | ||
done chan struct{} | ||
data types.DataProcessor | ||
} | ||
|
||
func NewServer(conn net.Conn, data types.DataProcessor) *Server { | ||
return &Server{ | ||
//init theads | ||
server := &Server{ | ||
wire: NewWire(conn), | ||
requests: make(chan *Message, 1024), | ||
responses: make(chan *Message, 1024), | ||
done: make(chan struct{}, 5), | ||
data: data, | ||
} | ||
for i := 0; i < threadCount; i++ { | ||
go func(s *Server) { | ||
for { | ||
msg := <-s.requests | ||
switch msg.Type { | ||
case TypeRead: | ||
s.handleRead(msg) | ||
case TypeWrite: | ||
s.handleWrite(msg) | ||
case TypeUnmap: | ||
s.handleUnmap(msg) | ||
case TypePing: | ||
s.handlePing(msg) | ||
} | ||
} | ||
}(server) | ||
} | ||
Comment on lines
+33
to
+49
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. 🛠️ Refactor suggestion Add graceful shutdown mechanism for goroutines for i := 0; i < threadCount; i++ {
go func(s *Server) {
for {
- msg := <-s.requests
+ select {
+ case msg, ok := <-s.requests:
+ if !ok {
+ return
+ }
switch msg.Type {
...
}
+ case <-s.done:
+ return
+ }
}
}(server)
}
|
||
return server | ||
} | ||
|
||
func (s *Server) Handle() error { | ||
|
@@ -43,16 +68,7 @@ func (s *Server) readFromWire(ret chan<- error) { | |
ret <- err | ||
return | ||
} | ||
switch msg.Type { | ||
case TypeRead: | ||
go s.handleRead(msg) | ||
case TypeWrite: | ||
go s.handleWrite(msg) | ||
case TypeUnmap: | ||
go s.handleUnmap(msg) | ||
case TypePing: | ||
go s.handlePing(msg) | ||
} | ||
s.requests <- msg | ||
ret <- nil | ||
} | ||
|
||
|
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.
🛠️ Refactor suggestion
Possible memory retention
Since
c.messages[resp.Seq]
is never set tonil
afterreq.Complete
, stale requests may remain in memory. If large objects are attached, memory usage could grow unnecessarily. Consider clearing this reference:func (c *Client) handleResponse(resp *Message) { req := c.messages[resp.Seq] req.Type = resp.Type req.Size = resp.Size req.Data = resp.Data req.Complete <- struct{}{} + c.messages[resp.Seq] = nil }