fix regression that caused more queries then necessary to be made to gitlab api

This commit is contained in:
Massaki Archambault 2024-06-06 21:46:29 -04:00
parent 8c53ccea6a
commit 8293825c2b
2 changed files with 45 additions and 11 deletions

View File

@ -15,8 +15,8 @@ type Group struct {
mux sync.Mutex
// group content cache
groupCache map[string]fstree.GroupSource
projectCache map[string]fstree.RepositorySource
childGroups map[string]fstree.GroupSource
childRepository map[string]fstree.RepositorySource
}
func (g *Group) GetGroupID() uint64 {
@ -27,8 +27,8 @@ func (g *Group) InvalidateCache() {
g.mux.Lock()
defer g.mux.Unlock()
g.groupCache = nil
g.projectCache = nil
g.childGroups = nil
g.childRepository = nil
}
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
@ -36,7 +36,10 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
// TODO: cache invalidation?
group, found := c.groupCache[gid]
if found {
c.logger.Debug("Group cache hit", "gid", gid)
return group, nil
} else {
c.logger.Debug("Group cache miss; fetching group", "gid", gid)
}
// If not in cache, fetch group infos from API
@ -44,12 +47,40 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
if err != nil {
return nil, fmt.Errorf("failed to fetch group with id %v: %v", gid, err)
}
c.logger.Debug("Fetched group", "gid", gid)
newGroup := Group{
ID: gitlabGroup.ID,
Name: gitlabGroup.Path,
groupCache: nil,
projectCache: nil,
childGroups: nil,
childRepository: nil,
}
// save in cache
c.groupCache[gid] = &newGroup
return &newGroup, nil
}
func (c *gitlabClient) newGroupFromGitlabGroup(gitlabGroup *gitlab.Group) (*Group, error) {
gid := gitlabGroup.ID
// start by searching the cache
group, found := c.groupCache[gid]
if found {
c.logger.Debug("Group cache hit", "gid", gid)
return group, nil
} else {
c.logger.Debug("Group cache miss; registering group", "gid", gid)
}
// if not in cache, convert and save to cache now
newGroup := Group{
ID: gitlabGroup.ID,
Name: gitlabGroup.Path,
childGroups: nil,
childRepository: nil,
}
// save in cache
@ -64,7 +95,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
// Get cached data if available
// TODO: cache cache invalidation?
if group.groupCache == nil || group.projectCache == nil {
if group.childGroups == nil || group.childRepository == nil {
groupCache := make(map[string]fstree.GroupSource)
projectCache := make(map[string]fstree.RepositorySource)
@ -82,7 +113,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
return nil, nil, fmt.Errorf("failed to fetch groups in gitlab: %v", err)
}
for _, gitlabGroup := range gitlabGroups {
group, _ := c.fetchGroup(gitlabGroup.ID)
group, _ := c.newGroupFromGitlabGroup(gitlabGroup)
groupCache[group.Name] = group
}
if response.CurrentPage >= response.TotalPages {
@ -114,8 +145,8 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
listProjectOpt.Page = response.NextPage
}
group.groupCache = groupCache
group.projectCache = projectCache
group.childGroups = groupCache
group.childRepository = projectCache
}
return group.groupCache, group.projectCache, nil
return group.childGroups, group.childRepository, nil
}

View File

@ -34,7 +34,10 @@ func (c *gitlabClient) fetchUser(uid int) (*User, error) {
// TODO: cache invalidation?
user, found := c.userCache[uid]
if found {
c.logger.Debug("User cache hit", "uid", uid)
return user, nil
} else {
c.logger.Debug("User cache miss", "uid", uid)
}
// If not in cache, fetch group infos from API