Skip to content

Commit 9d8c257

Browse files
committed
ws mock server keepalive_timeout_seconds query parameter support
1 parent 2893a58 commit 9d8c257

File tree

1 file changed

+13
-3
lines changed
  • internal/events/websocket/mock_server

1 file changed

+13
-3
lines changed

internal/events/websocket/mock_server/server.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9+
"strconv"
910
"strings"
1011
"sync"
1112
"time"
@@ -51,9 +52,18 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
5152

5253
// Connection successful. WebSocket is open.
5354

55+
// keepalive timeout
56+
keepalive_seconds := KEEPALIVE_TIMEOUT_SECONDS
57+
if keepalive_seconds_string := r.URL.Query().Get("keepalive_timeout_seconds"); keepalive_seconds_string != "" {
58+
if val, err := strconv.Atoi(keepalive_seconds_string); err == nil && val >= 10 && val <= 600 {
59+
keepalive_seconds = val
60+
}
61+
}
62+
keepalive_duration := time.Duration(keepalive_seconds) * time.Second
63+
5464
// Get connected at time and set automatic read timeout
5565
connectedAtTimestamp := time.Now().UTC().Format(time.RFC3339Nano)
56-
conn.SetReadDeadline(time.Now().Add(time.Second * KEEPALIVE_TIMEOUT_SECONDS))
66+
conn.SetReadDeadline(time.Now().Add(keepalive_duration))
5767

5868
client := &Client{
5969
clientName: util.RandomGUID()[:8],
@@ -129,7 +139,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
129139
Session: WelcomeMessagePayloadSession{
130140
ID: fmt.Sprintf("%v_%v", ws.ServerId, client.clientName),
131141
Status: "connected",
132-
KeepaliveTimeoutSeconds: KEEPALIVE_TIMEOUT_SECONDS,
142+
KeepaliveTimeoutSeconds: keepalive_seconds,
133143
ReconnectUrl: nil,
134144
ConnectedAt: connectedAtTimestamp,
135145
},
@@ -156,7 +166,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
156166
}
157167

158168
// Set up ping/pong and keepalive handling
159-
client.keepAliveTimer = time.NewTicker(10 * time.Second)
169+
client.keepAliveTimer = time.NewTicker(keepalive_duration)
160170
client.pingTimer = time.NewTicker(5 * time.Second)
161171
client.keepAliveLoopChan = make(chan struct{})
162172
client.pingLoopChan = make(chan struct{})

0 commit comments

Comments
 (0)