diff options
author | Michael Muré <batolettre@gmail.com> | 2022-12-27 19:39:09 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-12-27 19:39:09 +0100 |
commit | b27958758fe59f3c984243f4ad3cb8bcb4f43e86 (patch) | |
tree | d04c5e9b3baf91a14c32aa742938ada718f07914 /cache/repo_cache.go | |
parent | 8cea6c7515e42cb8821a03c9bfebb0a8f63a01be (diff) | |
download | git-bug-b27958758fe59f3c984243f4ad3cb8bcb4f43e86.tar.gz |
cache: simplify cache building events handling
Diffstat (limited to 'cache/repo_cache.go')
-rw-r--r-- | cache/repo_cache.go | 47 |
1 files changed, 24 insertions, 23 deletions
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 |