summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-sched/job.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-09-26 19:23:41 +0000
committerLibravatar GitHub <noreply@github.com>2024-09-26 19:23:41 +0000
commitf3e2d36d6455e6d16aba833bdf64806377c8178f (patch)
tree8a1e87f349205e8073b72df6e5349c553db80a17 /vendor/codeberg.org/gruf/go-sched/job.go
parent[chore] Fix some contrast issues in themes; performance tweaks (#3358) (diff)
downloadgotosocial-f3e2d36d6455e6d16aba833bdf64806377c8178f.tar.xz
[chore] update go-sched pkg (#3357)
* update go-sched to v1.2.4 which removes some now unused dependencies * whoops, remove test output
Diffstat (limited to 'vendor/codeberg.org/gruf/go-sched/job.go')
-rw-r--r--vendor/codeberg.org/gruf/go-sched/job.go32
1 files changed, 23 insertions, 9 deletions
diff --git a/vendor/codeberg.org/gruf/go-sched/job.go b/vendor/codeberg.org/gruf/go-sched/job.go
index e94c00024..2531769d6 100644
--- a/vendor/codeberg.org/gruf/go-sched/job.go
+++ b/vendor/codeberg.org/gruf/go-sched/job.go
@@ -4,9 +4,9 @@ import (
"reflect"
"strconv"
"strings"
+ "sync/atomic"
"time"
-
- "codeberg.org/gruf/go-atomics"
+ "unsafe"
)
// Job encapsulates logic for a scheduled job to be run according
@@ -14,7 +14,7 @@ import (
// holding onto a next execution time safely in a concurrent environment.
type Job struct {
id uint64
- next atomics.Time
+ next unsafe.Pointer // *time.Time
timing Timing
call func(time.Time)
panic func(interface{})
@@ -33,9 +33,6 @@ func NewJob(fn func(now time.Time)) *Job {
panic: func(i interface{}) { panic(i) },
}
- // Init next time ptr
- j.next.Store(zerotime)
-
return j
}
@@ -99,14 +96,20 @@ func (job *Job) OnPanic(fn func(interface{})) *Job {
// Next returns the next time this Job is expected to run.
func (job *Job) Next() time.Time {
- return job.next.Load()
+ return loadTime(&job.next)
}
// Run will execute this Job and pass through given now time.
func (job *Job) Run(now time.Time) {
defer func() {
- if r := recover(); r != nil {
+ switch r := recover(); {
+ case r == nil:
+ // no panic
+ case job != nil &&
+ job.panic != nil:
job.panic(r)
+ default:
+ panic(r)
}
}()
job.call(now)
@@ -120,10 +123,21 @@ func (job *Job) String() string {
buf.WriteString(strconv.FormatUint(job.id, 10))
buf.WriteByte(' ')
buf.WriteString("next=")
- buf.WriteString(job.next.Load().Format(time.StampMicro))
+ buf.WriteString(loadTime(&job.next).Format(time.StampMicro))
buf.WriteByte(' ')
buf.WriteString("timing=")
buf.WriteString(reflect.TypeOf(job.timing).String())
buf.WriteByte('}')
return buf.String()
}
+
+func loadTime(p *unsafe.Pointer) time.Time {
+ if p := atomic.LoadPointer(p); p != nil {
+ return *(*time.Time)(p)
+ }
+ return zerotime
+}
+
+func storeTime(p *unsafe.Pointer, t time.Time) {
+ atomic.StorePointer(p, unsafe.Pointer(&t))
+}