/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
go/libkb/bgticker.go
47 строк
1 KB
Joshua Blum
Use RandomJitter to delay background operations (#22936)
09 мар 2020, 20:31
Не верифицирован
09 мар 2020, 20:31
854985a
Код
Авторство
О чём код?
package libkb import ( "time" ) const DefaultBgTickerWait = 5 * time.Second type BgTicker struct { C <-chan time.Time c chan time.Time ticker *time.Ticker resumeWait time.Duration } // This ticker wrap's Go's time.Ticker to wait a given time.Duration before // firing. This is helpful to not overload the mobile apps when they are // brought to the foreground and all have tasks that are ready to fire. // NewBgTicker will panic if wait > duration as time.Ticker does with a // negative duration. func NewBgTicker(duration time.Duration) *BgTicker { return NewBgTickerWithWait(duration, DefaultBgTickerWait) } func NewBgTickerWithWait(duration time.Duration, wait time.Duration) *BgTicker { c := make(chan time.Time, 1) t := &BgTicker{ C: c, c: c, ticker: time.NewTicker(duration - wait), resumeWait: wait, } go t.tick() return t } func (t *BgTicker) tick() { for c := range t.ticker.C { time.Sleep(RandomJitter(t.resumeWait)) t.c <- c } } func (t *BgTicker) Stop() { t.ticker.Stop() }