|
|
|
|
@@ -65,11 +65,13 @@ func (sm *SafeMap[K, V]) Len() int {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SafeWebsocketClientBuilder struct {
|
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
useTLS *bool
|
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
useTLS *bool
|
|
|
|
|
channelSize *int64
|
|
|
|
|
authenticateFn func(*SafeWebsocketClient) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
|
|
|
|
@@ -91,6 +93,11 @@ func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBui
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *SafeWebsocketClientBuilder) AuthenticateFn(authenticateFn func(*SafeWebsocketClient) error) *SafeWebsocketClientBuilder {
|
|
|
|
|
b.authenticateFn = authenticateFn
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *SafeWebsocketClientBuilder) Path(path string) *SafeWebsocketClientBuilder {
|
|
|
|
|
b.path = &path
|
|
|
|
|
return b
|
|
|
|
|
@@ -101,7 +108,12 @@ func (b *SafeWebsocketClientBuilder) RawQuery(rawQuery string) *SafeWebsocketCli
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *SafeWebsocketClientBuilder) Build() (*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 {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
@@ -111,19 +123,29 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
|
useTLS = *b.useTLS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.channelSize == nil {
|
|
|
|
|
channelSize := int64(1)
|
|
|
|
|
b.channelSize = &channelSize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsClient := SafeWebsocketClient{
|
|
|
|
|
baseHost: *b.baseHost,
|
|
|
|
|
basePort: *b.basePort,
|
|
|
|
|
useTLS: useTLS,
|
|
|
|
|
path: b.path,
|
|
|
|
|
rawQuery: b.rawQuery,
|
|
|
|
|
dataChannel: make(chan []byte, 1),
|
|
|
|
|
dataChannel: make(chan []byte, *b.channelSize),
|
|
|
|
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
reconnectCh: make(chan struct{}, 1),
|
|
|
|
|
isConnected: false,
|
|
|
|
|
doneMap: NewSafeMap[string, chan struct{}](),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.authenticateFn != nil {
|
|
|
|
|
wsClient.authenticateFn = b.authenticateFn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go wsClient.reconnectHandler()
|
|
|
|
|
|
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
|
@@ -134,20 +156,21 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SafeWebsocketClient struct {
|
|
|
|
|
baseHost string
|
|
|
|
|
basePort uint16
|
|
|
|
|
useTLS bool
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
dataChannel chan []byte
|
|
|
|
|
mu *custom_rwmutex.CustomRwMutex
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
ctx context.Context
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
reconnectCh chan struct{}
|
|
|
|
|
isConnected bool
|
|
|
|
|
|
|
|
|
|
doneMap *SafeMap[string, chan struct{}]
|
|
|
|
|
baseHost string
|
|
|
|
|
basePort uint16
|
|
|
|
|
useTLS bool
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
dataChannel chan []byte
|
|
|
|
|
mu *custom_rwmutex.CustomRwMutex
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
cancelFuncs []context.CancelFunc
|
|
|
|
|
ctx context.Context
|
|
|
|
|
reconnectCh chan struct{}
|
|
|
|
|
reconnectChans []chan struct{}
|
|
|
|
|
isConnected bool
|
|
|
|
|
doneMap *SafeMap[string, chan struct{}]
|
|
|
|
|
authenticateFn func(*SafeWebsocketClient) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
@@ -176,33 +199,38 @@ func (wsClient *SafeWebsocketClient) connect() 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 == websocket.ErrCloseSent {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
|
if err == websocket.ErrCloseSent {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.conn != nil {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
if wsClient.conn != nil {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
wsClient.conn = conn
|
|
|
|
|
wsClient.isConnected = true
|
|
|
|
|
|
|
|
|
|
if wsClient.authenticateFn != nil {
|
|
|
|
|
if err := wsClient.authenticateFn(wsClient); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
wsClient.ctx = ctx
|
|
|
|
|
wsClient.cancel = cancel
|
|
|
|
|
wsClient.conn = conn
|
|
|
|
|
wsClient.isConnected = true
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
|
|
|
|
go wsClient.startPingTicker(pingCtx)
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
|
|
|
|
|
|
|
|
|
go wsClient.startPingTicker(ctx)
|
|
|
|
|
go wsClient.startReceiveHandler(ctx)
|
|
|
|
|
receiverCtx, receiverCancel := context.WithCancel(context.Background())
|
|
|
|
|
go wsClient.startReceiveHandler(receiverCtx)
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, receiverCancel)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
@@ -215,10 +243,13 @@ func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("ping ticker stopped")
|
|
|
|
|
return
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.conn == nil {
|
|
|
|
|
return fmt.Errorf("connecrtion closed")
|
|
|
|
|
return fmt.Errorf("connection closed")
|
|
|
|
|
}
|
|
|
|
|
if err := wsClient.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
|
|
|
log.Printf("Ping failed: %v. Will attempt reconnect.", err)
|
|
|
|
|
@@ -226,9 +257,6 @@ func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("Ping ticker stopped due to context cancellation")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -237,10 +265,10 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("Reconnect handler stopped")
|
|
|
|
|
log.Println("receive handler stopped")
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
if err := wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
conn := wsClient.conn
|
|
|
|
|
|
|
|
|
|
if conn == nil {
|
|
|
|
|
@@ -254,12 +282,15 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|
|
|
|
}
|
|
|
|
|
select {
|
|
|
|
|
case wsClient.dataChannel <- message:
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("Reconnect handler stopped")
|
|
|
|
|
default:
|
|
|
|
|
log.Println("Data channel full, dropping message")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -278,22 +309,21 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
|
select {
|
|
|
|
|
case <-wsClient.reconnectCh:
|
|
|
|
|
log.Println("Reconnect triggered")
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.cancel != nil {
|
|
|
|
|
wsClient.cancel()
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
log.Println("Attempting reconnect in %v...", backoff)
|
|
|
|
|
isInnerLoop := true
|
|
|
|
|
for isInnerLoop {
|
|
|
|
|
log.Printf("Attempting reconnect in %v...", backoff)
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(backoff):
|
|
|
|
|
if err := wsClient.connect(); err != nil {
|
|
|
|
|
log.Println("Reconnect failed: %v", err)
|
|
|
|
|
log.Printf("Reconnect failed: %v", err)
|
|
|
|
|
if backoff < maxBackoff {
|
|
|
|
|
backoff *= 2
|
|
|
|
|
}
|
|
|
|
|
@@ -301,16 +331,65 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
|
}
|
|
|
|
|
log.Println("Reconnected successfully")
|
|
|
|
|
backoff = 1 * time.Second
|
|
|
|
|
break
|
|
|
|
|
isInnerLoop = false
|
|
|
|
|
continue
|
|
|
|
|
case <-wsClient.ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if wsClient.reconnectChans != nil {
|
|
|
|
|
for _, reconnectCh := range wsClient.reconnectChans {
|
|
|
|
|
reconnectCh <- struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} {
|
|
|
|
|
reconnectCh := make(chan struct{})
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
wsClient.reconnectChans = append(wsClient.reconnectChans, reconnectCh)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return reconnectCh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
|
|
|
|
return wsClient.dataChannel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) WriteJSON(message any) error {
|
|
|
|
|
return wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
return wsClient.conn.WriteJSON(message)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) Close() error {
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if wsClient.reconnectChans != nil {
|
|
|
|
|
for _, reconnectChan := range wsClient.reconnectChans {
|
|
|
|
|
close(reconnectChan)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if wsClient.conn != nil {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
close(wsClient.dataChannel)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|