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

(DAL) Remove global variable #1813

Merged
merged 2 commits into from
Jul 16, 2024
Merged

(DAL) Remove global variable #1813

merged 2 commits into from
Jul 16, 2024

Conversation

nick-bisonai
Copy link
Collaborator

Description

Refactor package to remove global variables

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.

Deployment

  • Should publish npm package
  • Should publish Docker image

@nick-bisonai nick-bisonai self-assigned this Jul 15, 2024
Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Walkthrough

Walkthrough

The changes focus on refactoring and improving the modularity of the DAL API server initialization and its components. Key modifications include the restructuring of controller setup and usage, introducing a keyCache for API key management, and adjustments to test cases. Additionally, the handleWebsocket function is now exported, and middleware handling is refined to enhance context management.

Changes

Files Change Summary
node/pkg/dal/api/controller.go Refactored Setup to return Controller pointer, exported HandleWebsocket, adjusted functions to use controller instance from context.
node/pkg/dal/api/route.go Updated api.Get("/ws", ...) to use HandleWebsocket instead of ApiController.handleWebsocket.
node/pkg/dal/app.go Introduced keyCache in DAL API server initialization; refactored setup and shutdown procedures; updated controller setup to include keyCache.
node/pkg/dal/collector/collector.go Minor change: added an empty line before a defer statement in processIncomingData.
node/pkg/dal/tests/api_test.go Removed testItems.Controller.Start(ctx) calls, replaced references to testItems.Collector with testItems.Controller.Collector for specific operations.
node/pkg/dal/tests/collector_test.go Added setup for websocket communication, data processing in tests, and logging enhancements; replaced collector := testItems.Collector with collector := testItems.Controller.Collector.
node/pkg/dal/tests/main_test.go Removed Collector field from TestItems struct, added keycache import, modified setup function for key insertion and initialization, adjusted cleanup to use Controller.Collector.
node/pkg/dal/utils/initializer/initializer.go Added api.Controller and keycache.KeyCache parameters to Setup, handled new parameters, set EnablePrintRoutes to false, added middleware for context management, included stats.StatsMiddleware.

Poem

In code's vast and winding lane,
The Controller leads again.
With Websocket now set free,
And keyCache for security.
Tests revamped, and routes anew,
In harmony, our functions grew.
🐇✨ Code's melody, just for you!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nick-bisonai nick-bisonai linked an issue Jul 15, 2024 that may be closed by this pull request
@nick-bisonai nick-bisonai marked this pull request as ready for review July 16, 2024 01:07
@nick-bisonai nick-bisonai requested a review from a team as a code owner July 16, 2024 01:07
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3a88c7f and 4f98e27.

Files selected for processing (8)
  • node/pkg/dal/api/controller.go (9 hunks)
  • node/pkg/dal/api/route.go (1 hunks)
  • node/pkg/dal/app.go (1 hunks)
  • node/pkg/dal/collector/collector.go (1 hunks)
  • node/pkg/dal/tests/api_test.go (8 hunks)
  • node/pkg/dal/tests/collector_test.go (4 hunks)
  • node/pkg/dal/tests/main_test.go (4 hunks)
  • node/pkg/dal/utils/initializer/initializer.go (6 hunks)
Files skipped from review due to trivial changes (1)
  • node/pkg/dal/collector/collector.go
Additional context used
Learnings (1)
node/pkg/dal/api/route.go (1)
Learnt from: nick-bisonai
PR: Bisonai/orakl#1791
File: node/pkg/dal/api/controller.go:0-0
Timestamp: 2024-07-12T02:42:57.258Z
Learning: In the `node/pkg/dal/api/controller.go`, the `handleWebsocket` function no longer requires explicit validation for the `X-Api-Key` header, as this is handled by the `gofiber keyauth` middleware in `initializer.go`.
Additional comments not posted (7)
node/pkg/dal/api/route.go (1)

14-14: Approved: Updated websocket handler usage.

The change to use HandleWebsocket in the websocket route is consistent with the PR's goal to refactor and improve the code structure.

node/pkg/dal/app.go (1)

39-41: Ensure proper shutdown sequence.

The deferred shutdown of the application is a good practice. However, ensure that other resources are also cleanly shutdown to prevent any resource leakage.

node/pkg/dal/utils/initializer/initializer.go (1)

Line range hint 28-75: Refactored Setup function with enhanced error handling and middleware setup.

The changes to the Setup function introduce apiController and keyCache as parameters, enhancing the function's modularity and reducing reliance on global state. The added null checks for apiController and keyCache improve robustness by preventing potential runtime errors due to null references.

The middleware setup now includes context and key cache information, which aligns with the PR's objective to enhance the application's configurability and maintainability.

node/pkg/dal/api/controller.go (2)

Line range hint 18-36: Enhanced Setup function with improved error handling and structure.

The Setup function now properly handles errors from configuration requests and collector creation, providing detailed log messages for failures. This improves the function's reliability and maintainability by ensuring that errors are not silently ignored and are adequately reported.

The separation of concerns is evident as the function now solely focuses on setting up the controller with the necessary configurations and collector, rather than handling multiple unrelated tasks.


Line range hint 105-157: Improved WebSocket handling in HandleWebsocket function.

The HandleWebsocket function has been refactored to use local storage (conn.Locals) for passing the apiController and context, aligning with best practices for handling state in WebSocket servers. This change reduces dependencies on global variables and enhances the function's testability and reusability.

The detailed error handling and logging within the function provide clear diagnostics in case of failures, which is crucial for maintaining live WebSocket connections.

node/pkg/dal/tests/api_test.go (2)

Line range hint 44-68: Updated test cases to reflect new controller structure and functionality.

The test cases have been updated to use the new Controller structure, ensuring that they remain effective and relevant after the refactoring. The tests now properly handle new configurations and collector setups, aligning with the changes in the main codebase.

The use of Controller.Collector in the tests ensures that the collector's functionality is thoroughly tested, which is crucial for maintaining the reliability of data processing and retrieval features.


Line range hint 143-203: Enhanced WebSocket testing in TestApiWebsocket.

The test case for WebSocket functionality (TestApiWebsocket) has been significantly improved to test the new WebSocket handling code. The test ensures that the WebSocket connections are properly managed and that data is correctly processed and sent over the WebSocket.

This test is crucial for verifying the robustness and reliability of the WebSocket implementation, especially after the refactoring changes made in the main codebase.

node/pkg/dal/app.go Show resolved Hide resolved
node/pkg/dal/tests/collector_test.go Show resolved Hide resolved
node/pkg/dal/tests/main_test.go Show resolved Hide resolved
return err
}
defer func() {
_ = app.Shutdown()
Copy link
Contributor

Choose a reason for hiding this comment

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

just out of curiosity, I've seen this pattern a lot in the code where the return value is assigned to a blank identifier, which is not used. Afaik the blank identifier is useful when a function returns multiple values and you are not interested in some of them. Is it still a good practice to use the blank identifier in this way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so,
it didn't pass the linter when I didn't assign blank identifier, so it kind of became habit I think

Copy link
Contributor

@Intizar-T Intizar-T left a comment

Choose a reason for hiding this comment

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

lgtm! lets merge this and I'll continue my dal changes after this merge

@nick-bisonai nick-bisonai merged commit f3389a3 into master Jul 16, 2024
2 checks passed
@nick-bisonai nick-bisonai deleted the feat/refactor-dal-api branch July 16, 2024 07:13
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.

(DAL) refactor data availability layer api
2 participants