timer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

type Cron struct {
	// contains filtered or unexported fields
}

Cron

Example
package main

import (
	"fmt"
	"github.com/name5566/leaf/timer"
)

func main() {
	d := timer.NewDispatcher(10)

	// cron expr
	cronExpr, err := timer.NewCronExpr("* * * * * *")
	if err != nil {
		return
	}

	// cron
	var c *timer.Cron
	c = d.CronFunc(cronExpr, func() {
		fmt.Println("My name is Leaf")
		c.Stop()
	})

	// dispatch
	(<-d.ChanTimer).Cb()

}
Output:

My name is Leaf

func (*Cron) Stop

func (c *Cron) Stop()

type CronExpr

type CronExpr struct {
	// contains filtered or unexported fields
}

Field name | Mandatory? | Allowed values | Allowed special characters ---------- | ---------- | -------------- | -------------------------- Seconds | No | 0-59 | * / , - Minutes | Yes | 0-59 | * / , - Hours | Yes | 0-23 | * / , - Day of month | Yes | 1-31 | * / , - Month | Yes | 1-12 | * / , - Day of week | Yes | 0-6 | * / , -

Example
package main

import (
	"fmt"
	"github.com/name5566/leaf/timer"
	"time"
)

func main() {
	cronExpr, err := timer.NewCronExpr("0 * * * *")
	if err != nil {
		return
	}

	fmt.Println(cronExpr.Next(time.Date(
		2000, 1, 1,
		20, 10, 5,
		0, time.UTC,
	)))

}
Output:

2000-01-01 21:00:00 +0000 UTC

func NewCronExpr

func NewCronExpr(expr string) (cronExpr *CronExpr, err error)

goroutine safe

func (*CronExpr) Next

func (e *CronExpr) Next(t time.Time) time.Time

goroutine safe

type Dispatcher

type Dispatcher struct {
	ChanTimer chan *Timer
}

one dispatcher per goroutine (goroutine not safe)

func NewDispatcher

func NewDispatcher(l int) *Dispatcher

func (*Dispatcher) AfterFunc

func (disp *Dispatcher) AfterFunc(d time.Duration, cb func()) *Timer

func (*Dispatcher) CronFunc

func (disp *Dispatcher) CronFunc(cronExpr *CronExpr, _cb func()) *Cron

type Timer

type Timer struct {
	// contains filtered or unexported fields
}

Timer

Example
package main

import (
	"fmt"
	"github.com/name5566/leaf/timer"
)

func main() {
	d := timer.NewDispatcher(10)

	// timer 1
	d.AfterFunc(1, func() {
		fmt.Println("My name is Leaf")
	})

	// timer 2
	t := d.AfterFunc(1, func() {
		fmt.Println("will not print")
	})
	t.Stop()

	// dispatch
	(<-d.ChanTimer).Cb()

}
Output:

My name is Leaf

func (*Timer) Cb

func (t *Timer) Cb()

func (*Timer) Stop

func (t *Timer) Stop()

Jump to

Keyboard shortcuts

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