opentofu/internal/getproviders/memoize_source.go
Martin Atkins 0b734a2803 command: Make provider installation interruptible
In earlier commits we started to make the installation codepath
context-aware so that it could be canceled in the event of a SIGINT, but
we didn't complete wiring that through the API of the getproviders
package.

Here we make the getproviders.Source interface methods, along with some
other functions that can make network requests, take a context.Context
argument and act appropriately if that context is cancelled.

The main providercache.Installer.EnsureProviderVersions method now also
has some context-awareness so that it can abort its work early if its
context reports any sort of error. That avoids waiting for the process
to wind through all of the remaining iterations of the various loops,
logging each request failure separately, and instead returns just
a single aggregate "canceled" error.

We can then set things up in the "terraform init" and
"terraform providers mirror" commands so that the context will be
cancelled if we get an interrupt signal, allowing provider installation
to abort early while still atomically completing any local-side effects
that may have started.
2020-09-29 10:00:35 -07:00

104 lines
3.1 KiB
Go

package getproviders
import (
"context"
"sync"
"github.com/hashicorp/terraform/addrs"
)
// MemoizeSource is a Source that wraps another Source and remembers its
// results so that they can be returned more quickly on future calls to the
// same object.
//
// Each MemoizeSource maintains a cache of response it has seen as part of its
// body. All responses are retained for the remaining lifetime of the object.
// Errors from the underlying source are also cached, and so subsequent calls
// with the same arguments will always produce the same errors.
//
// A MemoizeSource can be called concurrently, with incoming requests processed
// sequentially.
type MemoizeSource struct {
underlying Source
availableVersions map[addrs.Provider]memoizeAvailableVersionsRet
packageMetas map[memoizePackageMetaCall]memoizePackageMetaRet
mu sync.Mutex
}
type memoizeAvailableVersionsRet struct {
VersionList VersionList
Warnings Warnings
Err error
}
type memoizePackageMetaCall struct {
Provider addrs.Provider
Version Version
Target Platform
}
type memoizePackageMetaRet struct {
PackageMeta PackageMeta
Err error
}
var _ Source = (*MemoizeSource)(nil)
// NewMemoizeSource constructs and returns a new MemoizeSource that wraps
// the given underlying source and memoizes its results.
func NewMemoizeSource(underlying Source) *MemoizeSource {
return &MemoizeSource{
underlying: underlying,
availableVersions: make(map[addrs.Provider]memoizeAvailableVersionsRet),
packageMetas: make(map[memoizePackageMetaCall]memoizePackageMetaRet),
}
}
// AvailableVersions requests the available versions from the underlying source
// and caches them before returning them, or on subsequent calls returns the
// result directly from the cache.
func (s *MemoizeSource) AvailableVersions(ctx context.Context, provider addrs.Provider) (VersionList, Warnings, error) {
s.mu.Lock()
defer s.mu.Unlock()
if existing, exists := s.availableVersions[provider]; exists {
return existing.VersionList, nil, existing.Err
}
ret, warnings, err := s.underlying.AvailableVersions(ctx, provider)
s.availableVersions[provider] = memoizeAvailableVersionsRet{
VersionList: ret,
Err: err,
Warnings: warnings,
}
return ret, warnings, err
}
// PackageMeta requests package metadata from the underlying source and caches
// the result before returning it, or on subsequent calls returns the result
// directly from the cache.
func (s *MemoizeSource) PackageMeta(ctx context.Context, provider addrs.Provider, version Version, target Platform) (PackageMeta, error) {
s.mu.Lock()
defer s.mu.Unlock()
key := memoizePackageMetaCall{
Provider: provider,
Version: version,
Target: target,
}
if existing, exists := s.packageMetas[key]; exists {
return existing.PackageMeta, existing.Err
}
ret, err := s.underlying.PackageMeta(ctx, provider, version, target)
s.packageMetas[key] = memoizePackageMetaRet{
PackageMeta: ret,
Err: err,
}
return ret, err
}
func (s *MemoizeSource) ForDisplay(provider addrs.Provider) string {
return s.underlying.ForDisplay(provider)
}