Skip to content

Commit

Permalink
ws mock server keepalive_timeout_seconds query parameter support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dkamps18 committed Feb 10, 2024
1 parent 2893a58 commit 9d8c257
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 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 Down Expand Up @@ -129,7 +139,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 +166,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

0 comments on commit 9d8c257

Please sign in to comment.