Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws mock server keepalive_timeout_seconds query parameter support #312

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading