Skip to content

Commit

Permalink
refactor: speed up ListProjects
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Dec 15, 2024
1 parent a9518fa commit 5846de2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion langserver/internal/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *client) GetDefaultProject() string {
}

func (c *client) ListProjects(ctx context.Context) ([]*cloudresourcemanager.Project, error) {
caller := c.cloudresourcemanagerService.Projects.List().Context(ctx)
caller := c.cloudresourcemanagerService.Projects.List().PageSize(1000).Context(ctx)

list, err := caller.Do()
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions langserver/internal/bigquery/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ func (c *cache) callListProjects(ctx context.Context) ([]*cloudresourcemanager.P
}

if len(result) > 0 {
err := c.db.InsertProjects(ctx, result)
if err != nil {
// TODO
fmt.Fprintf(os.Stderr, "failed to insert projects: %v\n", err)
insertSize := 1000
for i := 0; i < len(result); i += insertSize {
insertItems := result[i:min(len(result), i+insertSize)]
err := c.db.InsertProjects(ctx, insertItems)
if err != nil {
// TODO
fmt.Fprintf(os.Stderr, "failed to insert projects: %v\n", err)
}
}
}
return result, nil
Expand Down

0 comments on commit 5846de2

Please sign in to comment.