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
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
11 changes: 10 additions & 1 deletion d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/background"
"oss.terrastruct.com/d2/lib/imgbundler"
"oss.terrastruct.com/d2/lib/log"
ctxlog "oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/pdf"
"oss.terrastruct.com/d2/lib/png"
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", "", 0, "the number of seconds before d2 will timeout. default is 60s.")
alixander marked this conversation as resolved.
Show resolved Hide resolved
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 := log.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"
"oss.terrastruct.com/d2/lib/log"
)

// 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 := log.WithTimeout(ctx, time.Minute)
defer cancel()

graphBytes, err := d2graph.SerializeGraph(g)
Expand Down
4 changes: 3 additions & 1 deletion e2etests/report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,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 := log.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
}
14 changes: 14 additions & 0 deletions lib/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"runtime/debug"
"testing"
"time"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
Expand Down Expand Up @@ -112,3 +113,16 @@ func Stderr(ctx context.Context) context.Context {

return With(ctx, l)
}

// 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) {
gavin-ts marked this conversation as resolved.
Show resolved Hide resolved
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)
}