You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR enables a new "local" storage type configuration option which swaps the Redis driver to a local non-locking hash map. It also mocks the Queue interface to provide error-free pubsub (obviously it won't work in an actual cluster).
To test it, simply disable analytics, and set the storage type from "redis" to "local".
The local store does all complex operations in-memory, and does not rely on any underlying k/v data store capability (which means it could be used for more data stores even if they don't support Lists, Sets, or Sorted Sets).
Providing a stateless gateway that can be deployed without a dependency makes time-to-initial-value faster for end-users. As it supports in-memory operations, quotas, rate-limits, and local tokens all work without any change. This is also useful for our own developers running tests, and existing customers that are spinning up test environments for their own engineers.
How This Has Been Tested
Manually tested the gateway (rate limit and quota only)
Underlying library passes all storage tests (keyvalue, set, sortedset, list, and queue)
Types of changes
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 change)
Refactoring or add test (improvements in base code or adds test coverage to functionality)
Checklist
I ensured that the documentation is up to date
I explained why this PR updates go.mod in detail with reasoning why it's required
I would like a code coverage CI quality gate exception and have explained why
PR Type
Enhancement
Description
Added support for a new "local" storage type configuration option which swaps the Redis driver to a local non-locking hash map.
Modified gateway initialization to handle "local" storage type for analytics and general storage connection.
Updated NewConnector function to handle "local" storage type.
Added a replacement directive in go.mod for the local storage module.
Changes walkthrough 📝
Relevant files
Enhancement
server.go
Add support for "local" storage type in gateway initialization
gateway/server.go
Added condition to support "local" storage type for analytics.
Modified error message to include "local" storage type.
Configuration Validation The error message in line 1252 is misleading as it only mentions Redis, despite the code now supporting 'local' storage type. This could confuse users about the valid configuration options.
Connector Initialization The implementation for the 'local' storage type in line 243 does not handle potential errors that might occur during the creation of a new connector. It assumes success without any error checking.
Why: This suggestion addresses a potential bug by preventing a nil pointer dereference, which is crucial for the stability and reliability of the code.
10
Enhancement
Add a conditional check for analytics enablement before storage type validation
Add a check to ensure that analytics are enabled before checking the storage type, to avoid unnecessary log errors when analytics are disabled.
-if gwConfig.EnableAnalytics && gwConfig.Storage.Type != "redis" && gwConfig.Storage.Type != "local" {- mainLog.Fatal("Analytics requires Redis Storage backend, please enable Redis in the tyk.conf file.")+if gwConfig.EnableAnalytics {+ if gwConfig.Storage.Type != "redis" && gwConfig.Storage.Type != "local" {+ mainLog.Fatal("Analytics requires Redis Storage backend, please enable Redis in the tyk.conf file.")+ }
}
Suggestion importance[1-10]: 9
Why: Adding the conditional check ensures that the storage type validation is only performed when analytics are enabled, preventing unnecessary log errors and improving code efficiency.
9
Improve the error message for unsupported storage types
Consider adding a more specific error message for when neither 'redis' nor 'local' storage types are configured. This will help users understand that the system supports only these two types and that one of them must be configured correctly.
if gwConfig.Storage.Type != "redis" && gwConfig.Storage.Type != "local" {
- mainLog.Fatal("storage connection details not set, please ensure that the storage type is set to Redis and that the connection parameters are correct.")+ mainLog.Fatal("Unsupported storage type configured. Please ensure that the storage type is set to either 'redis' or 'local' and that the connection parameters are correct.")
}
Suggestion importance[1-10]: 7
Why: The suggestion improves the clarity of the error message, making it more specific about the supported storage types. This enhances user understanding and debugging.
7
Maintainability
Refactor storage type validation into a separate function
Refactor the condition to check the storage type to a separate function to improve code readability and maintainability.
-if gwConfig.Storage.Type != "redis" && gwConfig.Storage.Type != "local" {+if !isValidStorageType(gwConfig.Storage.Type) {
mainLog.Fatal("storage connection details not set, please ensure that the storage type is set to Redis and that the connection parameters are correct.")
}
+// New function+func isValidStorageType(storageType string) bool {+ return storageType == "redis" || storageType == "local"+}+
Suggestion importance[1-10]: 8
Why: Refactoring the storage type validation into a separate function improves code readability and maintainability, making the codebase easier to manage and extend.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
This PR uses the new "local" storage driver and is dependent on the in-mem-kv branch/PR in the storage repo to enable Tyk to run without Redis.
Description
This PR enables a new "local" storage type configuration option which swaps the Redis driver to a local non-locking hash map. It also mocks the Queue interface to provide error-free pubsub (obviously it won't work in an actual cluster).
To test it, simply disable analytics, and set the storage type from
"redis"
to"local"
.The local store does all complex operations in-memory, and does not rely on any underlying k/v data store capability (which means it could be used for more data stores even if they don't support Lists, Sets, or Sorted Sets).
Related Issue
JIRA Issue
Related PR
Motivation and Context
Providing a stateless gateway that can be deployed without a dependency makes time-to-initial-value faster for end-users. As it supports in-memory operations, quotas, rate-limits, and local tokens all work without any change. This is also useful for our own developers running tests, and existing customers that are spinning up test environments for their own engineers.
How This Has Been Tested
Manually tested the gateway (rate limit and quota only)
Underlying library passes all storage tests (
keyvalue
,set
,sortedset
,list
, andqueue
)Types of changes
Checklist
PR Type
Enhancement
Description
NewConnector
function to handle "local" storage type.go.mod
for the local storage module.Changes walkthrough 📝
server.go
Add support for "local" storage type in gateway initialization
gateway/server.go
connection_handler.go
Handle "local" storage type in NewConnector function
storage/connection_handler.go
function.
go.mod
Add local replacement directive for storage module
go.mod