gotosocial/vendor/codeberg.org/gruf/go-sched/timing.go
kim 7cc40302a5
[chore] consolidate caching libraries (#704)
* add miekg/dns dependency

* set/validate accountDomain

* move finger to dereferencer

* totally break GetRemoteAccount

* start reworking finger func a bit

* start reworking getRemoteAccount a bit

* move mention parts to namestring

* rework webfingerget

* use util function to extract webfinger parts

* use accountDomain

* rework finger again, final form

* just a real nasty commit, the worst

* remove refresh from account

* use new ASRepToAccount signature

* fix incorrect debug call

* fix for new getRemoteAccount

* rework GetRemoteAccount

* start updating tests to remove repetition

* break a lot of tests
Move shared test logic into the testrig,
rather than having it scattered all over
the place. This allows us to just mock
the transport controller once, and have
all tests use it (unless they need not to
for some other reason).

* fix up tests to use main mock httpclient

* webfinger only if necessary

* cheeky linting with the lads

* update mentionName regex
recognize instance accounts

* don't finger instance accounts

* test webfinger part extraction

* increase default worker count to 4 per cpu

* don't repeat regex parsing

* final search for discovered accountDomain

* be more permissive in namestring lookup

* add more extraction tests

* simplify GetParseMentionFunc

* skip long search if local account

* fix broken test

* consolidate to all use same caching libraries

Signed-off-by: kim <grufwub@gmail.com>

* perform more caching in the database layer

Signed-off-by: kim <grufwub@gmail.com>

* remove ASNote cache

Signed-off-by: kim <grufwub@gmail.com>

* update cache library, improve db tracing hooks

Signed-off-by: kim <grufwub@gmail.com>

* return ErrNoEntries if no account status IDs found, small formatting changes

Signed-off-by: kim <grufwub@gmail.com>

* fix tests, thanks tobi!

Signed-off-by: kim <grufwub@gmail.com>

Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2022-07-10 17:18:21 +02:00

93 lines
2.0 KiB
Go

package sched
import (
"time"
)
var (
// zerotime is zero time.Time (unix epoch).
zerotime = time.Time{}
// emptytiming is a global timingempty to check against.
emptytiming = timingempty{}
)
// Timing provides scheduling for a Job, determining the next time
// for given current time that execution is required. Please note that
// calls to .Next() may alter the results of the next call, and should
// only be called by the Scheduler.
type Timing interface {
Next(time.Time) time.Time
}
// timingempty is a 'zero' Timing implementation that always returns zero time.
type timingempty struct{}
func (timingempty) Next(time.Time) time.Time {
return zerotime
}
// Once implements Timing to provide a run-once Job execution.
type Once time.Time
func (o *Once) Next(time.Time) time.Time {
ret := *(*time.Time)(o)
*o = Once(zerotime) // reset
return ret
}
// Periodic implements Timing to provide a recurring Job execution.
type Periodic time.Duration
func (p Periodic) Next(now time.Time) time.Time {
return now.Add(time.Duration(p))
}
// PeriodicAt implements Timing to provide a recurring Job execution starting at 'Once' time.
type PeriodicAt struct {
Once Once
Period Periodic
}
func (p *PeriodicAt) Next(now time.Time) time.Time {
if next := p.Once.Next(now); !next.IsZero() {
return next
}
return p.Period.Next(now)
}
// TimingWrap allows combining two different Timing implementations.
type TimingWrap struct {
Outer Timing
Inner Timing
// determined next times
outerNext time.Time
innerNext time.Time
}
func (t *TimingWrap) Next(now time.Time) time.Time {
if t.outerNext.IsZero() {
// Regenerate outermost next run time
t.outerNext = t.Outer.Next(now)
}
if t.innerNext.IsZero() {
// Regenerate innermost next run time
t.innerNext = t.Inner.Next(now)
}
// If outer comes before inner, return outer
if t.outerNext != zerotime &&
t.outerNext.Before(t.innerNext) {
next := t.outerNext
t.outerNext = zerotime
return next
}
// Else, return inner
next := t.innerNext
t.innerNext = zerotime
return next
}