Skip to content
Draft
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
2 changes: 1 addition & 1 deletion docs/pages/operation/websocket.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ server:

A connection over a cap is refused with `429` before the upgrade; a subscription over `maxSubscriptions` gets a JSON-RPC error and the connection stays open. Refusals are counted in `erpc_websocket_rejected_total`. The caps are per instance, so a fleet admits up to N times each.

The upgrade request must name a network (`/<project-id>/<architecture>/<chain-id>`, or a path that [domain aliasing](/operation/url) resolves to one); otherwise it is rejected with `400`. On shutdown eRPC closes every client connection with `1001` (going away) straight away so clients reconnect to another instance; in-flight requests get up to `server.waitBeforeShutdown` to answer first.
The upgrade request must name a network (`/<project-id>/<architecture>/<chain-id>`, or a path that [domain aliasing](/operation/url) resolves to one); otherwise it is rejected with `400`. On shutdown eRPC closes every client connection with `1001` (going away) straight away so clients reconnect to another instance; in-flight requests get up to `server.waitBeforeShutdown` to answer first. New connections are refused with `503` before the upgrade from then on.

## Cost frames

Expand Down
10 changes: 9 additions & 1 deletion erpc/ws_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func (s *HttpServer) handleWebSocket(
) {
wsCfg := s.serverCfg.WebSocket

// Refused before the upgrade once shutdown has begun, so the client's dial
// fails instead of completing the handshake and being closed right away.
if s.draining.Load() {
lg.Debug().Msg("websocket connection refused: server shutting down")
http.Error(w, "server shutting down", http.StatusServiceUnavailable)
return
}

// Checked before the upgrade so a refused client gets a plain 429.
clientIP := s.resolveRealClientIP(r)
if reason, ok := s.wsConnLimiter.acquire(clientIP); !ok {
Expand Down Expand Up @@ -132,7 +140,7 @@ func (s *HttpServer) handleWebSocket(
lg.Info().Str("connId", wsc.id).Str("remoteAddr", r.RemoteAddr).Msg("websocket connection established")

// Store before checking draining so shutdownWebSockets either sees this
// connection or we see its flag.
// connection or we see its flag (shutdown began during the handshake).
s.activeWsConns.Store(wsc.id, wsc)
telemetry.GaugeHandle(telemetry.MetricWebsocketConnectionsActive, project.Config.Id).Inc()
if s.draining.Load() {
Expand Down
18 changes: 10 additions & 8 deletions erpc/ws_server_hardening_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ func TestWebSocket_UnsubscribeIsScopedToConnection(t *testing.T) {
}

// Shutdown closes client connections with 1001 as soon as it starts, not
// after waitBeforeShutdown, and lets in-flight requests answer first.
// after waitBeforeShutdown, lets in-flight requests answer first, and
// refuses new connections before the upgrade.
func TestWebSocket_ShutdownClosesPromptlyAfterInflightRequests(t *testing.T) {
util.ResetGock()
defer util.ResetGock()
Expand Down Expand Up @@ -299,13 +300,14 @@ func TestWebSocket_ShutdownClosesPromptlyAfterInflightRequests(t *testing.T) {
require.ErrorAs(t, err, &closeErr)
assert.Equal(t, websocket.CloseGoingAway, closeErr.Code)

// A draining server closes new connections straight away.
late := dialWs(t, addr)
defer late.Close()
_ = late.SetReadDeadline(time.Now().Add(3 * time.Second))
_, _, err = late.ReadMessage()
require.ErrorAs(t, err, &closeErr)
assert.Equal(t, websocket.CloseGoingAway, closeErr.Code)
// A draining server refuses new connections with 503 before the upgrade.
late, lateResp, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://%s/test_ws/evm/123", addr), nil)
if late != nil {
_ = late.Close()
}
require.ErrorIs(t, err, websocket.ErrBadHandshake, "a draining server must not complete the handshake")
require.NotNil(t, lateResp)
assert.Equal(t, http.StatusServiceUnavailable, lateResp.StatusCode)
}

// A peer that never answers pings is disconnected.
Expand Down
Loading