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

Add new websocket handler and skeleton for its deps #6630

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

illia-malachyn
Copy link
Contributor

@illia-malachyn illia-malachyn commented Nov 8, 2024

Closes #6593 #6583 #6617
Relates to #6584

  • Added websocket controller
  • Added mock block provider
  • Added data provider factory
  • Added websocket handler
  • Added websocket config
  • Added a tiny POC test for websocket handler

* Added websocket controller
* Added mock block provider
* Added data provider factory
* Added websocket handler
* Added websocket config
* Added a tiny POC test for websocket handler
@codecov-commenter
Copy link

codecov-commenter commented Nov 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 40.95%. Comparing base (acbc34f) to head (839c35c).
Report is 9 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #6630       +/-   ##
===========================================
- Coverage   41.22%   40.95%    -0.28%     
===========================================
  Files        2054       66     -1988     
  Lines      182347     9199   -173148     
===========================================
- Hits        75175     3767    -71408     
+ Misses     100871     5153    -95718     
+ Partials     6301      279     -6022     
Flag Coverage Δ
unittests 40.95% <ø> (-0.28%) ⬇️

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.

@illia-malachyn
Copy link
Contributor Author

Speaking of tests, I see it the following way: we should write a wrap websocket connection with our custom mock and write unit tests for websocket controller (it accepts ws conn as argument). This will require some code, so a separate task should be created for this #6631

return websocket.DefaultDialer.Dial(wsURL, nil)
}

func (s *WsHandlerSuite) TestSubscribeRequest() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is only to make sure we get back data from data providers. this should removed in favor of #6631

@Guitarheroua Guitarheroua self-requested a review November 8, 2024 16:18
engine/access/rest/websockets/config.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/controller.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/threadsafe_map.go Outdated Show resolved Hide resolved
engine/access/rest/websockets/models.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/data_provider/blocks.go Outdated Show resolved Hide resolved
* Use contexts as function arguments
* Move models to folder for consistency
* Change parse msg function
* Simplify mock block provider to remove dedlock

func (c *Controller) handleAction(ctx context.Context, action string, message interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than passing the action, you can use the type in the switch. this also allows you to avoid casting

engine/access/rest/websockets/data_provider/blocks.go Outdated Show resolved Hide resolved
func (c *Controller) handleUnsubscribe(ctx context.Context, msg models.UnsubscribeMessageRequest) {
id, err := uuid.Parse(msg.ID)
if err != nil {
c.logger.Debug().Err(err).Msg("error parsing message ID")
Copy link
Contributor

Choose a reason for hiding this comment

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

we should return an error response to the client here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added an appropriate TODO for every handler. I wanted to do it in a separate task.
We also have 2 more or less related issues for this #6724 #6642

I want to add tests alongside each request/response handler but it will require a lot of changes. This PR is already big enough.

func (c *Controller) handleSubscribe(ctx context.Context, msg models.SubscribeMessageRequest) {
dp := c.dataProvidersFactory.NewDataProvider(ctx, c.communicationChannel, msg.Topic)
c.dataProviders.Add(dp.ID(), dp)
dp.Run(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

we should return a response here

}

controller := NewWebSocketController(logger, h.websocketConfig, h.streamApi, h.streamConfig, conn)
controller.HandleConnection(context.TODO())
Copy link
Contributor

Choose a reason for hiding this comment

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

i believe this needs to be blocking, and HandleConnection is currently non-blocking

utils/concurrentmap/concurrent_map.go Show resolved Hide resolved
utils/concurrentmap/concurrent_map.go Show resolved Hide resolved
utils/concurrentmap/concurrent_map.go Show resolved Hide resolved
config Config
conn *websocket.Conn
communicationChannel chan interface{}
dataProviders *concurrentmap.ConcurrentMap[uuid.UUID, dp.DataProvider]
Copy link
Contributor

Choose a reason for hiding this comment

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

We could use string here, as a key, as this is what we get from Subscription

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think uuid is more compact, faster to compare, and better named for ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Access] Implement web socket handler for new web socket connection under /ws endpoint
5 participants