From 052fce5bf2b1995fe45b88626e5342e61933d7cd Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 23 May 2024 13:22:50 +0200 Subject: refactor: use a single value for per-source import timeouts --- internal/config/config.go | 90 +++++++++++++++++++++----------------------- internal/config/structs.go | 25 ++++++------ internal/fetcher/channel.go | 2 +- internal/importer/main.go | 4 +- internal/importer/options.go | 4 +- internal/importer/package.go | 4 +- 6 files changed, 60 insertions(+), 69 deletions(-) (limited to 'internal') diff --git a/internal/config/config.go b/internal/config/config.go index 0b3b9ba..c2e026c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -118,33 +118,31 @@ var defaultConfig = Config{ UpdateAt: mustLocalTime("04:00:00"), Sources: map[string]*Source{ "nixos": { - Name: "NixOS", - Key: "nixos", - Enable: true, - Importer: Options, - Fetcher: Channel, - Channel: "nixpkgs", - URL: "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz", - ImportPath: "nixos/release.nix", - Attribute: "options", - OutputPath: "share/doc/nixos", - FetchTimeout: Duration{5 * time.Minute}, - ImportTimeout: Duration{15 * time.Minute}, - Repo: nixpkgs, + Name: "NixOS", + Key: "nixos", + Enable: true, + Importer: Options, + Fetcher: Channel, + Channel: "nixpkgs", + URL: "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz", + ImportPath: "nixos/release.nix", + Attribute: "options", + OutputPath: "share/doc/nixos", + Timeout: Duration{5 * time.Minute}, + Repo: nixpkgs, }, "darwin": { - Name: "Darwin", - Key: "darwin", - Enable: false, - Importer: Options, - Fetcher: Channel, - Channel: "darwin", - URL: "https://github.com/LnL7/nix-darwin/archive/master.tar.gz", - ImportPath: "release.nix", - Attribute: "options", - OutputPath: "share/doc/darwin", - FetchTimeout: Duration{5 * time.Minute}, - ImportTimeout: Duration{15 * time.Minute}, + Name: "Darwin", + Key: "darwin", + Enable: false, + Importer: Options, + Fetcher: Channel, + Channel: "darwin", + URL: "https://github.com/LnL7/nix-darwin/archive/master.tar.gz", + ImportPath: "release.nix", + Attribute: "options", + OutputPath: "share/doc/darwin", + Timeout: Duration{5 * time.Minute}, Repo: Repository{ Type: GitHub, Owner: "LnL7", @@ -152,18 +150,17 @@ var defaultConfig = Config{ }, }, "home-manager": { - Name: "Home Manager", - Key: "home-manager", - Enable: false, - Importer: Options, - Channel: "home-manager", - URL: "https://github.com/nix-community/home-manager/archive/master.tar.gz", - Fetcher: Channel, - ImportPath: "default.nix", - Attribute: "docs.json", - OutputPath: "share/doc/home-manager", - FetchTimeout: Duration{5 * time.Minute}, - ImportTimeout: Duration{15 * time.Minute}, + Name: "Home Manager", + Key: "home-manager", + Enable: false, + Importer: Options, + Channel: "home-manager", + URL: "https://github.com/nix-community/home-manager/archive/master.tar.gz", + Fetcher: Channel, + ImportPath: "default.nix", + Attribute: "docs.json", + OutputPath: "share/doc/home-manager", + Timeout: Duration{5 * time.Minute}, Repo: Repository{ Type: GitHub, Owner: "nix-community", @@ -171,16 +168,15 @@ var defaultConfig = Config{ }, }, "nixpkgs": { - Name: "Nix Packages", - Key: "nixpkgs", - Enable: true, - Importer: Packages, - Fetcher: ChannelNixpkgs, - Channel: "nixos-unstable", - OutputPath: "packages.json.br", - FetchTimeout: Duration{5 * time.Minute}, - ImportTimeout: Duration{15 * time.Minute}, - Repo: nixpkgs, + Name: "Nix Packages", + Key: "nixpkgs", + Enable: true, + Importer: Packages, + Fetcher: ChannelNixpkgs, + Channel: "nixos-unstable", + OutputPath: "packages.json.br", + Timeout: Duration{5 * time.Minute}, + Repo: nixpkgs, }, }, }, diff --git a/internal/config/structs.go b/internal/config/structs.go index 4137261..a434698 100644 --- a/internal/config/structs.go +++ b/internal/config/structs.go @@ -33,17 +33,16 @@ type Importer struct { } type Source struct { - Name string `comment:"Human-readable name of source for generating links"` - Key string `comment:"Machine-readable name of source. Must be URL- and path-safe."` - Enable bool `comment:"Controls whether to show in the web interface and to run fetch/import jobs."` - Fetcher Fetcher `comment:"How to fetch options.json. One of 'channel', 'channel-nixpkgs' or 'download'."` - Importer ImporterType `comment:"Kind of data available from source. Currently supports 'packages' and 'options'."` - Channel string `comment:"(Fetcher=channel) Local name for channel, (Fetcher=channel-nixpkgs) Remote name of channel."` - URL string `comment:"(Fetcher=channel) Remote URL for channel, (Fetcher=download) Path containing files named 'revision' and 'options.json'."` - Attribute string `comment:"(Fetcher=channel) Nix attribute name (i.e. nix-build -A) that builds an {options,packages}.json"` - ImportPath string `comment:"(Fetcher=channel) Sub-path of imported channel which contains the attribute above, e.g. release.nix"` - FetchTimeout Duration `comment:"Abort fetch if it takes longer than this."` - ImportTimeout Duration `comment:"Abort import if it takes longer than this."` - OutputPath string `comment:"(Fetcher=channel) Path under ./result symlink to folder containing {options,packages}.json."` - Repo Repository `comment:"Used to generate declaration/definition links"` + Name string `comment:"Human-readable name of source for generating links"` + Key string `comment:"Machine-readable name of source. Must be URL- and path-safe."` + Enable bool `comment:"Controls whether to show in the web interface and to run fetch/import jobs."` + Fetcher Fetcher `comment:"How to fetch options.json. One of 'channel', 'channel-nixpkgs' or 'download'."` + Importer ImporterType `comment:"Kind of data available from source. Currently supports 'packages' and 'options'."` + Channel string `comment:"(Fetcher=channel) Local name for channel, (Fetcher=channel-nixpkgs) Remote name of channel."` + URL string `comment:"(Fetcher=channel) Remote URL for channel, (Fetcher=download) Path containing files named 'revision' and 'options.json'."` + Attribute string `comment:"(Fetcher=channel) Nix attribute name (i.e. nix-build -A) that builds an {options,packages}.json"` + ImportPath string `comment:"(Fetcher=channel) Sub-path of imported channel which contains the attribute above, e.g. release.nix"` + Timeout Duration `comment:"Abort import if it takes longer than this."` + OutputPath string `comment:"(Fetcher=channel) Path under ./result symlink to folder containing {options,packages}.json."` + Repo Repository `comment:"Used to generate declaration/definition links"` } diff --git a/internal/fetcher/channel.go b/internal/fetcher/channel.go index 3756012..01c4ae8 100644 --- a/internal/fetcher/channel.go +++ b/internal/fetcher/channel.go @@ -44,7 +44,7 @@ func (i *ChannelFetcher) FetchIfNeeded( args := []string{ "--no-build-output", "--timeout", - strconv.Itoa(int(i.Source.FetchTimeout.Seconds() - 1)), + strconv.Itoa(int(i.Source.Timeout.Seconds() - 1)), fmt.Sprintf("<%s/%s>", i.Source.Channel, i.Source.ImportPath), "--attr", i.Source.Attribute, diff --git a/internal/importer/main.go b/internal/importer/main.go index 6be5b45..bd41af2 100644 --- a/internal/importer/main.go +++ b/internal/importer/main.go @@ -28,7 +28,7 @@ func createSourceImporter( "fetcher", source.Fetcher.String(), "timeout", - source.FetchTimeout.Duration, + source.Timeout.Duration, ) logger.Debug("starting fetcher") @@ -42,7 +42,7 @@ func createSourceImporter( sourceMeta.Updated = time.Time{} } previousUpdate := sourceMeta.Updated - ctx, cancel := context.WithTimeout(parent, source.FetchTimeout.Duration) + ctx, cancel := context.WithTimeout(parent, source.Timeout.Duration) defer cancel() files, err := fetcher.FetchIfNeeded(ctx, &sourceMeta) diff --git a/internal/importer/options.go b/internal/importer/options.go index 4089f62..c26836e 100644 --- a/internal/importer/options.go +++ b/internal/importer/options.go @@ -87,8 +87,7 @@ func NewOptionProcessor(infile io.ReadCloser, source *config.Source) (*OptionIng return &i, nil } -func (i *OptionIngester) Process(parent context.Context) (<-chan nix.Importable, <-chan error) { - ctx, cancel := context.WithTimeout(parent, i.source.ImportTimeout.Duration) +func (i *OptionIngester) Process(ctx context.Context) (<-chan nix.Importable, <-chan error) { results := make(chan nix.Importable) errs := make(chan error) @@ -96,7 +95,6 @@ func (i *OptionIngester) Process(parent context.Context) (<-chan nix.Importable, defer i.infile.Close() defer close(results) defer close(errs) - defer cancel() outer: for mv := range i.dec.Stream() { diff --git a/internal/importer/package.go b/internal/importer/package.go index 3fee8d7..4e193db 100644 --- a/internal/importer/package.go +++ b/internal/importer/package.go @@ -104,8 +104,7 @@ func convertToLicense(in map[string]any) *nix.License { return l } -func (i *PackageIngester) Process(parent context.Context) (<-chan nix.Importable, <-chan error) { - ctx, cancel := context.WithTimeout(parent, i.source.ImportTimeout.Duration) +func (i *PackageIngester) Process(ctx context.Context) (<-chan nix.Importable, <-chan error) { results := make(chan nix.Importable) errs := make(chan error) @@ -113,7 +112,6 @@ func (i *PackageIngester) Process(parent context.Context) (<-chan nix.Importable defer i.infile.Close() defer close(results) defer close(errs) - defer cancel() outer: for mv := range i.dec.Stream() { -- cgit 1.4.1