1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/mirrors-Gubernator

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
interval.go 4.5 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
Shawn Poulson Отправлено 17.02.2022 22:06 47b7b2d
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"errors"
"time"
"github.com/mailgun/holster/v4/clock"
"github.com/mailgun/holster/v4/syncutil"
)
// Interval is a one-shot ticker. Call `Next()` to trigger the start of an
// interval. Read the `C` channel for tick event.
type Interval struct {
C chan struct{}
in chan struct{}
wg syncutil.WaitGroup
}
// NewInterval creates a new ticker like object, however
// the `C` channel does not return the current time and
// `C` channel will only get a tick after `Next()` has
// been called.
func NewInterval(d clock.Duration) *Interval {
i := Interval{
C: make(chan struct{}, 1),
in: make(chan struct{}, 1),
}
i.run(d)
return &i
}
func (i *Interval) run(d clock.Duration) {
i.wg.Until(func(done chan struct{}) bool {
select {
case <-i.in:
time.Sleep(d)
i.C <- struct{}{}
return true
case <-done:
return false
}
})
}
func (i *Interval) Stop() {
i.wg.Stop()
}
// Next queues the next interval to run, If multiple calls to Next() are
// made before previous intervals have completed they are ignored.
func (i *Interval) Next() {
select {
case i.in <- struct{}{}:
default:
}
}
const (
GregorianMinutes int64 = iota
GregorianHours
GregorianDays
GregorianWeeks
GregorianMonths
GregorianYears
)
// GregorianDuration returns the entire duration of the Gregorian interval
func GregorianDuration(now clock.Time, d int64) (int64, error) {
switch d {
case GregorianMinutes:
return 60000, nil
case GregorianHours:
return 3.6e+6, nil
case GregorianDays:
return 8.64e+7, nil
case GregorianWeeks:
return 0, errors.New("`Duration = GregorianWeeks` not yet supported; consider making a PR!`")
case GregorianMonths:
y, m, _ := now.Date()
// Given the beginning of the month, subtract the end of the current month to get the duration
begin := clock.Date(y, m, 1, 0, 0, 0, 0, now.Location())
end := begin.AddDate(0, 1, 0).Add(-clock.Nanosecond)
return end.UnixNano() - begin.UnixNano()/1000000, nil
case GregorianYears:
y, _, _ := now.Date()
// Given the beginning of the year, subtract the end of the current year to get the duration
begin := clock.Date(y, clock.January, 1, 0, 0, 0, 0, now.Location())
end := begin.AddDate(1, 0, 0).Add(-clock.Nanosecond)
return end.UnixNano() - begin.UnixNano()/1000000, nil
}
return 0, errors.New("behavior DURATION_IS_GREGORIAN is set; but `Duration` is not a valid gregorian interval")
}
// GregorianExpiration returns an gregorian interval as defined by the
// 'DURATION_IS_GREGORIAN` Behavior. it returns the expiration time as the
// end of GREGORIAN interval in milliseconds from `now`.
//
// Example: If `now` is 2019-01-01 11:20:10 and `d` = GregorianMinutes then the return
// expire time would be 2019-01-01 11:20:59 in milliseconds since epoch
func GregorianExpiration(now clock.Time, d int64) (int64, error) {
switch d {
case GregorianMinutes:
return now.Truncate(clock.Minute).
Add(clock.Minute-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianHours:
y, m, d := now.Date()
// See time.Truncate() documentation on why we can' reliably use time.Truncate(Hour) here.
return clock.Date(y, m, d, now.Hour(), 0, 0, 0, now.Location()).
Add(clock.Hour-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianDays:
y, m, d := now.Date()
return clock.Date(y, m, d, 23, 59, 59, int(clock.Second-clock.Nanosecond), now.Location()).
UnixNano() / 1000000, nil
case GregorianWeeks:
return 0, errors.New("`Duration = GregorianWeeks` not yet supported; consider making a PR!`")
case GregorianMonths:
y, m, _ := now.Date()
return clock.Date(y, m, 1, 0, 0, 0, 0, now.Location()).
AddDate(0, 1, 0).Add(-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianYears:
y, _, _ := now.Date()
return clock.Date(y, clock.January, 1, 0, 0, 0, 0, now.Location()).
AddDate(1, 0, 0).
Add(-clock.Nanosecond).
UnixNano() / 1000000, nil
}
return 0, errors.New("behavior DURATION_IS_GREGORIAN is set; but `Duration` is not a valid gregorian interval")
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/mirrors-Gubernator.git
git@api.gitlife.ru:oschina-mirror/mirrors-Gubernator.git
oschina-mirror
mirrors-Gubernator
mirrors-Gubernator
master