Skip to content

Commit

Permalink
better channel naming
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Mar 18, 2021
1 parent 55777f5 commit bc46e51
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/models/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ var assetLoaders = sync.Map{}
// represents a goroutine loading assets for an org, stores the loaded assets (and possible error) and
// a channel to notify any listeners that the loading is complete
type assetLoader struct {
ch chan struct{}
done chan struct{}
assets *OrgAssets
err error
}

// loadOrgAssetsOnce is a thread safe method to create new org assets from the DB in a thread safe manner
// that ensures only one goroutine is fetching the org at once. (others will block on the first completing)
func loadOrgAssetsOnce(ctx context.Context, db *sqlx.DB, orgID OrgID) (*OrgAssets, error) {
loader := assetLoader{ch: make(chan struct{})}
loader := assetLoader{done: make(chan struct{})}
actual, inFlight := assetLoaders.LoadOrStore(orgID, &loader)
actualLoader := actual.(*assetLoader)
if inFlight {
<-actualLoader.ch
<-actualLoader.done
} else {
actualLoader.assets, actualLoader.err = NewOrgAssets(ctx, db, orgID, nil, RefreshAll)
close(actualLoader.ch)
close(actualLoader.done)
assetLoaders.Delete(orgID)
}
return actualLoader.assets, actualLoader.err
Expand Down

0 comments on commit bc46e51

Please sign in to comment.