fix: fixing client deadlock condition & reconnection issues
This commit is contained in:
@@ -65,11 +65,12 @@ func (sm *SafeMap[K, V]) Len() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SafeWebsocketClientBuilder struct {
|
type SafeWebsocketClientBuilder struct {
|
||||||
baseHost *string `nil_checker:"required"`
|
baseHost *string `nil_checker:"required"`
|
||||||
basePort *uint16 `nil_checker:"required"`
|
basePort *uint16 `nil_checker:"required"`
|
||||||
path *string
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
useTLS *bool
|
useTLS *bool
|
||||||
|
channelSize *int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
||||||
@@ -101,7 +102,12 @@ func (b *SafeWebsocketClientBuilder) RawQuery(rawQuery string) *SafeWebsocketCli
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) Build(channelSize int) (*SafeWebsocketClient, error) {
|
func (b *SafeWebsocketClientBuilder) ChannelSize(channelSize int64) *SafeWebsocketClientBuilder {
|
||||||
|
b.channelSize = &channelSize
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketClient, error) {
|
||||||
if err := internal.NilChecker(b); err != nil {
|
if err := internal.NilChecker(b); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -111,14 +117,20 @@ func (b *SafeWebsocketClientBuilder) Build(channelSize int) (*SafeWebsocketClien
|
|||||||
useTLS = *b.useTLS
|
useTLS = *b.useTLS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.channelSize == nil {
|
||||||
|
channelSize := int64(1)
|
||||||
|
b.channelSize = &channelSize
|
||||||
|
}
|
||||||
|
|
||||||
wsClient := SafeWebsocketClient{
|
wsClient := SafeWebsocketClient{
|
||||||
baseHost: *b.baseHost,
|
baseHost: *b.baseHost,
|
||||||
basePort: *b.basePort,
|
basePort: *b.basePort,
|
||||||
useTLS: useTLS,
|
useTLS: useTLS,
|
||||||
path: b.path,
|
path: b.path,
|
||||||
rawQuery: b.rawQuery,
|
rawQuery: b.rawQuery,
|
||||||
dataChannel: make(chan []byte, channelSize),
|
dataChannel: make(chan []byte, *b.channelSize),
|
||||||
mu: custom_rwmutex.NewCustomRwMutex(),
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
||||||
|
ctx: ctx,
|
||||||
reconnectCh: make(chan struct{}, 1),
|
reconnectCh: make(chan struct{}, 1),
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
doneMap: NewSafeMap[string, chan struct{}](),
|
doneMap: NewSafeMap[string, chan struct{}](),
|
||||||
@@ -142,8 +154,8 @@ type SafeWebsocketClient struct {
|
|||||||
dataChannel chan []byte
|
dataChannel chan []byte
|
||||||
mu *custom_rwmutex.CustomRwMutex
|
mu *custom_rwmutex.CustomRwMutex
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
|
cancelFuncs []context.CancelFunc
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
|
||||||
reconnectCh chan struct{}
|
reconnectCh chan struct{}
|
||||||
isConnected bool
|
isConnected bool
|
||||||
|
|
||||||
@@ -176,18 +188,16 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conn.SetPingHandler(func(pingData string) error {
|
conn.SetPingHandler(func(pingData string) error {
|
||||||
return wsClient.mu.WriteHandler(func() error {
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
||||||
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
if err == websocket.ErrCloseSent {
|
||||||
if err == websocket.ErrCloseSent {
|
return nil
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
||||||
})
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
@@ -195,14 +205,17 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
wsClient.conn.Close()
|
wsClient.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
wsClient.ctx = ctx
|
|
||||||
wsClient.cancel = cancel
|
|
||||||
wsClient.conn = conn
|
wsClient.conn = conn
|
||||||
|
|
||||||
wsClient.isConnected = true
|
wsClient.isConnected = true
|
||||||
|
|
||||||
go wsClient.startPingTicker(ctx)
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
||||||
go wsClient.startReceiveHandler(ctx)
|
go wsClient.startPingTicker(pingCtx)
|
||||||
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
||||||
|
|
||||||
|
receiverCtx, receiverCancel := context.WithCancel(context.Background())
|
||||||
|
go wsClient.startReceiveHandler(receiverCtx)
|
||||||
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, receiverCancel)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -215,10 +228,13 @@ func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("ping ticker stopped")
|
||||||
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
if wsClient.conn == nil {
|
if wsClient.conn == nil {
|
||||||
return fmt.Errorf("connecrtion closed")
|
return fmt.Errorf("connection closed")
|
||||||
}
|
}
|
||||||
if err := wsClient.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
if err := wsClient.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
||||||
log.Printf("Ping failed: %v. Will attempt reconnect.", err)
|
log.Printf("Ping failed: %v. Will attempt reconnect.", err)
|
||||||
@@ -226,9 +242,6 @@ func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("Ping ticker stopped due to context cancellation")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -237,10 +250,10 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Println("Reconnect handler stopped")
|
log.Println("receive handler stopped")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
wsClient.mu.ReadHandler(func() error {
|
if err := wsClient.mu.ReadHandler(func() error {
|
||||||
conn := wsClient.conn
|
conn := wsClient.conn
|
||||||
|
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
@@ -254,12 +267,16 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case wsClient.dataChannel <- message:
|
case wsClient.dataChannel <- message:
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("Reconnect handler stopped")
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
log.Println("Data channel full, dropping message")
|
log.Println("Data channel full, dropping message")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -275,19 +292,15 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|||||||
backoff := 1 * time.Second
|
backoff := 1 * time.Second
|
||||||
maxBackoff := 30 * time.Second
|
maxBackoff := 30 * time.Second
|
||||||
for {
|
for {
|
||||||
if wsClient.ctx == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
select {
|
select {
|
||||||
case <-wsClient.reconnectCh:
|
case <-wsClient.reconnectCh:
|
||||||
log.Println("Reconnect triggered")
|
log.Println("Reconnect triggered")
|
||||||
wsClient.mu.WriteHandler(func() error {
|
if wsClient.cancelFuncs != nil {
|
||||||
if wsClient.cancel != nil {
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
wsClient.cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
wsClient.isConnected = false
|
}
|
||||||
return nil
|
wsClient.isConnected = false
|
||||||
})
|
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
@@ -311,8 +324,10 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-wsClient.ctx.Done():
|
case <-wsClient.ctx.Done():
|
||||||
log.Println("Reconnect handler stopped due to client shutdown")
|
log.Println("reconnect handler stopped due to client shutdown")
|
||||||
|
wsClient.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,8 +339,10 @@ func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
|||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) Close() error {
|
func (wsClient *SafeWebsocketClient) Close() error {
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
if wsClient.cancel != nil {
|
if wsClient.cancelFuncs != nil {
|
||||||
wsClient.cancel()
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if wsClient.conn != nil {
|
if wsClient.conn != nil {
|
||||||
wsClient.conn.Close()
|
wsClient.conn.Close()
|
||||||
|
|||||||
@@ -1,22 +1,39 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-sigChan
|
||||||
|
fmt.Println("\nReceived interrupt signal. Shutting down gracefully...")
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
Path("/ws/test/data_1").
|
Path("/ws/test/data_1").
|
||||||
UseTLS(false).
|
UseTLS(false).
|
||||||
Build()
|
Build(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dataChannel := wsClient.DataChannel()
|
dataChannel := wsClient.DataChannel()
|
||||||
|
|
||||||
for data := range dataChannel {
|
for data := range dataChannel {
|
||||||
|
|||||||
Reference in New Issue
Block a user