aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/time.go
diff options
context:
space:
mode:
authorSlack Coder <slackcoder@server.ky>2024-10-23 06:07:32 -0500
committerSlack Coder <slackcoder@server.ky>2024-10-23 06:14:49 -0500
commitca3a097505c42c2aec16e208c571c1b496c4b14b (patch)
treeaa1020524d6166ad2e527c15864f42e0ce15301b /internal/service/time.go
parentc8ba7263e7923fc76eee7dbe129a8bfbc9501bb7 (diff)
downloadmirror-ca3a097505c42c2aec16e208c571c1b496c4b14b.tar.xz
Support reload and refactor config
- Simplify config parameters. - Support hot config reloading.
Diffstat (limited to 'internal/service/time.go')
-rw-r--r--internal/service/time.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/internal/service/time.go b/internal/service/time.go
new file mode 100644
index 0000000..b275b82
--- /dev/null
+++ b/internal/service/time.go
@@ -0,0 +1,59 @@
+package service
+
+import (
+ "math/rand"
+ "time"
+)
+
+type Duration struct {
+ time.Duration
+}
+
+func DurationRef(v time.Duration) *Duration {
+ return &Duration{v}
+}
+
+func (s Duration) MarshalText() ([]byte, error) {
+ return []byte(s.Duration.String()), nil
+}
+
+func (s *Duration) Max(args ...*Duration) Duration {
+ m := s
+
+ for _, o := range args {
+ if m.Duration < o.Duration {
+ v := o
+ m = v
+ }
+ }
+
+ return *m
+}
+
+func (s *Duration) Min(args ...*Duration) Duration {
+ m := s
+
+ for _, o := range args {
+ if m.Duration > o.Duration {
+ v := o
+ m = v
+ }
+ }
+
+ return *m
+}
+
+func (s *Duration) UnmarshalText(text []byte) error {
+ v, err := time.ParseDuration(string(text))
+ if err != nil {
+ return err
+ }
+ *s = Duration{v}
+
+ return nil
+}
+
+func RandomDuration(from Duration, until Duration) Duration {
+ period := until.Duration - from.Duration
+ return Duration{from.Duration + time.Duration(rand.Intn(int(period)))}
+}