|
|
|
|
@@ -6,6 +6,7 @@ import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
|
|
|
|
@@ -17,6 +18,52 @@ const (
|
|
|
|
|
pingPeriod = 10 * time.Second
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SafeMap[K comparable, V any] struct {
|
|
|
|
|
m sync.Map
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSafeMap[K comparable, V any]() *SafeMap[K, V] {
|
|
|
|
|
return &SafeMap[K, V]{
|
|
|
|
|
m: sync.Map{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *SafeMap[K, V]) Store(key K, value V) {
|
|
|
|
|
sm.m.Store(key, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *SafeMap[K, V]) Load(key K) (value V, ok bool) {
|
|
|
|
|
val, loaded := sm.m.Load(key)
|
|
|
|
|
if !loaded {
|
|
|
|
|
return *new(V), false
|
|
|
|
|
}
|
|
|
|
|
return val.(V), true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *SafeMap[K, V]) Delete(key K) {
|
|
|
|
|
sm.m.Delete(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *SafeMap[K, V]) Range(f func(K, V) bool) {
|
|
|
|
|
sm.m.Range(func(key, value any) bool {
|
|
|
|
|
k, ok1 := key.(K)
|
|
|
|
|
v, ok2 := value.(V)
|
|
|
|
|
if !ok1 || !ok2 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return f(k, v)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *SafeMap[K, V]) Len() int {
|
|
|
|
|
count := 0
|
|
|
|
|
sm.Range(func(_ K, _ V) bool {
|
|
|
|
|
count++
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SafeWebsocketClientBuilder struct {
|
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
|
@@ -74,8 +121,11 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
|
|
|
|
reconnectCh: make(chan struct{}, 1),
|
|
|
|
|
isConnected: false,
|
|
|
|
|
doneMap: NewSafeMap[string, chan struct{}](),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go wsClient.reconnectHandler()
|
|
|
|
|
|
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
|
|
|
|
}
|
|
|
|
|
@@ -96,6 +146,8 @@ type SafeWebsocketClient struct {
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
reconnectCh chan struct{}
|
|
|
|
|
isConnected bool
|
|
|
|
|
|
|
|
|
|
doneMap *SafeMap[string, chan struct{}]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
@@ -124,77 +176,90 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn.SetPingHandler(func(pingData string) error {
|
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
|
if err == websocket.ErrCloseSent {
|
|
|
|
|
return nil
|
|
|
|
|
return wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
|
if err == websocket.ErrCloseSent {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.conn != nil {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
wsClient.ctx = ctx
|
|
|
|
|
wsClient.cancel = cancel
|
|
|
|
|
wsClient.conn = conn
|
|
|
|
|
wsClient.isConnected = true
|
|
|
|
|
|
|
|
|
|
go wsClient.startPingTicker(ctx)
|
|
|
|
|
go wsClient.startReceiveHandler(ctx)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
go wsClient.startPingTicker()
|
|
|
|
|
go wsClient.startReceiveHandler()
|
|
|
|
|
go wsClient.reconnectHandler()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) startPingTicker() {
|
|
|
|
|
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.conn == nil {
|
|
|
|
|
return fmt.Errorf("connecrtion closed")
|
|
|
|
|
}
|
|
|
|
|
if err := wsClient.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
|
|
|
log.Printf("Ping failed: %v. Will attempt reconnect.", err)
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
|
log.Println("Ping ticker stopped")
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("Ping ticker stopped due to context cancellation")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|
|
|
|
func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("Reconnect handler stopped")
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
if err := wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
conn := wsClient.conn
|
|
|
|
|
|
|
|
|
|
if conn == nil {
|
|
|
|
|
return fmt.Errorf("no active connection, waiting for reconnect")
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return fmt.Errorf("connection closed")
|
|
|
|
|
}
|
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return fmt.Errorf("failed to read message: %v", err)
|
|
|
|
|
}
|
|
|
|
|
select {
|
|
|
|
|
case wsClient.dataChannel <- message:
|
|
|
|
|
default:
|
|
|
|
|
log.Println("Data channel full, dropping message")
|
|
|
|
|
}
|
|
|
|
|
wsClient.dataChannel <- message
|
|
|
|
|
return nil
|
|
|
|
|
}); err != nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -207,12 +272,40 @@ func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
|
backoff := 1 * time.Second
|
|
|
|
|
maxBackoff := 30 * time.Second
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-wsClient.reconnectCh:
|
|
|
|
|
wsClient.cancel()
|
|
|
|
|
wsClient.connect()
|
|
|
|
|
log.Println("Reconnect triggered")
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.cancel != nil {
|
|
|
|
|
wsClient.cancel()
|
|
|
|
|
}
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
log.Println("Attempting reconnect in %v...", backoff)
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(backoff):
|
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
|
log.Println("Reconnect failed: %v", err)
|
|
|
|
|
if backoff < maxBackoff {
|
|
|
|
|
backoff *= 2
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
log.Println("Reconnected successfully")
|
|
|
|
|
backoff = 1 * time.Second
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
|
log.Println("Reconnect handler stopped due to client shutdown")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|