From fcd4fefb642720e7b3bae16c9243080453c4e386 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Wed, 14 Jun 2023 11:47:43 -0700 Subject: [PATCH] move to timelib.WithTimeout --- d2cli/main.go | 4 ++-- d2plugin/exec.go | 4 ++-- e2etests/report/main.go | 3 ++- lib/log/log.go | 14 -------------- lib/time/time.go | 26 ++++++++++++++++++++++++++ 5 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 lib/time/time.go diff --git a/d2cli/main.go b/d2cli/main.go index 9ae124b20..81c0de2da 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -32,12 +32,12 @@ 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" "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" @@ -307,7 +307,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { return w.run() } - ctx, cancel := log.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) diff --git a/d2plugin/exec.go b/d2plugin/exec.go index bc9835846..9e5b4821f 100644 --- a/d2plugin/exec.go +++ b/d2plugin/exec.go @@ -15,7 +15,7 @@ import ( "oss.terrastruct.com/util-go/xmain" "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 @@ -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 { - ctx, cancel := log.WithTimeout(ctx, time.Minute) + ctx, cancel := timelib.WithTimeout(ctx, time.Minute) defer cancel() graphBytes, err := d2graph.SerializeGraph(g) diff --git a/e2etests/report/main.go b/e2etests/report/main.go index e50fca540..481cc9fd1 100644 --- a/e2etests/report/main.go +++ b/e2etests/report/main.go @@ -16,6 +16,7 @@ import ( "time" "oss.terrastruct.com/d2/lib/log" + timelib "oss.terrastruct.com/d2/lib/time" ) //go:embed template.html @@ -70,7 +71,7 @@ func main() { if !*skipTests { ctx := log.Stderr(context.Background()) - ctx, cancel := log.WithTimeout(ctx, 2*time.Minute) + ctx, cancel := timelib.WithTimeout(ctx, 2*time.Minute) defer cancel() // don't want to pass empty args to CommandContext diff --git a/lib/log/log.go b/lib/log/log.go index 681da7231..d70edf0b8 100644 --- a/lib/log/log.go +++ b/lib/log/log.go @@ -8,7 +8,6 @@ import ( "os" "runtime/debug" "testing" - "time" "cdr.dev/slog" "cdr.dev/slog/sloggers/sloghuman" @@ -113,16 +112,3 @@ 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) -} diff --git a/lib/time/time.go b/lib/time/time.go new file mode 100644 index 000000000..a9a1f692b --- /dev/null +++ b/lib/time/time.go @@ -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) +}