-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1392 from gavin-ts/custom-timeout
configure timeout with D2_TIMEOUT
- Loading branch information
Showing
7 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |