fix regression that caused more queries then necessary to be made to gitlab api
This commit is contained in:
parent
8e350a7dac
commit
4667c12e47
|
@ -15,8 +15,8 @@ type Group struct {
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
|
|
||||||
// group content cache
|
// group content cache
|
||||||
groupCache map[string]fstree.GroupSource
|
childGroups map[string]fstree.GroupSource
|
||||||
projectCache map[string]fstree.RepositorySource
|
childRepository map[string]fstree.RepositorySource
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) GetGroupID() uint64 {
|
func (g *Group) GetGroupID() uint64 {
|
||||||
|
@ -27,8 +27,8 @@ func (g *Group) InvalidateCache() {
|
||||||
g.mux.Lock()
|
g.mux.Lock()
|
||||||
defer g.mux.Unlock()
|
defer g.mux.Unlock()
|
||||||
|
|
||||||
g.groupCache = nil
|
g.childGroups = nil
|
||||||
g.projectCache = nil
|
g.childRepository = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
|
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
|
||||||
|
@ -36,7 +36,10 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
|
||||||
// TODO: cache invalidation?
|
// TODO: cache invalidation?
|
||||||
group, found := c.groupCache[gid]
|
group, found := c.groupCache[gid]
|
||||||
if found {
|
if found {
|
||||||
|
c.logger.Debug("Group cache hit", "gid", gid)
|
||||||
return group, nil
|
return group, nil
|
||||||
|
} else {
|
||||||
|
c.logger.Debug("Group cache miss; fetching group", "gid", gid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not in cache, fetch group infos from API
|
// If not in cache, fetch group infos from API
|
||||||
|
@ -44,12 +47,40 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to fetch group with id %v: %v", gid, err)
|
return nil, fmt.Errorf("failed to fetch group with id %v: %v", gid, err)
|
||||||
}
|
}
|
||||||
|
c.logger.Debug("Fetched group", "gid", gid)
|
||||||
newGroup := Group{
|
newGroup := Group{
|
||||||
ID: gitlabGroup.ID,
|
ID: gitlabGroup.ID,
|
||||||
Name: gitlabGroup.Path,
|
Name: gitlabGroup.Path,
|
||||||
|
|
||||||
groupCache: nil,
|
childGroups: nil,
|
||||||
projectCache: 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
|
// save in cache
|
||||||
|
@ -64,7 +95,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
|
||||||
|
|
||||||
// Get cached data if available
|
// Get cached data if available
|
||||||
// TODO: cache cache invalidation?
|
// TODO: cache cache invalidation?
|
||||||
if group.groupCache == nil || group.projectCache == nil {
|
if group.childGroups == nil || group.childRepository == nil {
|
||||||
groupCache := make(map[string]fstree.GroupSource)
|
groupCache := make(map[string]fstree.GroupSource)
|
||||||
projectCache := make(map[string]fstree.RepositorySource)
|
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)
|
return nil, nil, fmt.Errorf("failed to fetch groups in gitlab: %v", err)
|
||||||
}
|
}
|
||||||
for _, gitlabGroup := range gitlabGroups {
|
for _, gitlabGroup := range gitlabGroups {
|
||||||
group, _ := c.fetchGroup(gitlabGroup.ID)
|
group, _ := c.newGroupFromGitlabGroup(gitlabGroup)
|
||||||
groupCache[group.Name] = group
|
groupCache[group.Name] = group
|
||||||
}
|
}
|
||||||
if response.CurrentPage >= response.TotalPages {
|
if response.CurrentPage >= response.TotalPages {
|
||||||
|
@ -114,8 +145,8 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
|
||||||
listProjectOpt.Page = response.NextPage
|
listProjectOpt.Page = response.NextPage
|
||||||
}
|
}
|
||||||
|
|
||||||
group.groupCache = groupCache
|
group.childGroups = groupCache
|
||||||
group.projectCache = projectCache
|
group.childRepository = projectCache
|
||||||
}
|
}
|
||||||
return group.groupCache, group.projectCache, nil
|
return group.childGroups, group.childRepository, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,10 @@ func (c *gitlabClient) fetchUser(uid int) (*User, error) {
|
||||||
// TODO: cache invalidation?
|
// TODO: cache invalidation?
|
||||||
user, found := c.userCache[uid]
|
user, found := c.userCache[uid]
|
||||||
if found {
|
if found {
|
||||||
|
c.logger.Debug("User cache hit", "uid", uid)
|
||||||
return user, nil
|
return user, nil
|
||||||
|
} else {
|
||||||
|
c.logger.Debug("User cache miss", "uid", uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not in cache, fetch group infos from API
|
// If not in cache, fetch group infos from API
|
||||||
|
|
Loading…
Reference in New Issue