gotime

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 3 Imported by: 0

README

timer

timerheap&timerwheel written in Go.

usage

package main

import (
    "fmt"
    "time"

    "github.com/pyihe/timer/timewheel"
)

func main() {
    tmr := timewheel.New(10*time.Millisecond, 256, 16)
    defer tmr.Stop()
    
    afterID, err := tmr.After(1*time.Second, func() {
        fmt.Println("after 1 second")
    })
    if err != nil {
        //handle err
    }
    
    // operate taskID if necessary
    _ = afterID

    everyID, err := tmr.Every(1*time.Second, func() {
        fmt.Println("every 1 second")
    })
    if err != nil {
        //handle err
    }
    
    // operate taskId if necessary
    _ = everyID
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilFunc     = errors.New("function must not be nil")
	ErrTimerClosed = errors.New("time closed")
	ErrInvalidExpr = errors.New("invalid cron desc")
)

Functions

func SleepWithCtx

func SleepWithCtx(ctx context.Context, d time.Duration) (err error)

SleepWithCtx 携带ctx的Sleep,可以通过ctx控制提前终止sleep

Types

type Timer

type Timer interface {
	Stop()                                            // 停止定时器
	Delete(uint64) error                              // 删除指定ID的任务
	After(d time.Duration, fn func()) (uint64, error) // duration后执行一次, 返回任务ID
	Every(d time.Duration, fn func()) (uint64, error) // 每duration执行一次, 返回任务ID
	Cron(desc string, fn func()) (uint64, error)      // 按照给定的DESC执行任务
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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