From b27958758fe59f3c984243f4ad3cb8bcb4f43e86 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 27 Dec 2022 19:39:09 +0100 Subject: cache: simplify cache building events handling --- cache/repo_cache.go | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'cache/repo_cache.go') diff --git a/cache/repo_cache.go b/cache/repo_cache.go index 6e7396ed..edfe1baf 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -72,17 +72,17 @@ type RepoCache struct { userIdentityId entity.Id } -// NewRepoCache create or open an unnamed (aka default) cache on top of a raw repository. -// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered +// NewRepoCache create or open a cache on top of a raw repository. +// The caller is expected to read all returned events before the cache is considered // ready to use. -func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent, error) { +func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent) { return NewNamedRepoCache(r, defaultRepoName) } // NewNamedRepoCache create or open a named cache on top of a raw repository. -// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered +// The caller is expected to read all returned events before the cache is considered // ready to use. -func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent, error) { +func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) { c := &RepoCache{ repo: r, name: name, @@ -102,35 +102,36 @@ func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan } // small buffer so that below functions can emit an event without blocking - events := make(chan BuildEvent, 10) - defer close(events) + events := make(chan BuildEvent) - err := c.lock(events) - if err != nil { - return &RepoCache{}, events, err - } + go func() { + defer close(events) - err = c.load() - if err == nil { - return c, events, nil - } + err := c.lock(events) + if err != nil { + events <- BuildEvent{Err: err} + return + } + + err = c.load() + if err == nil { + return + } - // Cache is either missing, broken or outdated. Rebuilding. - c.buildCache(events) + // Cache is either missing, broken or outdated. Rebuilding. + c.buildCache(events) + }() - return c, events, nil + return c, events } func NewRepoCacheNoEvents(r repository.ClockedRepo) (*RepoCache, error) { - cache, events, err := NewRepoCache(r) - if err != nil { - return nil, err - } + cache, events := NewRepoCache(r) for event := range events { if event.Err != nil { for range events { } - return nil, err + return nil, event.Err } } return cache, nil -- cgit