fixed panic when client connected abruptly
This commit is contained in:
@@ -5,13 +5,15 @@ import (
|
||||
)
|
||||
|
||||
type MessageChannel struct {
|
||||
msgChan chan proto.Message
|
||||
onClose func()
|
||||
msgChan chan proto.Message
|
||||
isClosed bool
|
||||
onClose func()
|
||||
}
|
||||
|
||||
func NewMessageChannel() *MessageChannel {
|
||||
return &MessageChannel{
|
||||
msgChan: make(chan proto.Message, 1),
|
||||
// allow some buffer to avoid blocked writes
|
||||
msgChan: make(chan proto.Message, 2),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +22,9 @@ func (m *MessageChannel) OnClose(f func()) {
|
||||
}
|
||||
|
||||
func (m *MessageChannel) WriteMessage(msg proto.Message) error {
|
||||
if m.isClosed {
|
||||
return ErrChannelClosed
|
||||
}
|
||||
m.msgChan <- msg
|
||||
return nil
|
||||
}
|
||||
@@ -29,6 +34,7 @@ func (m *MessageChannel) ReadChan() <-chan proto.Message {
|
||||
}
|
||||
|
||||
func (m *MessageChannel) Close() {
|
||||
m.isClosed = true
|
||||
close(m.msgChan)
|
||||
if m.onClose != nil {
|
||||
m.onClose()
|
||||
|
||||
Reference in New Issue
Block a user