-
Notifications
You must be signed in to change notification settings - Fork 0
Fixed bug on cache work #8
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
Conversation
WalkthroughThe changes refactor the lease creation and caching logic in the application. The Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant HTTPServer
participant Application
participant LeaseCache
participant LeaseManager
Client->>HTTPServer: Request lease creation
HTTPServer->>Application: CreateLease(key, ttl)
Application->>LeaseCache: checkCreatedLeasePresenceInCache(key)
alt Lease ID found in cache
LeaseCache-->>Application: leaseID
Application-->>HTTPServer: StatusAccepted, leaseID
else Lease not found
Application->>LeaseManager: CreateLease(key, ttl)
LeaseManager-->>Application: leaseID
Application->>LeaseCache: addLeaseToCache(key, "created", leaseID, ttl)
Application-->>HTTPServer: StatusCreated, leaseID
end
HTTPServer->>Client: Respond with lease status and ID
Possibly related PRs
Poem
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
🧹 Nitpick comments (3)
internal/application/application.go (2)
37-50: Good optimization that prevents duplicate lease creation.Checking for an existing lease in the cache before attempting to create a new one is an excellent enhancement. This approach prevents unnecessary storage operations and ensures consistency.
However, there's a potential issue with metrics collection on line 42:
- metrics.LeaseOperations.WithLabelValues(metrics.LeaseOperationGet, "error").Inc()This explicit increment might lead to double counting since you already have a deferred metrics increment on line 35. The deferred one should be sufficient to capture all operation outcomes.
70-81: Effective cache check implementation with room for improvement.The method efficiently checks for existing leases with "created" status. However, the hardcoded string "created" should be replaced with a constant for better maintainability.
- if cachedLease.Status == "created" { + if cachedLease.Status == storage.StatusCreated {Also, the logging is helpful for debugging cache hits.
internal/delivery/http/server.go (1)
92-96: Successfully simplified lease creation flow.The HTTP handler now delegates all caching responsibility to the application layer, which is a good separation of concerns. This makes the code more maintainable and easier to understand.
Consider enhancing error handling to provide more context to API consumers:
if err != nil { - w.WriteHeader(http.StatusInternalServerError) + log.Errorf("Failed to create lease: %v", err) + http.Error(w, "Failed to create lease", http.StatusInternalServerError) return }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
internal/application/application.go(2 hunks)internal/delivery/http/server.go(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
internal/application/application.go (4)
internal/infrastructure/metrics/metrics.go (2)
LeaseOperations(13-19)LeaseOperationGet(9-9)internal/application/command/leasemanagement/leaseManagement.go (1)
CreateLease(17-47)internal/infrastructure/storage/storage.go (1)
StatusAccepted(8-8)internal/infrastructure/cache/cache.go (1)
LeaseCacheRecord(29-32)
🔇 Additional comments (4)
internal/application/application.go (3)
35-35: Great use of deferred metrics collection.Using
deferensures metrics are consistently recorded after function completion regardless of which return path is taken. This is a good practice.
52-55: Appropriate status handling for cached leases.Setting the status to
StatusAcceptedfor cached leases correctly indicates that the lease already exists, which is consistent with the behavior defined in theleasemanagement.CreateLeasefunction.
83-92: Well-designed cache update method with proper null check.The method properly checks if the cache is nil before attempting to set a value, which is a good defensive programming practice. Making this method private is appropriate since it's now only used internally.
internal/delivery/http/server.go (1)
86-90: Good error handling for lease TTL parsing.The code gracefully handles the case when the lease TTL header cannot be parsed by falling back to a default duration and logging a warning message, which is a good practice.
Summary by CodeRabbit
New Features
Refactor
Bug Fixes