Skip to content

Commit 44a1680

Browse files
authored
refactor(internal/librarian): parallelize rust source fetching (#3528)
The Rust source fetching logic is extracted into a dedicated fetchRustSources function that fetches all source repositories concurrently.
1 parent 43c8afb commit 44a1680

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/yuin/goldmark v1.7.13
2424
golang.org/x/exp v0.0.0-20250911091902-df9299821621
2525
golang.org/x/mod v0.30.0
26+
golang.org/x/sync v0.19.0
2627
google.golang.org/genproto v0.0.0-20251103181224-f26f9409b101
2728
google.golang.org/genproto/googleapis/api v0.0.0-20251029180050-ab9386a59fda
2829
google.golang.org/genproto/googleapis/rpc v0.0.0-20251029180050-ab9386a59fda
@@ -265,7 +266,6 @@ require (
265266
golang.org/x/exp/typeparams v0.0.0-20251023183803-a4bb9ffd2546 // indirect
266267
golang.org/x/net v0.47.0 // indirect
267268
golang.org/x/oauth2 v0.31.0 // indirect
268-
golang.org/x/sync v0.18.0 // indirect
269269
golang.org/x/sys v0.38.0 // indirect
270270
golang.org/x/telemetry v0.0.0-20251111182119-bc8e575c7b54 // indirect
271271
golang.org/x/text v0.31.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
880880
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
881881
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
882882
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
883-
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
884-
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
883+
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
884+
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
885885
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
886886
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
887887
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

internal/librarian/generate.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/googleapis/librarian/internal/librarian/rust"
2929
"github.com/googleapis/librarian/internal/yaml"
3030
"github.com/urfave/cli/v3"
31+
"golang.org/x/sync/errgroup"
3132
)
3233

3334
const (
@@ -191,28 +192,11 @@ func generate(ctx context.Context, language string, library *config.Library, cfg
191192
return nil, err
192193
}
193194
case languageRust:
194-
sources := &rust.Sources{
195-
Googleapis: googleapisDir,
196-
}
197-
sources.Discovery, err = fetchSource(ctx, cfgSources.Discovery, discoveryRepo)
198-
if err != nil {
199-
return nil, err
200-
}
201-
sources.Conformance, err = fetchSource(ctx, cfgSources.Conformance, protobufRepo)
202-
if err != nil {
203-
return nil, err
204-
}
205-
sources.Showcase, err = fetchSource(ctx, cfgSources.Showcase, showcaseRepo)
195+
sources, err := fetchRustSources(ctx, cfgSources)
206196
if err != nil {
207197
return nil, err
208198
}
209-
if cfgSources.ProtobufSrc != nil {
210-
dir, err := fetchSource(ctx, cfgSources.ProtobufSrc, protobufRepo)
211-
if err != nil {
212-
return nil, err
213-
}
214-
sources.ProtobufSrc = filepath.Join(dir, cfgSources.ProtobufSrc.Subpath)
215-
}
199+
sources.Googleapis = googleapisDir
216200
keep, err := rust.Keep(library)
217201
if err != nil {
218202
return nil, fmt.Errorf("library %s: %w", library.Name, err)
@@ -230,6 +214,53 @@ func generate(ctx context.Context, language string, library *config.Library, cfg
230214
return library, nil
231215
}
232216

217+
// fetchRustSources fetches all source repositories needed for Rust generation
218+
// in parallel. It returns a rust.Sources struct with all directories populated.
219+
func fetchRustSources(ctx context.Context, cfgSources *config.Sources) (*rust.Sources, error) {
220+
sources := &rust.Sources{}
221+
222+
g, ctx := errgroup.WithContext(ctx)
223+
g.Go(func() error {
224+
dir, err := fetchSource(ctx, cfgSources.Discovery, discoveryRepo)
225+
if err != nil {
226+
return err
227+
}
228+
sources.Discovery = dir
229+
return nil
230+
})
231+
g.Go(func() error {
232+
dir, err := fetchSource(ctx, cfgSources.Conformance, protobufRepo)
233+
if err != nil {
234+
return err
235+
}
236+
sources.Conformance = dir
237+
return nil
238+
})
239+
g.Go(func() error {
240+
dir, err := fetchSource(ctx, cfgSources.Showcase, showcaseRepo)
241+
if err != nil {
242+
return err
243+
}
244+
sources.Showcase = dir
245+
return nil
246+
})
247+
248+
if cfgSources.ProtobufSrc != nil {
249+
g.Go(func() error {
250+
dir, err := fetchSource(ctx, cfgSources.ProtobufSrc, protobufRepo)
251+
if err != nil {
252+
return err
253+
}
254+
sources.ProtobufSrc = filepath.Join(dir, cfgSources.ProtobufSrc.Subpath)
255+
return nil
256+
})
257+
}
258+
if err := g.Wait(); err != nil {
259+
return nil, err
260+
}
261+
return sources, nil
262+
}
263+
233264
func formatLibrary(ctx context.Context, language string, library *config.Library) error {
234265
switch language {
235266
case languageFake:

0 commit comments

Comments
 (0)