- Notifications
You must be signed in to change notification settings - Fork921
chore: add cacheCloser to cleanup all opened files#18473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
34 changes: 18 additions & 16 deletionscoderd/dynamicparameters/render.go
This file contains hidden or 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 |
---|---|---|
@@ -131,46 +131,48 @@ func (r *loader) Renderer(ctx context.Context, db database.Store, cache *files.C | ||
return r.staticRender(ctx, db) | ||
} | ||
return r.dynamicRenderer(ctx, db,files.NewCacheCloser(cache)) | ||
} | ||
// Renderer caches all the necessary files when rendering a template version's | ||
// parameters. It must be closed after use to release the cached files. | ||
func (r *loader) dynamicRenderer(ctx context.Context, db database.Store, cache *files.CacheCloser) (*dynamicRenderer, error) { | ||
closeFiles := true // If the function returns with no error, this will toggle to false. | ||
defer func() { | ||
if closeFiles { | ||
cache.Close() | ||
} | ||
}() | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// If they can read the template version, then they can read the file for | ||
// parameter loading purposes. | ||
//nolint:gocritic | ||
fileCtx := dbauthz.AsFileReader(ctx) | ||
var templateFS fs.FS | ||
var err error | ||
templateFS, err = cache.Acquire(fileCtx, r.job.FileID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("acquire template file: %w", err) | ||
} | ||
var moduleFilesFS *files.CloseFS | ||
if r.terraformValues.CachedModuleFiles.Valid { | ||
moduleFilesFS, err = cache.Acquire(fileCtx, r.terraformValues.CachedModuleFiles.UUID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("acquire module files: %w", err) | ||
} | ||
templateFS = files.NewOverlayFS(templateFS, []files.Overlay{{Path: ".terraform/modules", FS: moduleFilesFS}}) | ||
} | ||
closeFiles = false // Caller will have to call close | ||
return &dynamicRenderer{ | ||
data: r, | ||
templateFS:templateFS, | ||
db: db, | ||
ownerErrors: make(map[uuid.UUID]error), | ||
close: cache.Close, | ||
}, nil | ||
} | ||
4 changes: 4 additions & 0 deletionscoderd/files/cache.go
This file contains hidden or 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
57 changes: 57 additions & 0 deletionscoderd/files/closer.go
This file contains hidden or 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,57 @@ | ||
package files | ||
import ( | ||
"context" | ||
"sync" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
) | ||
// CacheCloser is a cache wrapper used to close all acquired files. | ||
// This is a more simple interface to use if opening multiple files at once. | ||
type CacheCloser struct { | ||
cache FileAcquirer | ||
closers []func() | ||
mu sync.Mutex | ||
} | ||
func NewCacheCloser(cache FileAcquirer) *CacheCloser { | ||
return &CacheCloser{ | ||
cache: cache, | ||
closers: make([]func(), 0), | ||
} | ||
} | ||
func (c *CacheCloser) Close() { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
for _, doClose := range c.closers { | ||
doClose() | ||
} | ||
// Prevent further acquisitions | ||
c.cache = nil | ||
// Remove any references | ||
c.closers = nil | ||
} | ||
func (c *CacheCloser) Acquire(ctx context.Context, fileID uuid.UUID) (*CloseFS, error) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if c.cache == nil { | ||
return nil, xerrors.New("cache is closed, and cannot acquire new files") | ||
} | ||
f, err := c.cache.Acquire(ctx, fileID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.closers = append(c.closers, f.close) | ||
return f, nil | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.