Skip to content

Commit 1e5f5fa

Browse files
committed
store: GenerateName: optimize check if name is taken
The function was using NodeGroupByName, which validated the name to be in the correct format, but also if the information on disk was correct, obtained the last activity date, etc. Given that names generated should always match the correct format, and we're only looking for _possible_ conflicts, we can simplify the validation. The results of the generated name will still be validated when used, as the Create function calls `NodeGroupByName` as part of creating; https://github.com/docker/buildx/blob/2d65b12a65ab5529c6eba55605a790580dad5f4b/builder/builder.go#L358-L387 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent b642e05 commit 1e5f5fa

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

store/store.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
9898
return &ng, nil
9999
}
100100

101+
// nameExists checks if a nodeGroup with the given name was found on disk.
102+
// It does not validate the name, neither the information on disk to be
103+
// correct.
104+
func (t *Txn) nameExists(name string) bool {
105+
_, err := os.Stat(filepath.Join(t.s.cfg.Dir(), instanceDir, name))
106+
return err == nil
107+
}
108+
101109
func (t *Txn) Save(ng *NodeGroup) error {
102110
name, err := ValidateName(ng.Name)
103111
if err != nil {

store/util.go

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

33
import (
44
"math/rand/v2"
5-
"os"
65
"regexp"
76
"strings"
87

@@ -44,14 +43,9 @@ func GenerateName(txn *Txn) (string, error) {
4443
nouns[rand.IntN(len(nouns))] + "_" +
4544
themes[rand.IntN(len(themes))] // #nosec G404 -- ignore "Use of weak random number generator"
4645

47-
if _, err := txn.NodeGroupByName(name); err != nil {
48-
if !os.IsNotExist(errors.Cause(err)) {
49-
return "", err
50-
}
51-
} else {
52-
continue
46+
if !txn.nameExists(name) {
47+
return name, nil
5348
}
54-
return name, nil
5549
}
5650
return "", errors.Errorf("failed to generate random name")
5751
}

0 commit comments

Comments
 (0)