feat: add Dapr distributed cache provider - #2870
Conversation
Port the Dapr cache provider from Ratify v1 to v2, enabling the high-availability deployment mode where verify/mutate cache state is shared across replicas via a Dapr state store. - Add internal/cache/dapr implementing the generic cache.Cache[T] interface (JSON-encoding values), gated by the HighAvailability feature flag and namespacing keys via internal/context. - Wire the server cache creation to use the Dapr cache when high-availability is enabled, falling back to Ristretto otherwise. - Add github.com/dapr/go-sdk dependency and unit tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (58.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #2870 +/- ##
==========================================
- Coverage 76.53% 76.29% -0.24%
==========================================
Files 88 89 +1
Lines 4027 4075 +48
==========================================
+ Hits 3082 3109 +27
- Misses 799 817 +18
- Partials 146 149 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR ports the Dapr-backed distributed cache provider from Ratify v1 into the v2 codebase to support the experimental high-availability mode where verify/mutate cache state can be shared across replicas via a Dapr state store.
Changes:
- Adds a new generic Dapr cache provider (
internal/cache/dapr) implementingcache.Cache[T]using JSON encoding and Dapr state APIs (with optional TTL metadata). - Updates the HTTP server to construct caches via a
newCache[T]helper that selects Dapr when the high-availability feature flag is enabled, otherwise falling back to Ristretto. - Introduces the
github.com/dapr/go-sdkdependency.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/httpserver/server.go | Routes cache construction through a helper to switch between Ristretto and the new Dapr cache based on a feature flag. |
| internal/cache/dapr/dapr.go | Implements a Dapr state-store-backed cache with JSON serialization and TTL support. |
| internal/cache/dapr/dapr_test.go | Adds unit tests for Dapr cache Get/Set/Delete using a stubbed Dapr client. |
| go.mod | Adds github.com/dapr/go-sdk dependency (and related indirect deps). |
| go.sum | Adds checksums for the new dependency set. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| mutateCache, err := newCache[string](defaultCacheTTL) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to create mutate cache: %w", err) | ||
| } |
| var md map[string]string | ||
| if ttl > 0 { | ||
| md = map[string]string{"ttlInSeconds": strconv.Itoa(int(ttl.Seconds()))} | ||
| } |
- Call featureflag.InitFeatureFlagsFromEnv() at startup so RATIFY_EXPERIMENTAL_HIGH_AVAILABILITY actually enables the Dapr cache path at runtime (previously only initialized in tests). - Compute ttlInSeconds with integer duration math and round up, so any positive sub-second TTL maps to at least 1 second instead of an unintended ttlInSeconds=0. Add a sub-second TTL test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Addressed the AI review feedback in 6199d96:
|
Description
Ports the Dapr cache provider from Ratify v1 (
pkg/cache/dapr) to v2, enabling the high-availability deployment mode where verify/mutate cache state is shared across replicas via a Dapr state store.Changes
internal/cache/dapr: new provider implementing the genericcache.Cache[T]interface. Values are JSON-encoded so anyTcan be shared across replicas. Keys are namespaced viainternal/context.CreateCacheKey, and construction is gated by theEXPERIMENTAL_HIGH_AVAILABILITYfeature flag.Set/SetWithTTLare merged into a singleSet(ctx, key, value, ttl)to match the v2 Ristretto/in-memory providers; a non-positive TTL falls back to the configured default.Getreturnscache.ErrNotFoundwhen the state entry is empty, honoring the v2 cache contract.internal/httpserver: cache creation now goes through anewCache[T]helper that returns the Dapr cache when high-availability is enabled, otherwise Ristretto. Default behavior is unchanged.github.com/dapr/go-sdkdependency and unit tests mirroring the v1 coverage.Testing
go build ./...go test ./internal/cache/... ./internal/httpserver/...— all passgolangci-linton the new package — 0 issues