Skip to content

Commit 650a67b

Browse files
authored
Merge pull request #8012 from onflow/peter/speedup-websockets-keepalive-test
[Access] Speedup websockets keepalive test
2 parents 7a3c1ee + 6dbc495 commit 650a67b

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

engine/access/rest/websockets/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ func NewDefaultWebsocketConfig() Config {
6464
InactivityTimeout: DefaultInactivityTimeout,
6565
}
6666
}
67+
68+
type KeepaliveConfig struct {
69+
PingPeriod time.Duration
70+
PongWait time.Duration
71+
}
72+
73+
func DefaultKeepaliveConfig() KeepaliveConfig {
74+
return KeepaliveConfig{
75+
PingPeriod: PingPeriod,
76+
PongWait: PongWait,
77+
}
78+
}

engine/access/rest/websockets/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type Controller struct {
136136
dataProviderFactory dp.DataProviderFactory
137137
dataProvidersGroup *sync.WaitGroup
138138
limiter *rate.Limiter
139+
140+
keepaliveConfig KeepaliveConfig
139141
}
140142

141143
func NewWebSocketController(
@@ -158,6 +160,7 @@ func NewWebSocketController(
158160
dataProviderFactory: dataProviderFactory,
159161
dataProvidersGroup: &sync.WaitGroup{},
160162
limiter: limiter,
163+
keepaliveConfig: DefaultKeepaliveConfig(),
161164
}
162165
}
163166

@@ -213,13 +216,13 @@ func (c *Controller) configureKeepalive() error {
213216
// The Pong handler itself only resets the read deadline after receiving a Pong.
214217
// It doesn't set an initial deadline. The initial read deadline is crucial to prevent the server from waiting
215218
// forever if the client doesn't send Pongs.
216-
if err := c.conn.SetReadDeadline(time.Now().Add(PongWait)); err != nil {
219+
if err := c.conn.SetReadDeadline(time.Now().Add(c.keepaliveConfig.PongWait)); err != nil {
217220
return fmt.Errorf("failed to set the initial read deadline: %w", err)
218221
}
219222

220223
// Establish a Pong handler which sets the handler for pong messages received from the peer.
221224
c.conn.SetPongHandler(func(string) error {
222-
return c.conn.SetReadDeadline(time.Now().Add(PongWait))
225+
return c.conn.SetReadDeadline(time.Now().Add(c.keepaliveConfig.PongWait))
223226
})
224227

225228
return nil
@@ -235,7 +238,7 @@ func (c *Controller) keepalive(ctx context.Context) error {
235238
}
236239
}()
237240

238-
pingTicker := time.NewTicker(PingPeriod)
241+
pingTicker := time.NewTicker(c.keepaliveConfig.PingPeriod)
239242
defer pingTicker.Stop()
240243

241244
for {

engine/access/rest/websockets/controller_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,11 @@ func (s *WsControllerSuite) TestControllerShutdown() {
894894
}
895895

896896
func (s *WsControllerSuite) TestKeepaliveRoutine() {
897+
keepaliveConfig := KeepaliveConfig{
898+
PingPeriod: time.Microsecond,
899+
PongWait: 2 * time.Microsecond,
900+
}
901+
897902
s.T().Run("Successfully pings connection n times", func(t *testing.T) {
898903
conn := connmock.NewWebsocketConnection(t)
899904
conn.On("Close").Return(nil).Once()
@@ -923,9 +928,9 @@ func (s *WsControllerSuite) TestKeepaliveRoutine() {
923928

924929
factory := dpmock.NewDataProviderFactory(t)
925930
controller := NewWebSocketController(s.logger, s.wsConfig, conn, factory)
926-
controller.HandleConnection(context.Background())
931+
controller.keepaliveConfig = keepaliveConfig
927932

928-
conn.AssertExpectations(t)
933+
controller.HandleConnection(context.Background())
929934
})
930935

931936
s.T().Run("Error on write to closed connection", func(t *testing.T) {
@@ -938,15 +943,14 @@ func (s *WsControllerSuite) TestKeepaliveRoutine() {
938943

939944
factory := dpmock.NewDataProviderFactory(t)
940945
controller := NewWebSocketController(s.logger, s.wsConfig, conn, factory)
946+
controller.keepaliveConfig = keepaliveConfig
941947

942948
ctx, cancel := context.WithCancel(context.Background())
943949
defer cancel()
944950

945951
err := controller.keepalive(ctx)
946952
s.Require().Error(err)
947953
s.Require().ErrorIs(expectedError, err)
948-
949-
conn.AssertExpectations(t)
950954
})
951955

952956
s.T().Run("Error on write to open connection", func(t *testing.T) {
@@ -958,30 +962,28 @@ func (s *WsControllerSuite) TestKeepaliveRoutine() {
958962

959963
factory := dpmock.NewDataProviderFactory(t)
960964
controller := NewWebSocketController(s.logger, s.wsConfig, conn, factory)
965+
controller.keepaliveConfig = keepaliveConfig
961966

962967
ctx, cancel := context.WithCancel(context.Background())
963968
defer cancel()
964969

965970
err := controller.keepalive(ctx)
966971
s.Require().Error(err)
967972
s.Require().ErrorContains(err, "error sending ping")
968-
969-
conn.AssertExpectations(t)
970973
})
971974

972975
s.T().Run("Context cancelled", func(t *testing.T) {
973976
conn := connmock.NewWebsocketConnection(t)
974977
factory := dpmock.NewDataProviderFactory(t)
975978
controller := NewWebSocketController(s.logger, s.wsConfig, conn, factory)
979+
controller.keepaliveConfig = keepaliveConfig
976980

977981
ctx, cancel := context.WithCancel(context.Background())
978982
cancel() // Immediately cancel the context
979983

980984
// Start the keepalive process with the context canceled
981985
err := controller.keepalive(ctx)
982986
s.Require().NoError(err)
983-
984-
conn.AssertExpectations(t) // Should not invoke WriteMessage after context cancellation
985987
})
986988
}
987989

0 commit comments

Comments
 (0)