/
timeout.go
60 lines (50 loc) · 1.52 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2020-2021 C4 Project
//
// This file is part of c4t.
// Licenced under the MIT licence; see `LICENSE`.
package quantity
import (
"context"
"log"
"time"
)
// Timeout is a Duration with the semantics of non-positive values being an absence of timeout.
type Timeout time.Duration
// IsActive checks whether t represents a valid, active timeout.
func (t Timeout) IsActive() bool {
return 0 < t
}
// Log dumps this timeout to the logger l, if it is active.
func (t Timeout) Log(l *log.Logger) {
if t.IsActive() {
l.Printf("timeout at %s", t)
}
}
// OnContext lifts this timeout to a context with the given parent.
// If this timeout is active, the semantics are as context.WithTimeout; else, context.WithCancel.
func (t Timeout) OnContext(parent context.Context) (context.Context, context.CancelFunc) {
if !t.IsActive() {
return context.WithCancel(parent)
}
return context.WithTimeout(parent, time.Duration(t))
}
// String forwards the usual duration stringification for timeouts.
func (t Timeout) String() string {
return time.Duration(t).String()
}
// MarshalText marshals a timeout by stringifying it.
func (t Timeout) MarshalText() (text []byte, err error) {
return []byte(t.String()), nil
}
// UnmarshalText unmarshals a timeout by using ParseDuration.
func (t *Timeout) UnmarshalText(in []byte) error {
d, err := time.ParseDuration(string(in))
*t = Timeout(d)
return err
}
// Override overrides this timeout with new if new is active.
func (t *Timeout) Override(new Timeout) {
if new.IsActive() {
*t = new
}
}