-
Notifications
You must be signed in to change notification settings - Fork 600
fix: Added caching for key endpoints that lookup keys by id / whoami endpoint #4203
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
Closed
Akhileshait
wants to merge
10
commits into
unkeyed:main
from
Akhileshait:fix-caching-for-key-endpoints-#4150
Closed
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bb66900
added caching for key endpoints that loopkup keys by id/whoami
Akhileshait 02adfcd
Merge branch 'main' of https://github.com/Akhileshait/unkey into fix-…
Akhileshait 0495a75
updated the remaining handlers
Akhileshait 1ccbf61
fixed missing liveKeycache issue
Akhileshait 41adb66
fixes
Akhileshait 8dc1bf5
Merge branch 'main' into fix-caching-for-key-endpoints-#4150
Flo4604 1bfe0e0
Merge branch 'main' of https://github.com/Akhileshait/unkey into fix-…
Akhileshait b16c0aa
fixed merge conflicts
Akhileshait a5e915c
Merge branch 'fix-caching-for-key-endpoints-#4150' of https://github.…
Akhileshait a991c68
Merge branch 'main' into fix-caching-for-key-endpoints-#4150
Flo4604 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ type Handler struct { | |
| Keys keys.KeyService | ||
| Auditlogs auditlogs.AuditLogService | ||
| KeyCache cache.Cache[string, db.CachedKeyData] | ||
| LiveKeyCache cache.Cache[string, db.FindLiveKeyByIDRow] | ||
| UsageLimiter usagelimiter.Service | ||
| } | ||
|
|
||
|
|
@@ -61,7 +62,17 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { | |
| return err | ||
| } | ||
|
|
||
| key, err := db.Query.FindLiveKeyByID(ctx, h.DB.RO(), req.KeyId) | ||
| key, _, err := h.LiveKeyCache.SWR(ctx, req.KeyId, func(ctx context.Context) (db.FindLiveKeyByIDRow, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too |
||
| return db.Query.FindLiveKeyByID(ctx, h.DB.RO(), req.KeyId) | ||
| }, func(err error) cache.Op { | ||
| if err == nil { | ||
| return cache.WriteValue | ||
| } | ||
| if db.IsNotFound(err) { | ||
| return cache.WriteNull | ||
| } | ||
| return cache.Noop | ||
| }) | ||
Flo4604 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| if db.IsNotFound(err) { | ||
| return fault.Wrap( | ||
|
|
@@ -267,6 +278,7 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { | |
| } | ||
|
|
||
| h.KeyCache.Remove(ctx, key.Hash) | ||
| h.LiveKeyCache.Remove(ctx, key.ID) | ||
| if err := h.UsageLimiter.Invalidate(ctx, key.ID); err != nil { | ||
| h.Logger.Error("Failed to invalidate usage limit", | ||
| "error", err.Error(), | ||
|
|
||
This file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ type Handler struct { | |
| Keys keys.KeyService | ||
| Auditlogs auditlogs.AuditLogService | ||
| KeyCache cache.Cache[string, db.CachedKeyData] | ||
| LiveKeyCache cache.Cache[string, db.FindLiveKeyByIDRow] | ||
| UsageLimiter usagelimiter.Service | ||
| } | ||
|
|
||
|
|
@@ -64,7 +65,17 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { | |
| return err | ||
| } | ||
|
|
||
| key, err := db.Query.FindLiveKeyByID(ctx, h.DB.RO(), req.KeyId) | ||
| key, _, err := h.LiveKeyCache.SWR(ctx, req.KeyId, func(ctx context.Context) (db.FindLiveKeyByIDRow, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too |
||
| return db.Query.FindLiveKeyByID(ctx, h.DB.RO(), req.KeyId) | ||
| }, func(err error) cache.Op { | ||
| if err == nil { | ||
| return cache.WriteValue | ||
| } | ||
| if db.IsNotFound(err) { | ||
| return cache.WriteNull | ||
| } | ||
| return cache.Noop | ||
| }) | ||
Flo4604 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| if db.IsNotFound(err) { | ||
| return fault.Wrap( | ||
|
|
@@ -627,6 +638,7 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { | |
| } | ||
|
|
||
| h.KeyCache.Remove(ctx, key.Hash) | ||
| h.LiveKeyCache.Remove(ctx, key.ID) | ||
| if req.Credits.IsSpecified() { | ||
| if err := h.UsageLimiter.Invalidate(ctx, key.ID); err != nil { | ||
| h.Logger.Error("Failed to invalidate usage limit", | ||
|
|
||
This file contains hidden or 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 file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.