import "github.com/andy2046/tik"
Package tik implements Hierarchical Timing Wheels.
- Variables
- type Callback
- type Config
- type DefaultTimer
- type Option
- type Ticker
- type Timeout
- type Timer
bit.go ticker.go timeout.go timer.go
var (
// DefaultConfig is the default Ticker Config.
DefaultConfig = Config{
WheelBitNum: 6,
WheelNum: 4,
Timer: nil,
}
)
type Callback func()
Callback function to trigger when timeout expires.
type Config struct {
// number of value bits mapped in each wheel.
WheelBitNum uint8
// number of wheels.
WheelNum uint8
// Timer to progress the timing wheel.
Timer Timer
}
Config used to init Ticker.
type DefaultTimer struct {
// contains filtered or unexported fields
}
DefaultTimer implements Timer interface.
func (dt *DefaultTimer) Now() uint64
Now returns the absolute time when timer started in millisecond.
func (dt *DefaultTimer) Step() <-chan uint64
Step timing wheel by absolute time in millisecond.
func (dt *DefaultTimer) Stop()
Stop the DefaultTimer.
type Option = func(*Config)
Option applies config to Ticker Config.
type Ticker struct {
// contains filtered or unexported fields
}
Ticker progress the timing wheels.
func New(options ...Option) *Ticker
New initiates a new Ticker.
func (*Ticker) AnyExpired
func (tk *Ticker) AnyExpired() bool
AnyExpired returns true if expiry queue is not empty, false otherwise.
func (*Ticker) AnyPending
func (tk *Ticker) AnyPending() bool
AnyPending returns true if there is task in wheels, false otherwise.
func (tk *Ticker) Cancel(to *Timeout)
Cancel the Timeout scheduled if it has not yet expired.
func (tk *Ticker) Close()
Close stop processing any task, whether it is pending or expired.
func (tk *Ticker) IsClosed() bool
IsClosed returns true if closed, false otherwise.
func (tk *Ticker) Schedule(delay uint64, cb Callback) *Timeout
Schedule creates a one-shot action that executed after the given delay.
delay
is the time from now to delay execution,
the time unit of the delay depends on the Timer provided, default is millisecond.
cb
is the task to execute.
it returns nil
if Ticker is closed.
type Timeout struct {
// contains filtered or unexported fields
}
Timeout represents user timeout logic.
func (to *Timeout) Expired() bool
Expired returns true if timeout is in expired queue, false otherwise.
func (to *Timeout) Pending() bool
Pending returns true if timeout is in timing wheel, false otherwise.
type Timer interface {
// Now returns the absolute time when timer started.
Now() uint64
// Step channel to step timing wheel by absolute time.
Step() <-chan uint64
// Stop the timer.
Stop()
}
Timer progress the timing wheel by current time.
func NewTimer(interval uint64) Timer
NewTimer returns DefaultTimer with interval in millisecond.
Generated by godoc2md