time

package module
v0.0.0-...-fa5f906 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// ErrTimerIsNotRunning indicates that timer is not running
	ErrTimerIsNotRunning = E("timer is not running")
	// ErrTimerIsAlreadyRunning indicates that timer is already running
	ErrTimerIsAlreadyRunning = E("timer is already running")
)

Variables

View Source
var (
	// Beijing 表示北京时间
	Beijing = time.FixedZone("Asia/Beijing", 8*60*60)
)

Functions

func SleepToNextSecond

func SleepToNextSecond()

SleepToNextSecond sleep 到墙上时钟的下一秒

func SleepToNextSecondsN

func SleepToNextSecondsN(n int)

SleepToNextSecondsN sleep x N 版

func UpTime

func UpTime() time.Duration

UpTime 返回程序启动了多久。单调时钟

func Wait

func Wait(f func() (done bool), interval ...time.Duration)

Wait 等待是否满足条件,如果不满足则自旋等待。interval 如果不传参的话,默认是 100ms。如果传参的话,最小则为 1ms

Types

type E

type E string

E is the internal error type

func (E) Error

func (e E) Error() string

Error implements errors interface

type PeriodicSleeper

type PeriodicSleeper interface {
	Sleep(d time.Duration)
	Reset()
}

PeriodicSleeper 用在以下场景: 每次 sleep 结束之后, 当指定下一次 sleep 时长 T 时, 不会 完全按照 T 来 sleep, 而是参照自身上一次 sleep 的预期时间 tₙ₋₁ + T 来 sleep。这与 Tick 类似, 不同的是每次 sleep 的时间需要重新指定。

需要注意的是: 如果 tₙ₋₁ + T 小于当前时间, 则会触发一次 Reset 动作之后再 + T

func NewPeriodicSleeper

func NewPeriodicSleeper() PeriodicSleeper

NewPeriodicSleeper 以当前时间新建一个 PeriodicSleeper

type Tick

type Tick interface {
	Run()
	Stop()
	SetCallback(fn TickCallback)
}

Tick 提供一个长期尽可能周期性的 tick

func NewTickBeta

func NewTickBeta(interval time.Duration, callback TickCallback) (Tick, error)

NewTickBeta 新建一个 tick, 目前暂时实现毫秒级, 再低了不支持。此外, 仅支持精确到毫秒, 更低的不支持

此外, 目前理论上再高并发时会有竞争问题, 建议不要频繁创建销毁

type TickCallback

type TickCallback func(param TickCallbackParam)

type TickCallbackParam

type TickCallbackParam struct {
}

type TimeoutCallback

type TimeoutCallback func()

TimeoutCallback 表示超时时的回调函数

type Timer

type Timer interface {
	// Run starts timer in the background.
	Run() error

	// Stop stops the timer if it is running.
	Stop() error

	// Running check whether timer is running.
	Running() bool

	// Remain returns remaining time in the timer
	Remain() time.Duration

	// Elapsed returns elapsed time in the timer
	Elapsed() time.Duration
}

Timer is re-packaged time.Timer object, but have different methods.

func NewTimer

func NewTimer(d time.Duration, cb TimeoutCallback) Timer

NewTimer returns a new timer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL