timer

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

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

Go to latest
Published: Nov 23, 2022 License: MIT Imports: 2 Imported by: 1

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("timer closed")
	ErrInvalidExpr = errors.New("invalid cron desc")
)

Functions

This section is empty.

Types

type TaskID

type TaskID int64
const (
	EmptyTaskID TaskID = 0
)

func (TaskID) Int64

func (t TaskID) Int64() int64

type Timer

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

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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