|
|
|
@@ -6,6 +6,7 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
|
|
|
custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
|
|
|
@@ -17,6 +18,52 @@ const (
|
|
|
|
pingPeriod = 10 * time.Second
|
|
|
|
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 {
|
|
|
|
type SafeWebsocketClientBuilder struct {
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
@@ -59,8 +106,6 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var useTLS bool
|
|
|
|
var useTLS bool
|
|
|
|
if b.useTLS != nil {
|
|
|
|
if b.useTLS != nil {
|
|
|
|
useTLS = *b.useTLS
|
|
|
|
useTLS = *b.useTLS
|
|
|
|
@@ -72,16 +117,14 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
useTLS: useTLS,
|
|
|
|
useTLS: useTLS,
|
|
|
|
path: b.path,
|
|
|
|
path: b.path,
|
|
|
|
rawQuery: b.rawQuery,
|
|
|
|
rawQuery: b.rawQuery,
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
|
|
|
dataChannel: make(chan []byte, 1),
|
|
|
|
dataChannel: make(chan []byte, 1),
|
|
|
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
|
|
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
|
|
|
reconnectCh: make(chan struct{}, 1),
|
|
|
|
reconnectCh: make(chan struct{}, 1),
|
|
|
|
isConnected: false,
|
|
|
|
isConnected: false,
|
|
|
|
|
|
|
|
doneMap: NewSafeMap[string, chan struct{}](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
|
|
|
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -101,6 +144,8 @@ type SafeWebsocketClient struct {
|
|
|
|
cancel context.CancelFunc
|
|
|
|
cancel context.CancelFunc
|
|
|
|
reconnectCh chan struct{}
|
|
|
|
reconnectCh chan struct{}
|
|
|
|
isConnected bool
|
|
|
|
isConnected bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doneMap *SafeMap[string, chan struct{}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
@@ -129,21 +174,28 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conn.SetPingHandler(func(pingData string) error {
|
|
|
|
conn.SetPingHandler(func(pingData string) error {
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
return wsClient.mu.WriteHandler(func() error {
|
|
|
|
if err == websocket.ErrCloseSent {
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
return 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 nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@@ -157,6 +209,10 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
func (wsClient *SafeWebsocketClient) startPingTicker() {
|
|
|
|
func (wsClient *SafeWebsocketClient) startPingTicker() {
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doneKey := "startPingTicker"
|
|
|
|
|
|
|
|
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
|
|
|
|
|
|
|
done, _ := wsClient.doneMap.Load(doneKey)
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ticker.C:
|
|
|
|
@@ -167,7 +223,7 @@ func (wsClient *SafeWebsocketClient) startPingTicker() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
case <-done:
|
|
|
|
log.Println("Ping ticker stopped")
|
|
|
|
log.Println("Ping ticker stopped")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -175,29 +231,28 @@ func (wsClient *SafeWebsocketClient) startPingTicker() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|
|
|
func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|
|
|
|
|
|
|
doneKey := "startReceiveHandler"
|
|
|
|
|
|
|
|
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
|
|
|
|
|
|
|
done, _ := wsClient.doneMap.Load(doneKey)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-wsClient.reconnectCh:
|
|
|
|
case <-done:
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
|
|
|
|
log.Println("Reconnect handler stopped")
|
|
|
|
log.Println("Reconnect handler stopped")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
if err := wsClient.mu.ReadHandler(func() error {
|
|
|
|
conn := wsClient.conn
|
|
|
|
conn := wsClient.conn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if conn == nil {
|
|
|
|
if conn == nil {
|
|
|
|
return fmt.Errorf("no active connection, waiting for reconnect")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
wsClient.dataChannel <- message
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}); err != nil {
|
|
|
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
wsClient.dataChannel <- message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -210,8 +265,22 @@ func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
for range wsClient.reconnectCh {
|
|
|
|
doneKey := "reconnectHandler"
|
|
|
|
wsClient.connect()
|
|
|
|
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
|
|
|
|
|
|
|
done, _ := wsClient.doneMap.Load(doneKey)
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-wsClient.reconnectCh:
|
|
|
|
|
|
|
|
wsClient.cancel()
|
|
|
|
|
|
|
|
wsClient.connect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wsClient.doneMap.Range(func(s string, c chan struct{}) bool {
|
|
|
|
|
|
|
|
c <- struct{}{}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
case <-done:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|