Skip to content
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

configure timeout with D2_TIMEOUT #1392

Merged
merged 11 commits into from
Jun 15, 2023
Merged
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#### Features 🚀

- Configure timeout value with D2_TIMEOUT env var. [#1392](https://github.com/terrastruct/d2/pull/1392)

#### Improvements 🧹

- Display version on CLI help invocation [#1400](https://github.com/terrastruct/d2/pull/1400)
Expand Down
3 changes: 3 additions & 0 deletions ci/release/template/man/d2.1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Print debug logs.
.It Fl -img-cache Ar true
In watch mode, images used in icons are cached for subsequent compilations. This should be disabled if images might change
.Ns .
.It Fl -timeout Ar 120
The maximum number of seconds that D2 runs for before timing out and exiting. When rendering a large diagram, it is recommended to increase this value.
gavin-ts marked this conversation as resolved.
Show resolved Hide resolved
.Ns .
.It Fl h , -help
Print usage information and exit.
.It Fl v , -version
Expand Down
11 changes: 10 additions & 1 deletion d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"oss.terrastruct.com/d2/lib/png"
"oss.terrastruct.com/d2/lib/pptx"
"oss.terrastruct.com/d2/lib/textmeasure"
timelib "oss.terrastruct.com/d2/lib/time"
"oss.terrastruct.com/d2/lib/version"
"oss.terrastruct.com/d2/lib/xgif"

Expand Down Expand Up @@ -88,6 +89,11 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
if err != nil {
return err
}
timeoutFlag, err := ms.Opts.Int64("D2_TIMEOUT", "timeout", "", 120, "the maximum number of seconds that D2 runs for before timing out and exiting. When rendering a large diagram, it is recommended to increase this value")
if err != nil {
return err
}

versionFlag, err := ms.Opts.Bool("", "version", "v", false, "get the version")
if err != nil {
return err
Expand Down Expand Up @@ -160,6 +166,9 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
if *browserFlag != "" {
ms.Env.Setenv("BROWSER", *browserFlag)
}
if timeoutFlag != nil {
os.Setenv("D2_TIMEOUT", fmt.Sprintf("%d", *timeoutFlag))
}

var inputPath string
var outputPath string
Expand Down Expand Up @@ -298,7 +307,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return w.run()
}

ctx, cancel := context.WithTimeout(ctx, time.Minute*2)
ctx, cancel := timelib.WithTimeout(ctx, time.Minute*2)
defer cancel()

_, written, err := compile(ctx, ms, plugin, renderOpts, fontFamily, *animateIntervalFlag, inputPath, outputPath, *bundleFlag, *forceAppendixFlag, pw.Page)
Expand Down
3 changes: 2 additions & 1 deletion d2plugin/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"oss.terrastruct.com/util-go/xmain"

"oss.terrastruct.com/d2/d2graph"
timelib "oss.terrastruct.com/d2/lib/time"
)

// execPlugin uses the binary at pathname with the plugin protocol to implement
Expand Down Expand Up @@ -147,7 +148,7 @@ func (p *execPlugin) Info(ctx context.Context) (_ *PluginInfo, err error) {
}

func (p *execPlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
ctx, cancel := timelib.WithTimeout(ctx, time.Minute*2)
defer cancel()

graphBytes, err := d2graph.SerializeGraph(g)
Expand Down
5 changes: 4 additions & 1 deletion e2etests/report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"oss.terrastruct.com/d2/lib/log"
timelib "oss.terrastruct.com/d2/lib/time"
)

//go:embed template.html
Expand Down Expand Up @@ -69,8 +70,10 @@ func main() {

if !*skipTests {
ctx := log.Stderr(context.Background())
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
alixander marked this conversation as resolved.
Show resolved Hide resolved

ctx, cancel := timelib.WithTimeout(ctx, 2*time.Minute)
defer cancel()

// don't want to pass empty args to CommandContext
args := []string{"test", testDir, testMatchString}
if cpuProfileStr != "" {
Expand Down
11 changes: 11 additions & 0 deletions lib/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"os"
"strconv"
)

func Test() bool {
Expand All @@ -25,3 +26,13 @@ func DevOnly() bool {
func SkipGraphDiffTests() bool {
return os.Getenv("SKIP_GRAPH_DIFF_TESTS") != ""
}

func Timeout() (int, bool) {
if s := os.Getenv("D2_TIMEOUT"); s != "" {
i, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return int(i), true
}
}
return -1, false
}
26 changes: 26 additions & 0 deletions lib/time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package time

import (
"context"
"time"

"oss.terrastruct.com/d2/lib/env"
)

func HumanDate(t time.Time) string {
local := t.Local()
return local.Format(time.RFC822)
}

// WithTimeout returns context.WithTimeout(ctx, timeout) but timeout is overridden with D2_TIMEOUT if set
func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
t := timeout
if seconds, has := env.Timeout(); has {
t = time.Duration(seconds) * time.Second
}
if t <= 0 {
return ctx, func() {}
}

return context.WithTimeout(ctx, t)
}