Shaman: only add random suffix to checkout dir if already existing

Only add a random suffix to the checkout dir if it is necessary to ensure
uniqueness. If the client-supplied checkout directory doesn't exist yet,
it will be used as-is.
This commit is contained in:
Sybren A. Stüvel 2022-03-25 14:08:09 +01:00
parent 724938c7ae
commit 4b9501590e

View File

@ -110,11 +110,11 @@ func (m *Manager) PrepareCheckout(checkoutPath string) (ResolvedCheckoutInfo, er
defer m.checkoutUniquenessMutex.Unlock() defer m.checkoutUniquenessMutex.Unlock()
var lastErr error var lastErr error
// Just try 10 different random tokens. If that still doesn't work, fail. attemptCheckoutPath := checkoutPath
for try := 0; try < 10; try++ {
randomisedPath := fmt.Sprintf("%s-%s", checkoutPath, randomisedToken())
checkoutPaths, err := m.pathForCheckout(randomisedPath) // Just try 10 different random suffixes. If that still doesn't work, fail.
for try := 0; try < 10; try++ {
checkoutPaths, err := m.pathForCheckout(attemptCheckoutPath)
if err != nil { if err != nil {
return ResolvedCheckoutInfo{}, err return ResolvedCheckoutInfo{}, err
} }
@ -127,13 +127,15 @@ func (m *Manager) PrepareCheckout(checkoutPath string) (ResolvedCheckoutInfo, er
if stat, err := os.Stat(checkoutPaths.absolutePath); !os.IsNotExist(err) { if stat, err := os.Stat(checkoutPaths.absolutePath); !os.IsNotExist(err) {
if err == nil { if err == nil {
// No error stat'ing this path, indicating it's an existing checkout. // No error stat'ing this path, indicating it's an existing checkout.
// Just retry another random token.
lastErr = ErrCheckoutAlreadyExists lastErr = ErrCheckoutAlreadyExists
if stat.IsDir() { if stat.IsDir() {
logger.Debug().Msg("shaman: checkout path exists") logger.Debug().Msg("shaman: checkout path exists")
} else { } else {
logger.Warn().Msg("shaman: checkout path exists but is not a directory") logger.Warn().Msg("shaman: checkout path exists but is not a directory")
} }
// Retry with (another) random suffix.
attemptCheckoutPath = fmt.Sprintf("%s-%s", checkoutPath, randomisedToken())
continue continue
} }
// If it's any other error, it's really a problem on our side. Don't retry. // If it's any other error, it's really a problem on our side. Don't retry.