Skip to content

Commit

Permalink
util func for overriding default timeout according to env var
Browse files Browse the repository at this point in the history
  • Loading branch information
gavin-ts committed Jun 14, 2023
1 parent a5b8703 commit 19cc5ee
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
12 changes: 3 additions & 9 deletions 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 @@ -306,15 +307,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return w.run()
}

timeout := time.Minute * 2
if timeoutFlag != nil {
timeout = time.Duration(*timeoutFlag) * time.Second
}
if timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
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)
if err != nil {
Expand Down
13 changes: 3 additions & 10 deletions d2plugin/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"oss.terrastruct.com/util-go/xmain"

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

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

func (p *execPlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
timeout := time.Minute
if seconds, has := env.Timeout(); has {
timeout = time.Duration(seconds) * time.Second
}
if timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
ctx, cancel := log.WithTimeout(ctx, time.Minute)
defer cancel()

graphBytes, err := d2graph.SerializeGraph(g)
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions e2etests/report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"text/template"
"time"

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

Expand Down Expand Up @@ -71,15 +70,8 @@ func main() {
if !*skipTests {
ctx := log.Stderr(context.Background())

timeout := 2 * time.Minute
if seconds, has := env.Timeout(); has {
timeout = time.Duration(seconds) * time.Second
}
if timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
ctx, cancel := log.WithTimeout(ctx, 2*time.Minute)
defer cancel()

// don't want to pass empty args to CommandContext
args := []string{"test", testDir, testMatchString}
Expand Down
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) {
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)
}

0 comments on commit 19cc5ee

Please sign in to comment.