timewheel

package module
v0.0.0-...-2fa820f Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2018 License: GPL-3.0 Imports: 7 Imported by: 0

README

timewheel

provide a golang library of timewheel refer to the linux kernel timer.

Go Report Card Build Status

Install

go get github.com/brucemaclin/timewheel

Use

package main
import (
 tw "github.com/brucemaclin/timewheel"
 "time"
 "strconv"
 "fmt"
)
  
func getTestTask(interval int64) *tw.Task {
  task := &tw.Task{
    Expires: uint64(time.Now().UnixNano()/1000/1000 + interval),
  }
  task.Handle = test
  task.Data = &result{
    succ: make(chan string),
    data: time.Now().String(),
  }
  task.NeedCycle = true
  task.JobID = strconv.Itoa(int(interval))
  task.RunWeekdays[2] = true
  task.RunWeekdays[3] = true
  return task
}

func test(v interface{}) {
  d := v.(*result)
  fmt.Printf("my first timewheel %v\n", d.data)
  d.succ <- "succ"
}

type result struct {
 succ chan string
 data string
}

var mytw *tw.TimeWheel

func init() {
  mytw = tw.InitTimeWheel(1000)
  go mytw.Run()
}
func main() {
  task0 := getTestTask(0)
  mytw.AddTimer(task0)
  succ := <-task0.Data.(*result).succ
  fmt.Println(succ)
 }

Documentation

Index

Constants

View Source
const (
	TVNBITS uint = 6
	TVRBITS uint = 8
	TVNSIZE      = 1 << 6
	TVRSIZE      = 1 << 8
	TVNMASK      = TVNSIZE - 1
	TVRMASK      = TVRSIZE - 1
	MAXTVAL      = 1<<32 - 1
)
View Source
const (
	//FatalLevel fatalerror
	FatalLevel = 0
	//ErrorLevel error happen
	ErrorLevel = 1
	//WarnLevel just warn something wrong
	WarnLevel = 2
	//InfoLevel info what happen
	InfoLevel = 3
	//DebugLevel for debug
	DebugLevel = 4
)

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug for debug lowest level

func Debugf

func Debugf(format string, v ...interface{})

Debugf same as Debug

func Debugln

func Debugln(v ...interface{})

Debugln same as Debug

func Error

func Error(v ...interface{})

Error error level

func Errorf

func Errorf(format string, v ...interface{})

Errorf error level

func Errorln

func Errorln(v ...interface{})

Errorln error level

func Fatal

func Fatal(v ...interface{})

Fatal fatal error happen

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf same as fatal

func Index

func Index(n int, jiffies uint64) uint64

Index get nowtime index

func Info

func Info(v ...interface{})

Info info level

func Infof

func Infof(format string, v ...interface{})

Infof info level

func Logger

func Logger(filename string, level string) *os.File

Logger init log func

func Warning

func Warning(v ...interface{})

Warning warning level

func Warningf

func Warningf(format string, v ...interface{})

Warningf warning level

func Warningln

func Warningln(v ...interface{})

Warningln warning level

Types

type Task

type Task struct {
	JobID       string
	Expires     uint64 //time left to run
	Deleted     bool
	Handle      func(interface{}) //user's func to run
	Data        interface{}       //user's data for Handle to use
	NeedCycle   bool              //if needcycle if will check runweekdays
	RunWeekdays [7]bool           //which day need to run
	RunTime     string            //save the first time to  run
	AddDay      string
}

Task save job to run user's Handle

type TimeWheel

type TimeWheel struct {
	Interval uint64
	// contains filtered or unexported fields
}

TimeWheel make 5 floors to save

func InitTimeWheel

func InitTimeWheel(interval uint64) *TimeWheel

InitTimeWheel init and set interval

func (*TimeWheel) AddTimer

func (tw *TimeWheel) AddTimer(task *Task)

AddTimer add new task to timewheel

func (*TimeWheel) Delete

func (tw *TimeWheel) Delete(jobID string)

Delete delete the tw

func (*TimeWheel) Modify

func (tw *TimeWheel) Modify(task *Task)

Modify modify the tw first delete and then add

func (*TimeWheel) Run

func (tw *TimeWheel) Run()

Run start the tw

Jump to

Keyboard shortcuts

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