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

[Access] Implement keepalive routine with ping-ponging to ws connection in ws controller #6757

Merged

Conversation

UlyanaAndrukhiv
Copy link
Contributor

@UlyanaAndrukhiv UlyanaAndrukhiv commented Nov 25, 2024

Closes: #6638, #6639

Context

In this pull request implemented a keepalive routine to monitor WebSocket network connectivity using ping-pong messages.

Key Changes

  • Integrated a keepalive logic with for WebSocket controller to detect and handle connectivity issues and ensured the keepalive operates independently, managing ping-pong messages and monitoring connection health.
  • Added mechanisms to disable the WebSocket connection and gracefully shut down affected components when issues are detected.
  • Added tests to validate connectivity issue detection and proper shutdown handling, including edge cases like missed pong responses and delayed ping intervals.

@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2024

Codecov Report

Attention: Patch coverage is 61.84971% with 66 lines in your changes missing coverage. Please review.

Project coverage is 41.04%. Comparing base (61a0b0e) to head (cfad79f).
Report is 44 commits behind head on master.

Files with missing lines Patch % Lines
...ccess/rest/websockets/mock/websocket_connection.go 70.00% 11 Missing and 13 partials ⚠️
engine/access/rest/websockets/controller.go 69.11% 16 Missing and 5 partials ⚠️
engine/access/rest/websockets/connection.go 0.00% 18 Missing ⚠️
...access/rest/websockets/legacy/websocket_handler.go 66.66% 2 Missing ⚠️
engine/access/rest/websockets/handler.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6757      +/-   ##
==========================================
+ Coverage   40.96%   41.04%   +0.08%     
==========================================
  Files        2093     2095       +2     
  Lines      184478   184618     +140     
==========================================
+ Hits        75577    75785     +208     
+ Misses     102579   102470     -109     
- Partials     6322     6363      +41     
Flag Coverage Δ
unittests 41.04% <61.84%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@Guitarheroua Guitarheroua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the first round of review - looks cool!

engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller_test.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller_test.go Outdated Show resolved Hide resolved
s.Require().True(ok)
msg, err := json.Marshal(requestMessage)
s.Require().NoError(err)
*reqMsg = msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do? isn't reqMsg a local variable that's thrown away at the end of the function?

Copy link
Contributor Author

@UlyanaAndrukhiv UlyanaAndrukhiv Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of this code is to update the first argument of ReadJSON. Even though reqMsg is a local variable, it serves as a pointer to the argument passed into the mock. By modifying *reqMsg, the code changes the actual memory location that the pointer refers to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed vs passing the info in somehow? Can you add some comments explaining. it's unclear to me so it will most likely be unclear to the next person that works on these tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do it like that

	conn.On("ReadJSON", requestMessage).
		Return(nil).
		Once()

Copy link
Contributor Author

@UlyanaAndrukhiv UlyanaAndrukhiv Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do it like that

	conn.On("ReadJSON", requestMessage).
		Return(nil).
		Once()

This will not be worked cause, the strict pointer comparison by the mock library expects the exact pointer passed during the mock setup to be the same as the pointer passed during runtime. Since these pointers are not the same, the mock is not triggered.

As I understand, to properly mock the ReadJSON call, there are 2 options:

  1. Use mock.Anything as argument and then modify the argument inside the Run() callback. (This approach is implemented in current test)
  2. Use mock.MatchedBy, something like
s.connection.On("ReadJSON", mock.MatchedBy(func(arg interface{}) bool {
			rawMsg, ok := arg.(*json.RawMessage)
			if !ok {
				return false
			}
			*rawMsg = msg
			return true
		})).
			Return(nil).
			Once()

I’ve added a detailed comment in the code explaining why ReadJSON is mocked this way

engine/access/rest/websockets/controller_test.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
s.Require().True(ok)
msg, err := json.Marshal(requestMessage)
s.Require().NoError(err)
*reqMsg = msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed vs passing the info in somehow? Can you add some comments explaining. it's unclear to me so it will most likely be unclear to the next person that works on these tests.

Copy link
Contributor

@Guitarheroua Guitarheroua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have any more comments for this PR. LGTM!

Copy link
Contributor

@peterargue peterargue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a few small comments, but otherwise looks good.

engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller_test.go Outdated Show resolved Hide resolved
@peterargue peterargue added this pull request to the merge queue Dec 17, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 17, 2024
@peterargue peterargue added this pull request to the merge queue Dec 17, 2024
Merged via the queue into onflow:master with commit ed6a2c4 Dec 17, 2024
55 checks passed
@peterargue peterargue deleted the UlyanaAndrukhiv/6639-ws-ping-pong branch December 17, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants