Skip to content

Commit

Permalink
Ent: IngestSources optimized with concurrently (guacsec#1595)
Browse files Browse the repository at this point in the history
Signed-off-by: mrizzi <mrizzi@redhat.com>
  • Loading branch information
mrizzi authored Dec 20, 2023
1 parent 68210cf commit a599888
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/assembler/backends/ent/backend/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/guacsec/guac/pkg/assembler/graphql/model"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/gqlerror"
"golang.org/x/sync/errgroup"
)

func (b *EntBackend) HasSourceAt(ctx context.Context, filter *model.HasSourceAtSpec) ([]*model.HasSourceAt, error) {
Expand Down Expand Up @@ -162,12 +163,20 @@ func (b *EntBackend) Sources(ctx context.Context, filter *model.SourceSpec) ([]*

func (b *EntBackend) IngestSources(ctx context.Context, sources []*model.SourceInputSpec) ([]*model.SourceIDs, error) {
ids := make([]*model.SourceIDs, len(sources))
for i, src := range sources {
s, err := b.IngestSource(ctx, *src)
if err != nil {
return nil, err
}
ids[i] = s
eg, ctx := errgroup.WithContext(ctx)
for i := range sources {
index := i
src := sources[index]
concurrently(eg, func() error {
s, err := b.IngestSource(ctx, *src)
if err == nil {
ids[index] = s
}
return err
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return ids, nil
}
Expand Down Expand Up @@ -248,7 +257,7 @@ func upsertSource(ctx context.Context, client *ent.Tx, src model.SourceInputSpec
ID(ctx)
if err != nil {
if err != stdsql.ErrNoRows {
return nil, errors.Wrap(err, "upsert package version")
return nil, errors.Wrap(err, "upsert source name")
}

sourceNameID, err = client.SourceName.Query().
Expand Down

0 comments on commit a599888

Please sign in to comment.