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

fix(depth-check): correctly handle 'depth not enough' error condition #1707

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

tolgaOzen
Copy link
Member

@tolgaOzen tolgaOzen commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Updated API version from v1.1.4 to v1.1.5 across multiple documentation files.
  • Improvements

    • Enhanced permission checking mechanisms for better readability and maintainability.
    • Improved thread safety in request and response metadata handling.
  • Bug Fixes

    • Simplified codebase by removing unnecessary functions related to permission checks.

Copy link

coderabbitai bot commented Oct 18, 2024

Walkthrough

The changes involve updating the version number from "v1.1.4" to "v1.1.5" across multiple files associated with the Permify API documentation and versioning. Additionally, modifications were made to the permission checking mechanisms in the code, including the introduction of a new helper function for metadata responses, updates to ensure thread safety in concurrent operations, and the removal of outdated functions.

Changes

File Path Change Summary
docs/api-reference/apidocs.swagger.json Version updated from "v1.1.4" to "v1.1.5".
docs/api-reference/openapiv2/apidocs.swagger.json Version updated from "v1.1.4" to "v1.1.5" in the info section.
internal/info.go Version constant updated from "v1.1.4" to "v1.1.5".
proto/base/v1/openapi.proto Version updated from "v1.1.4" to "v1.1.5" in the info section.
internal/engines/check.go Introduced emptyResponseMetadata() function for creating empty metadata responses.
internal/invoke/invoke.go Updated Check method in DirectInvoker to use atomic operations for thread-safe metadata handling.
internal/invoke/utils.go Removed increaseCheckCount and decreaseDepth functions; updated checkDepth to use atomic operations.

Possibly related PRs

🐰 "In the garden of code, we hop with glee,
Versions updated, as bright as can be!
With functions refined, and threads that are safe,
Our API blooms, in the digital waif.
So let’s celebrate, with a joyful cheer,
For every small change, brings new hope near!" 🌼


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (2)
internal/invoke/invoke.go (2)

197-198: LGTM: Thread-safe check count increment

The use of atomic.AddInt32 for incrementing the check count ensures thread-safety in concurrent scenarios. This is an improvement over the previous non-atomic operation.

Consider updating the comment to reflect the atomic nature of the operation:

- // increaseCheckCount increments the CheckCount value in the response metadata by 1.
+ // Atomically increment the CheckCount value in the response metadata by 1.

Line range hint 1-458: Summary: Improved thread-safety in Check method

The changes in this file focus on enhancing thread-safety in the Check method of the DirectInvoker struct. The introduction of atomic operations for depth decrement and check count increment ensures that these operations are performed safely in concurrent scenarios. These modifications are well-implemented and align with best practices for concurrent programming.

Consider reviewing other methods in this file (and related files) that might benefit from similar thread-safety improvements, especially if they are likely to be called concurrently.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c5e27d7 and e0e4925.

⛔ Files ignored due to path filters (1)
  • pkg/pb/base/v1/openapi.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (7)
  • docs/api-reference/apidocs.swagger.json (1 hunks)
  • docs/api-reference/openapiv2/apidocs.swagger.json (1 hunks)
  • internal/engines/check.go (18 hunks)
  • internal/info.go (1 hunks)
  • internal/invoke/invoke.go (3 hunks)
  • internal/invoke/utils.go (1 hunks)
  • proto/base/v1/openapi.proto (1 hunks)
✅ Files skipped from review due to trivial changes (4)
  • docs/api-reference/apidocs.swagger.json
  • docs/api-reference/openapiv2/apidocs.swagger.json
  • internal/info.go
  • proto/base/v1/openapi.proto
🧰 Additional context used
🔇 Additional comments (12)
internal/invoke/invoke.go (2)

5-5: LGTM: Import of sync/atomic package

The addition of the sync/atomic package import is appropriate for the subsequent changes involving atomic operations in the Check method.


179-179: LGTM: Thread-safe depth decrement

The use of atomic.AddInt32 for decrementing the depth ensures thread-safety in concurrent scenarios. This is an improvement over the previous non-atomic operation.

internal/engines/check.go (8)

64-66: LGTM: Improved consistency in metadata creation

The change to use emptyResponseMetadata() instead of directly instantiating base.PermissionCheckResponseMetadata{} is a good improvement. It centralizes the creation of empty response metadata, making the code more consistent and easier to maintain.


116-117: LGTM: Consistent use of emptyResponseMetadata()

The change to use emptyResponseMetadata() here is consistent with the previous changes and maintains the improved pattern for creating empty response metadata.


Line range hint 272-282: LGTM: Consistent metadata handling in error cases

The changes in this segment consistently use emptyResponseMetadata() for error cases, which aligns with the overall improvement in metadata handling throughout the file.


Line range hint 302-326: LGTM: Consistent metadata handling in checkDirectRelation

The changes in the checkDirectRelation method consistently use emptyResponseMetadata(), which aligns with the overall improvement in metadata handling and maintains consistency throughout the file.


Line range hint 354-504: LGTM: Consistent metadata handling across multiple methods

The changes in this segment demonstrate a consistent approach to metadata handling across multiple methods, including checkTupleToUserSet, checkComputedUserSet, and checkDirectAttribute. This improves the overall consistency and maintainability of the code.


Line range hint 536-631: LGTM: Consistent metadata handling in checkDirectCall

The changes in the checkDirectCall method consistently use emptyResponseMetadata(), which aligns with the overall improvement in metadata handling and maintains consistency throughout the file.


Line range hint 639-911: LGTM: Consistent metadata handling and new emptyResponseMetadata function

The changes in this final segment complete the consistent use of emptyResponseMetadata() throughout the file. The addition of the emptyResponseMetadata() function at the end provides a centralized point for creating empty metadata, which improves maintainability and consistency.

The new function is simple and correctly initializes the CheckCount to 0:

func emptyResponseMetadata() *base.PermissionCheckResponseMetadata {
	return &base.PermissionCheckResponseMetadata{
		CheckCount: 0,
	}
}

This implementation is correct and aligns with the usage throughout the file.


Line range hint 1-911: Overall: Excellent improvement in metadata handling

The changes made throughout this file significantly improve the consistency and maintainability of the code by introducing and consistently using the emptyResponseMetadata() function. This refactoring provides a single point of control for creating empty metadata, which will make future modifications easier and reduce the likelihood of inconsistencies.

Key improvements:

  1. Consistent metadata creation across all methods
  2. Centralized empty metadata creation with emptyResponseMetadata()
  3. Improved readability and maintainability

These changes align well with best practices for code organization and will make the codebase more robust and easier to maintain in the future.

internal/invoke/utils.go (2)

5-5: Importing sync/atomic for atomic operations

The inclusion of the "sync/atomic" package is necessary to perform atomic operations on the Depth field, ensuring thread safety in concurrent environments.


12-14: Using atomic operation to prevent data races on Depth

Switching to atomic.LoadInt32(&request.GetMetadata().Depth) ensures that the Depth field is read atomically. This prevents potential data races when Depth may be accessed or modified concurrently by multiple goroutines.

@tolgaOzen tolgaOzen merged commit 3c7e9a9 into master Oct 18, 2024
12 checks passed
@tolgaOzen tolgaOzen deleted the fix/correct-depth-not-enough-error branch October 18, 2024 11:54
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.

1 participant