The usual convention to create new object is to prefix `New*` so this commit renames the `WorkerClientError`. Initially I thought it would be `NewWorkerClientError()` but looking at the package prefix it seems unneeded, i.e. `clienterrors.New()` already provides enough context it seems and it's the only error we construct. We could consider renaming it to `clienterror` (singular) too but that could be a followup. I would also like to make `clienterror.Error` implement the `error` interface but that should be a followup to make this (mechanical) rename trivial to review.
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package remotefile
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors"
|
|
)
|
|
|
|
type resolveResult struct {
|
|
url string
|
|
content []byte
|
|
err error
|
|
}
|
|
|
|
// TODO: could make this more generic
|
|
// since this is shared with the container
|
|
// resolver
|
|
type Resolver struct {
|
|
jobs int
|
|
queue chan resolveResult
|
|
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewResolver() *Resolver {
|
|
return &Resolver{
|
|
ctx: context.Background(),
|
|
queue: make(chan resolveResult, 2),
|
|
}
|
|
}
|
|
|
|
func (r *Resolver) Add(url string) {
|
|
client := NewClient()
|
|
r.jobs += 1
|
|
|
|
go func() {
|
|
content, err := client.Resolve(url)
|
|
r.queue <- resolveResult{url: url, content: content, err: err}
|
|
}()
|
|
}
|
|
|
|
func (r *Resolver) Finish() []Spec {
|
|
|
|
resultItems := make([]Spec, 0, r.jobs)
|
|
for r.jobs > 0 {
|
|
result := <-r.queue
|
|
r.jobs -= 1
|
|
|
|
var resultError *clienterrors.Error
|
|
if result.err != nil {
|
|
resultError = clienterrors.New(
|
|
clienterrors.ErrorRemoteFileResolution,
|
|
result.err.Error(),
|
|
result.url,
|
|
)
|
|
}
|
|
|
|
resultItems = append(resultItems, Spec{
|
|
URL: result.url,
|
|
Content: result.content,
|
|
ResolutionError: resultError,
|
|
})
|
|
}
|
|
|
|
return resultItems
|
|
}
|