-
Notifications
You must be signed in to change notification settings - Fork 69
Updated to use goroutines for mulitple async depedency resolution #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "io" | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/KyleBanks/depth" | ||
| ) | ||
|
|
@@ -20,45 +21,44 @@ const ( | |
| var outputJSON bool | ||
|
|
||
| func main() { | ||
| t := parse(os.Args[1:]) | ||
| if err := handlePkgs(t, flag.Args(), outputJSON); err != nil { | ||
| os.Exit(1) | ||
| ResolveInternal := flag.Bool("internal", false, "If set, resolves dependencies of internal (stdlib) packages.") | ||
| ResolveTest := flag.Bool("test", false, "If set, resolves dependencies used for testing.") | ||
| MaxDepth := flag.Int("max", 0, "Sets the maximum depth of dependencies to resolve.") | ||
| outputJSON := flag.Bool("json", false, "If set, outputs the depencies in JSON format.") | ||
| flag.Parse() | ||
|
|
||
| pkgs := flag.Args() | ||
| t := &depth.Tree{ | ||
| ResolveInternal: *ResolveInternal, | ||
| ResolveTest: *ResolveTest, | ||
| MaxDepth: *MaxDepth, | ||
| } | ||
| } | ||
|
|
||
| // parse constructs a depth.Tree from command-line arguments. | ||
| func parse(args []string) *depth.Tree { | ||
| f := flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
|
|
||
| var t depth.Tree | ||
| f.BoolVar(&t.ResolveInternal, "internal", false, "If set, resolves dependencies of internal (stdlib) packages.") | ||
| f.BoolVar(&t.ResolveTest, "test", false, "If set, resolves dependencies used for testing.") | ||
| f.IntVar(&t.MaxDepth, "max", 0, "Sets the maximum depth of dependencies to resolve.") | ||
| f.BoolVar(&outputJSON, "json", false, "If set, outputs the depencies in JSON format.") | ||
| f.Parse(args) | ||
| handlePkgs(t, pkgs, *outputJSON) | ||
|
|
||
| return &t | ||
| } | ||
|
|
||
| // handlePkgs takes a slice of package names, resolves a Tree on them, | ||
| // and outputs each Tree to Stdout. | ||
| func handlePkgs(t *depth.Tree, pkgs []string, outputJSON bool) error { | ||
| func handlePkgs(t *depth.Tree, pkgs []string, outputJSON bool) { | ||
| var wg sync.WaitGroup | ||
| for _, pkg := range pkgs { | ||
| err := t.Resolve(pkg) | ||
| if err != nil { | ||
| fmt.Printf("'%v': FATAL: %v\n", pkg, err) | ||
| return err | ||
| } | ||
|
|
||
| if outputJSON { | ||
| writePkgJSON(os.Stdout, *t.Root) | ||
| continue | ||
| } | ||
| wg.Add(1) | ||
| go handlePkg(&wg, t, pkg, outputJSON) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure reusing the same Tree concurrently is going to be safe, because resolving modified properties on the tree itself. If you put the parse method back, you could call parse within this for-loop to get a fresh tree for each package |
||
| } | ||
| wg.Wait() | ||
| } | ||
|
|
||
| func handlePkg(wg *sync.WaitGroup, t *depth.Tree, pkg string, outputJSON bool) { | ||
| defer wg.Done() | ||
| err := t.Resolve(pkg) | ||
| if err != nil { | ||
| fmt.Printf("'%v': FATAL: %v\n", pkg, err) | ||
| return | ||
| } | ||
| if outputJSON { | ||
| writePkgJSON(os.Stdout, *t.Root) | ||
| } else { | ||
| writePkg(os.Stdout, *t.Root, 0, false) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One last concern is printing, since this is going to happen concurrently the output may not always be cleanly separated by package, if that make sense. For example you may get halfway through printing PackageA's tree when PackageB starts printing
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I was using channels for solving this problem, where we send the tree struct with config and return the final filled tree to channel and then print it. In this approach problem was the pkg.go returns from multiple places, the channel needs to set for every return case to avoid a leak. |
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // writePkgJSON writes the full Pkg as JSON to the provided Writer. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you're using the WaitGroup and not channels I think you can keep the parse method + test