Skip to content

Commit

Permalink
Merge pull request #312 from Dkamps18/wskeepalive
Browse files Browse the repository at this point in the history
ws mock server keepalive_timeout_seconds query parameter support
  • Loading branch information
Xemdo authored Feb 13, 2024
2 parents 2893a58 + a996cc7 commit e5c22fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/events/websocket/mock_server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client struct {
keepAliveChanOpen bool
keepAliveLoopChan chan struct{}
keepAliveTimer *time.Ticker
keepAliveSeconds int
pingChanOpen bool
pingLoopChan chan struct{}
pingTimer *time.Ticker
Expand Down
25 changes: 21 additions & 4 deletions internal/events/websocket/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -51,9 +52,18 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)

// Connection successful. WebSocket is open.

// keepalive timeout
keepalive_seconds := KEEPALIVE_TIMEOUT_SECONDS
if keepalive_seconds_string := r.URL.Query().Get("keepalive_timeout_seconds"); keepalive_seconds_string != "" {
if val, err := strconv.Atoi(keepalive_seconds_string); err == nil && val >= 10 && val <= 600 {
keepalive_seconds = val
}
}
keepalive_duration := time.Duration(keepalive_seconds) * time.Second

// Get connected at time and set automatic read timeout
connectedAtTimestamp := time.Now().UTC().Format(time.RFC3339Nano)
conn.SetReadDeadline(time.Now().Add(time.Second * KEEPALIVE_TIMEOUT_SECONDS))
conn.SetReadDeadline(time.Now().Add(keepalive_duration))

client := &Client{
clientName: util.RandomGUID()[:8],
Expand All @@ -62,6 +72,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
connectionUrl: fmt.Sprintf("%v://%v/ws", serverManager.protocolHttp, r.Host),
KeepAliveEnabled: true,
keepAliveChanOpen: false,
keepAliveSeconds: keepalive_seconds,
pingChanOpen: false,
}

Expand Down Expand Up @@ -129,7 +140,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
Session: WelcomeMessagePayloadSession{
ID: fmt.Sprintf("%v_%v", ws.ServerId, client.clientName),
Status: "connected",
KeepaliveTimeoutSeconds: KEEPALIVE_TIMEOUT_SECONDS,
KeepaliveTimeoutSeconds: keepalive_seconds,
ReconnectUrl: nil,
ConnectedAt: connectedAtTimestamp,
},
Expand All @@ -156,7 +167,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
}

// Set up ping/pong and keepalive handling
client.keepAliveTimer = time.NewTicker(10 * time.Second)
client.keepAliveTimer = time.NewTicker(keepalive_duration)
client.pingTimer = time.NewTicker(5 * time.Second)
client.keepAliveLoopChan = make(chan struct{})
client.pingLoopChan = make(chan struct{})
Expand Down Expand Up @@ -319,6 +330,12 @@ func (ws *WebSocketServer) InitiateRestart() {
reconnectId = reconnectId[:len(reconnectId)-1]
clientConnectionUrl := strings.Replace(client.connectionUrl, "http://", "ws://", -1)
clientConnectionUrl = strings.Replace(clientConnectionUrl, "https://", "wss://", -1)
var reconnecturl string
if client.keepAliveSeconds != KEEPALIVE_TIMEOUT_SECONDS {
reconnecturl = fmt.Sprintf("%v?reconnect_id=%v&keepalive_timeout_seconds=%d", clientConnectionUrl, reconnectId, client.keepAliveSeconds)
} else {
reconnecturl = fmt.Sprintf("%v?reconnect_id=%v", clientConnectionUrl, reconnectId)
}
reconnectMsg, _ := json.Marshal(
ReconnectMessage{
Metadata: MessageMetadata{
Expand All @@ -331,7 +348,7 @@ func (ws *WebSocketServer) InitiateRestart() {
ID: sessionId,
Status: "reconnecting",
KeepaliveTimeoutSeconds: nil,
ReconnectUrl: fmt.Sprintf("%v?reconnect_id=%v", clientConnectionUrl, reconnectId),
ReconnectUrl: reconnecturl,
ConnectedAt: client.ConnectedAtTimestamp,
},
},
Expand Down

0 comments on commit e5c22fe

Please sign in to comment.