-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split caching logic into it's own type to use with LoaderClient (#17118)
Currently all the caching logic for schemas is in `pluginLoader` itself, that means other loader implementations (like the grpc `LoaderClient`) don't do any caching. This splits the cache logic out of `pluginLoader` into a new `cachedLoader`. `NewPluginLoader` internally wraps with that new type (unless `cacheOptions.disableFileCache` is set). But other loaders can now call `NewCachedLoader` to get a caching layer added. We'll want to go through the grpc codegen and converter implementations to make use of this. --------- Co-authored-by: Justin Van Patten <jvp@justinvp.com>
- Loading branch information
Showing
4 changed files
with
191 additions
and
52 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelog/pending/20240830--pkg--added-newcachedloader-for-caching-schema-loads.yaml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
changes: | ||
- type: feat | ||
scope: pkg | ||
description: Added `NewCachedLoader` for caching schema loads. |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package schema | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/blang/semver" | ||
) | ||
|
||
func NewCachedLoader(loader ReferenceLoader) ReferenceLoader { | ||
return &cachedLoader{ | ||
loader: loader, | ||
entries: make(map[string]PackageReference), | ||
} | ||
} | ||
|
||
type cachedLoader struct { | ||
loader ReferenceLoader | ||
|
||
m sync.RWMutex | ||
entries map[string]PackageReference | ||
} | ||
|
||
func (l *cachedLoader) LoadPackage(pkg string, version *semver.Version) (*Package, error) { | ||
ref, err := l.LoadPackageReference(pkg, version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ref.Definition() | ||
} | ||
|
||
func (l *cachedLoader) LoadPackageV2(ctx context.Context, descriptor *PackageDescriptor) (*Package, error) { | ||
ref, err := l.LoadPackageReferenceV2(ctx, descriptor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ref.Definition() | ||
} | ||
|
||
func (l *cachedLoader) LoadPackageReference(pkg string, version *semver.Version) (PackageReference, error) { | ||
return l.LoadPackageReferenceV2(context.Background(), &PackageDescriptor{ | ||
Name: pkg, | ||
Version: version, | ||
}) | ||
} | ||
|
||
func (l *cachedLoader) LoadPackageReferenceV2( | ||
ctx context.Context, descriptor *PackageDescriptor, | ||
) (PackageReference, error) { | ||
l.m.Lock() | ||
defer l.m.Unlock() | ||
|
||
var key string | ||
if descriptor.Parameterization == nil { | ||
key = packageIdentity(descriptor.Name, descriptor.Version) | ||
} else { | ||
key = packageIdentity(descriptor.Parameterization.Name, &descriptor.Parameterization.Version) | ||
} | ||
if p, ok := l.entries[key]; ok { | ||
return p, nil | ||
} | ||
|
||
p, err := l.loader.LoadPackageReferenceV2(ctx, descriptor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l.entries[key] = p | ||
return p, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package schema | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/blang/semver" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockLoader struct { | ||
GetPackageF func(context.Context, *PackageDescriptor) (PackageReference, error) | ||
} | ||
|
||
func (m *mockLoader) LoadPackage(pkg string, version *semver.Version) (*Package, error) { | ||
ref, err := m.LoadPackageReference(pkg, version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ref.Definition() | ||
} | ||
|
||
func (m *mockLoader) LoadPackageV2(ctx context.Context, descriptor *PackageDescriptor) (*Package, error) { | ||
ref, err := m.LoadPackageReferenceV2(ctx, descriptor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ref.Definition() | ||
} | ||
|
||
func (m *mockLoader) LoadPackageReference(pkg string, version *semver.Version) (PackageReference, error) { | ||
return m.LoadPackageReferenceV2(context.TODO(), &PackageDescriptor{ | ||
Name: pkg, | ||
Version: version, | ||
}) | ||
} | ||
|
||
func (m *mockLoader) LoadPackageReferenceV2( | ||
ctx context.Context, descriptor *PackageDescriptor, | ||
) (PackageReference, error) { | ||
return m.GetPackageF(ctx, descriptor) | ||
} | ||
|
||
func TestCachedLoader(t *testing.T) { | ||
t.Parallel() | ||
|
||
calls := 0 | ||
mockLoader := &mockLoader{ | ||
GetPackageF: func(context.Context, *PackageDescriptor) (PackageReference, error) { | ||
calls++ | ||
return nil, nil | ||
}, | ||
} | ||
|
||
loader := NewCachedLoader(mockLoader) | ||
|
||
_, err := loader.LoadPackageReference("pkg", nil) | ||
require.NoError(t, err) | ||
|
||
_, err = loader.LoadPackageReference("pkg", nil) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 1, calls) | ||
} |