Enhance WebSocket write handling and connection management
- Introduced a buffered write channel and a dedicated write worker goroutine to serialize write operations, preventing concurrent write panics. - Updated the Write and WriteControl methods to send messages through the write channel, improving error handling and connection stability. - Refactored ping and pong handlers to utilize the new write channel for sending control messages. - Enhanced publisher logic to manage write channels for WebSocket connections, ensuring efficient message delivery and error handling. - Bumped version to v0.23.0 to reflect these changes.
This commit is contained in:
@@ -1,11 +1,28 @@
|
||||
package publish
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"next.orly.dev/pkg/encoders/event"
|
||||
"next.orly.dev/pkg/interfaces/publisher"
|
||||
"next.orly.dev/pkg/interfaces/typer"
|
||||
)
|
||||
|
||||
// WriteRequest represents a write operation to be performed by the write worker
|
||||
type WriteRequest struct {
|
||||
Data []byte
|
||||
MsgType int
|
||||
IsControl bool
|
||||
Deadline time.Time
|
||||
}
|
||||
|
||||
// WriteChanSetter defines the interface for setting write channels
|
||||
type WriteChanSetter interface {
|
||||
SetWriteChan(*websocket.Conn, chan<- WriteRequest)
|
||||
GetWriteChan(*websocket.Conn) (chan<- WriteRequest, bool)
|
||||
}
|
||||
|
||||
// S is the control structure for the subscription management scheme.
|
||||
type S struct {
|
||||
publisher.Publishers
|
||||
@@ -36,3 +53,15 @@ func (s *S) Receive(msg typer.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetSocketPublisher returns the socketapi publisher instance
|
||||
func (s *S) GetSocketPublisher() WriteChanSetter {
|
||||
for _, p := range s.Publishers {
|
||||
if p.Type() == "socketapi" {
|
||||
if socketPub, ok := p.(WriteChanSetter); ok {
|
||||
return socketPub
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
v0.21.4
|
||||
v0.23.0
|
||||
Reference in New Issue
Block a user