Skip to content

Commit a96510d

Browse files
authored
Implements Fetch in MuxCache (#1326)
* Implements `Fetch` in `MuxCache` `MuxCache` doesn't implement the `Fetch` method and it returns an error. However `MuxCache` doesn't have any logic instead it delegates to the appropriate cache based on the request. This means that we can just use the same logic as `CreateWatch` to delegate to the appropriate cache's `Fetch` method. `Linear` cache doesnt implement `Fetch` either so this part of the call will fail but the rest of the caches will work as expected. Additionally it was a bit confusing to debug the issue since the error message resembles quite a bit a `gRPC Unimplemented` error. So I updated the error message in `Linear` cache to be more specific. Signed-off-by: sotiris <[email protected]> * make error lowercase Signed-off-by: sotiris <[email protected]> --------- Signed-off-by: sotiris <[email protected]>
1 parent 295d9fa commit a96510d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pkg/cache/v3/linear.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ func (cache *LinearCache) getVersion() string {
570570
}
571571

572572
func (cache *LinearCache) Fetch(context.Context, *Request) (Response, error) {
573-
return nil, errors.New("not implemented")
573+
return nil, errors.New("fetch is not implemented by LinearCache")
574574
}
575575

576576
// NumResources returns the number of resources currently in the cache.

pkg/cache/v3/mux.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cache
1616

1717
import (
1818
"context"
19-
"errors"
2019
"fmt"
2120
)
2221

@@ -54,6 +53,13 @@ func (mux *MuxCache) CreateDeltaWatch(request *DeltaRequest, sub Subscription, v
5453
return cache.CreateDeltaWatch(request, sub, value)
5554
}
5655

57-
func (mux *MuxCache) Fetch(context.Context, *Request) (Response, error) {
58-
return nil, errors.New("not implemented")
56+
func (mux *MuxCache) Fetch(ctx context.Context, request *Request) (Response, error) {
57+
key := mux.Classify(request)
58+
cache, exists := mux.Caches[key]
59+
if !exists {
60+
return nil, fmt.Errorf("no cache defined for key %s", key)
61+
}
62+
// Not all caches may implement Fetch; we delegate to the selected cache and if
63+
// it does not implement Fetch, it will return an error.
64+
return cache.Fetch(ctx, request)
5965
}

0 commit comments

Comments
 (0)