Skip to content

Commit 594543f

Browse files
authored
Merge pull request #6342 from tonistiigi/sourcepolicy-fixes
sourcepolicy: concurrency and cache fixes
2 parents 474cae7 + a3df584 commit 594543f

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

sourcepolicy/engine.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sourcepolicy
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/moby/buildkit/solver/pb"
78
spb "github.com/moby/buildkit/sourcepolicy/pb"
@@ -25,31 +26,30 @@ var (
2526
// Rule matching is delegated to the `Matcher` interface.
2627
// Mutations are delegated to the `Mutater` interface.
2728
type Engine struct {
28-
pol []*spb.Policy
29-
sources map[string]*selectorCache
29+
pol []*spb.Policy
30+
sourcesMu sync.Mutex
31+
sources map[string]*selectorCache
3032
}
3133

3234
// NewEngine creates a new source policy engine.
3335
func NewEngine(pol []*spb.Policy) *Engine {
3436
return &Engine{
35-
pol: pol,
37+
pol: pol,
38+
sources: make(map[string]*selectorCache),
3639
}
3740
}
3841

3942
// TODO: The key here can't be used to cache attr constraint regexes.
4043
func (e *Engine) selectorCache(src *spb.Selector) *selectorCache {
41-
if e.sources == nil {
42-
e.sources = map[string]*selectorCache{}
43-
}
44-
4544
key := src.MatchType.String() + " " + src.Identifier
4645

46+
e.sourcesMu.Lock()
47+
defer e.sourcesMu.Unlock()
4748
if s, ok := e.sources[key]; ok {
4849
return s
4950
}
5051

5152
s := &selectorCache{Selector: src}
52-
5353
e.sources[key] = s
5454
return s
5555
}
@@ -130,7 +130,7 @@ func (e *Engine) evaluatePolicy(ctx context.Context, pol *spb.Policy, srcOp *pb.
130130
var deny bool
131131
for _, rule := range pol.Rules {
132132
selector := e.selectorCache(rule.Selector)
133-
matched, err := match(selector, ident, srcOp.Attrs)
133+
matched, err := match(selector, ident, rule.Selector.Constraints, srcOp.Attrs)
134134
if err != nil {
135135
return false, errors.Wrap(err, "error matching source policy")
136136
}

sourcepolicy/matcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/pkg/errors"
88
)
99

10-
func match(src *selectorCache, ref string, attrs map[string]string) (bool, error) {
11-
for _, c := range src.Constraints {
10+
func match(src *selectorCache, ref string, constraints []*spb.AttrConstraint, attrs map[string]string) (bool, error) {
11+
for _, c := range constraints {
1212
if c == nil {
1313
return false, errors.Errorf("invalid nil constraint for %v", src)
1414
}

sourcepolicy/matcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func TestMatch(t *testing.T) {
319319

320320
for _, tc := range cases {
321321
t.Run(tc.name, func(t *testing.T) {
322-
matches, err := match(&selectorCache{Selector: tc.src}, tc.ref, tc.attrs)
322+
matches, err := match(&selectorCache{Selector: tc.src}, tc.ref, tc.src.Constraints, tc.attrs)
323323
if !tc.xErr {
324324
require.NoError(t, err)
325325
} else {

0 commit comments

Comments
 (0)