Skip to content

Commit

Permalink
Massive timeout (#195)
Browse files Browse the repository at this point in the history
* --massive-timeout

* nits
  • Loading branch information
ddddddO authored Jul 2, 2023
1 parent 8187496 commit b09286f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ root_h/
root_i/
root_j/
Primate/
cmd/gtree/gtree
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,16 @@ USAGE:
gtree output [command options] [arguments...]

OPTIONS:
--file value, -f value specify the path to markdown file. (default: stdin)
--two-spaces, --ts set this option when the markdown indent is 2 spaces. (default: tab spaces)
--four-spaces, --fs set this option when the markdown indent is 4 spaces. (default: tab spaces)
--massive, -m set this option when there are very many blocks of markdown. (default: false)
--json, -j set this option when outputting JSON. (default: tree)
--yaml, -y set this option when outputting YAML. (default: tree)
--toml, -t set this option when outputting TOML. (default: tree)
--watch, -w follow changes in markdown file. (default: false)
--help, -h show help
--file value, -f value specify the path to markdown file. (default: stdin)
--two-spaces, --ts set this option when the markdown indent is 2 spaces. (default: tab spaces)
--four-spaces, --fs set this option when the markdown indent is 4 spaces. (default: tab spaces)
--massive, -m set this option when there are very many blocks of markdown. (default: false)
--json, -j set this option when outputting JSON. (default: tree)
--yaml, -y set this option when outputting YAML. (default: tree)
--toml, -t set this option when outputting TOML. (default: tree)
--watch, -w follow changes in markdown file. (default: false)
--massive-timeout value, --mt value Set this option if you want to set a timeout. (default: 5s)
--help, -h show help
```

```console
Expand Down
24 changes: 21 additions & 3 deletions cmd/gtree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"time"

"github.com/ddddddO/gtree"
"github.com/fatih/color"
Expand Down Expand Up @@ -69,6 +70,18 @@ func main() {
Aliases: []string{"w"},
Usage: "follow changes in markdown file.",
},
&cli.DurationFlag{
Name: "massive-timeout",
Aliases: []string{"mt"},
Usage: "Set this option if you want to set a timeout.",
Value: time.Duration(5 * time.Second),
Action: func(ctx *cli.Context, v time.Duration) error {
if v <= 0 {
return errors.New("the timeout value should be greater than 0.")
}
return nil
},
},
}

mkdirFlags := []cli.Flag{
Expand Down Expand Up @@ -192,11 +205,16 @@ func actionOutput(c *cli.Context) error {
if err != nil {
return exitErrOpts(err)
}
options := []gtree.Option{oi, oo}
var om gtree.Option
if c.Bool("massive") {
// TODO: ほぼいらないけど、タイムアウト値をフラグで取ってcontext.WithTimeout渡すようにするでもいいかも
options = append(options, gtree.WithMassive(context.Background()))
om = gtree.WithMassive(context.Background())
}
if c.Duration("massive-timeout") > 0 {
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("massive-timeout"))
defer cancel()
om = gtree.WithMassive(ctx)
}
options := []gtree.Option{oi, oo, om}

markdownPath := c.Path("file")
if isInputStdin(markdownPath) {
Expand Down
2 changes: 1 addition & 1 deletion tree_handler_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ children:
func TestOutput_detecting_goroutinelerk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(500*time.Millisecond))
defer cancel()
w := &strings.Builder{}
w := io.Discard
r := strings.NewReader(tu.TwentyThousandRoots)
if gotErr := gtree.Output(w, r, gtree.WithMassive(ctx)); gotErr != nil {
if gotErr != context.DeadlineExceeded {
Expand Down

0 comments on commit b09286f

Please sign in to comment.