TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.51k stars 1.07k forks source link

[TT-11726]: fix delay in creating multiple large keys #6414

Closed kofoworola closed 1 month ago

kofoworola commented 1 month ago

User description

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

Checklist


PR Type

Bug fix


Description


Changes walkthrough 📝

Relevant files
Enhancement
auth_manager.go
Make key deletions asynchronous in `ResetQuota` method     

gateway/auth_manager.go
  • Made key deletions asynchronous using goroutines.
  • Updated ResetQuota method to improve performance.
  • +3/-3     

    💡 PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    github-actions[bot] commented 1 month ago

    API Changes

    no api changes detected
    github-actions[bot] commented 1 month ago

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Concurrency Concern
    The use of goroutines for `DeleteRawKey` and `deleteRawKeysWithAllowanceScope` methods might introduce race conditions or concurrency issues if not handled properly. Ensure that these operations are safe to perform concurrently and consider adding synchronization mechanisms if necessary.
    github-actions[bot] commented 1 month ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add synchronization and error handling to the asynchronous key deletion operations ___ **Using goroutines for deleting keys asynchronously can lead to race conditions or
    unhandled errors if not properly synchronized or monitored. Consider using a wait
    group or error handling mechanism to ensure that all operations complete
    successfully and any errors are appropriately handled.** [gateway/auth_manager.go [78-82]](https://github.com/TykTechnologies/tyk/pull/6414/files#diff-311e18b071d244ed1615c0019215b633278679df373288b3451bcc5cf7c52c4eR78-R82) ```diff -go b.store.DeleteRawKey(rateLimiterSentinelKey) -go b.store.DeleteRawKey(rawKey) -go b.deleteRawKeysWithAllowanceScope(b.store, session, keyName) +var wg sync.WaitGroup +wg.Add(3) +go func() { + defer wg.Done() + if err := b.store.DeleteRawKey(rateLimiterSentinelKey); err != nil { + log.Errorf("Failed to delete rate limiter key: %v", err) + } +}() +go func() { + defer wg.Done() + if err := b.store.DeleteRawKey(rawKey); err != nil { + log.Errorf("Failed to delete raw key: %v", err) + } +}() +go func() { + defer wg.Done() + if err := b.deleteRawKeysWithAllowanceScope(b.store, session, keyName); err != nil { + log.Errorf("Failed to delete keys with allowance scope: %v", err) + } +}() +wg.Wait() ```
    Suggestion importance[1-10]: 9 Why: The suggestion correctly identifies a potential issue with using goroutines for asynchronous operations without proper synchronization or error handling. Implementing a wait group and error handling ensures that all operations complete successfully and any errors are appropriately managed, which is crucial for maintaining the integrity and reliability of the system.
    9
    sonarcloud[bot] commented 1 month ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    100.0% Coverage on New Code
    0.0% Duplication on New Code

    See analysis details on SonarCloud

    kofoworola commented 1 month ago

    CLosed due to it being stale