move to timelib.WithTimeout
This commit is contained in:
parent
19cc5ee0e2
commit
fcd4fefb64
5 changed files with 32 additions and 19 deletions
|
|
@ -32,12 +32,12 @@ import (
|
||||||
"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/imgbundler"
|
"oss.terrastruct.com/d2/lib/imgbundler"
|
||||||
"oss.terrastruct.com/d2/lib/log"
|
|
||||||
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"
|
||||||
"oss.terrastruct.com/d2/lib/png"
|
"oss.terrastruct.com/d2/lib/png"
|
||||||
"oss.terrastruct.com/d2/lib/pptx"
|
"oss.terrastruct.com/d2/lib/pptx"
|
||||||
"oss.terrastruct.com/d2/lib/textmeasure"
|
"oss.terrastruct.com/d2/lib/textmeasure"
|
||||||
|
timelib "oss.terrastruct.com/d2/lib/time"
|
||||||
"oss.terrastruct.com/d2/lib/version"
|
"oss.terrastruct.com/d2/lib/version"
|
||||||
"oss.terrastruct.com/d2/lib/xgif"
|
"oss.terrastruct.com/d2/lib/xgif"
|
||||||
|
|
||||||
|
|
@ -307,7 +307,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
return w.run()
|
return w.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := log.WithTimeout(ctx, time.Minute*2)
|
ctx, cancel := timelib.WithTimeout(ctx, time.Minute*2)
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +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/log"
|
timelib "oss.terrastruct.com/d2/lib/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// execPlugin uses the binary at pathname with the plugin protocol to implement
|
// execPlugin uses the binary at pathname with the plugin protocol to implement
|
||||||
|
|
@ -148,7 +148,7 @@ 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 := log.WithTimeout(ctx, time.Minute)
|
ctx, cancel := timelib.WithTimeout(ctx, time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
graphBytes, err := d2graph.SerializeGraph(g)
|
graphBytes, err := d2graph.SerializeGraph(g)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/lib/log"
|
"oss.terrastruct.com/d2/lib/log"
|
||||||
|
timelib "oss.terrastruct.com/d2/lib/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed template.html
|
//go:embed template.html
|
||||||
|
|
@ -70,7 +71,7 @@ func main() {
|
||||||
if !*skipTests {
|
if !*skipTests {
|
||||||
ctx := log.Stderr(context.Background())
|
ctx := log.Stderr(context.Background())
|
||||||
|
|
||||||
ctx, cancel := log.WithTimeout(ctx, 2*time.Minute)
|
ctx, cancel := timelib.WithTimeout(ctx, 2*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// don't want to pass empty args to CommandContext
|
// don't want to pass empty args to CommandContext
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"cdr.dev/slog"
|
"cdr.dev/slog"
|
||||||
"cdr.dev/slog/sloggers/sloghuman"
|
"cdr.dev/slog/sloggers/sloghuman"
|
||||||
|
|
@ -113,16 +112,3 @@ func Stderr(ctx context.Context) context.Context {
|
||||||
|
|
||||||
return With(ctx, l)
|
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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
26
lib/time/time.go
Normal file
26
lib/time/time.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue