configure timeout with D2_TIMEOUT

This commit is contained in:
Gavin Nishizawa 2023-06-09 16:44:10 -07:00
parent 5004123b30
commit 7547d51727
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
4 changed files with 44 additions and 6 deletions

View file

@ -31,6 +31,7 @@ import (
"oss.terrastruct.com/d2/d2themes" "oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/d2themes/d2themescatalog" "oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/background" "oss.terrastruct.com/d2/lib/background"
"oss.terrastruct.com/d2/lib/env"
"oss.terrastruct.com/d2/lib/imgbundler" "oss.terrastruct.com/d2/lib/imgbundler"
ctxlog "oss.terrastruct.com/d2/lib/log" ctxlog "oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/pdf" "oss.terrastruct.com/d2/lib/pdf"
@ -298,8 +299,16 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return w.run() return w.run()
} }
ctx, cancel := context.WithTimeout(ctx, time.Minute*2) timeout := time.Minute * 2
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() defer cancel()
}
_, written, err := compile(ctx, ms, plugin, renderOpts, fontFamily, *animateIntervalFlag, inputPath, outputPath, *bundleFlag, *forceAppendixFlag, pw.Page) _, written, err := compile(ctx, ms, plugin, renderOpts, fontFamily, *animateIntervalFlag, inputPath, outputPath, *bundleFlag, *forceAppendixFlag, pw.Page)
if err != nil { if err != nil {

View file

@ -15,6 +15,7 @@ import (
"oss.terrastruct.com/util-go/xmain" "oss.terrastruct.com/util-go/xmain"
"oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/env"
) )
// execPlugin uses the binary at pathname with the plugin protocol to implement // execPlugin uses the binary at pathname with the plugin protocol to implement
@ -147,8 +148,15 @@ func (p *execPlugin) Info(ctx context.Context) (_ *PluginInfo, err error) {
} }
func (p *execPlugin) Layout(ctx context.Context, g *d2graph.Graph) error { func (p *execPlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
ctx, cancel := context.WithTimeout(ctx, time.Minute) 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() defer cancel()
}
graphBytes, err := d2graph.SerializeGraph(g) graphBytes, err := d2graph.SerializeGraph(g)
if err != nil { if err != nil {

View file

@ -15,6 +15,7 @@ import (
"text/template" "text/template"
"time" "time"
"oss.terrastruct.com/d2/lib/env"
"oss.terrastruct.com/d2/lib/log" "oss.terrastruct.com/d2/lib/log"
) )
@ -69,8 +70,17 @@ func main() {
if !*skipTests { if !*skipTests {
ctx := log.Stderr(context.Background()) ctx := log.Stderr(context.Background())
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
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() defer cancel()
}
// don't want to pass empty args to CommandContext // don't want to pass empty args to CommandContext
args := []string{"test", testDir, testMatchString} args := []string{"test", testDir, testMatchString}
if cpuProfileStr != "" { if cpuProfileStr != "" {

11
lib/env/env.go vendored
View file

@ -2,6 +2,7 @@ package env
import ( import (
"os" "os"
"strconv"
) )
func Test() bool { func Test() bool {
@ -25,3 +26,13 @@ func DevOnly() bool {
func SkipGraphDiffTests() bool { func SkipGraphDiffTests() bool {
return os.Getenv("SKIP_GRAPH_DIFF_TESTS") != "" 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
}