timewheel

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

cluster-timewheel

介绍

go实现的时间轮,可单机可分布式

安装
go get gitee.com/burybell/cluster-timewheel@v1.0.0
案例
单机
package main

import (
	"fmt"
	timewheel "gitee.com/burybell/cluster-timewheel"
	"time"
)

const (
	Call1 timewheel.CallId = iota
)

func init() {

	timewheel.AddCall(Call1, func(ctx *timewheel.Context) {
		fmt.Println("call1", ctx.String())
	})

}

func main() {

	local := timewheel.NewLocal(time.Second, 60)

	local.Run()

	for i := 0; i < 1000; i++ {
		context := timewheel.NewContext("cl1")
		local.AddTimer(time.Second*time.Duration(i), fmt.Sprintf("id-%d", i), context, Call1)
	}

	select {}
}

分布式

package main

import (
	"fmt"
	timewheel "gitee.com/burybell/cluster-timewheel"
	"github.com/go-redis/redis"
	"time"
)

const (
	Call1 timewheel.CallId = iota
)

func init() {

	timewheel.AddCall(Call1, func(ctx *timewheel.Context) {
		fmt.Println("call1", ctx.String())
	})

}

func main() {

	client := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
	cluster := timewheel.NewCluster(client, timewheel.Options{
		Key:      "test",
		Interval: time.Second,
		SlotNums: 60,
	})

	cluster.Run()

	for i := 0; i < 1000; i++ {
		context := timewheel.NewContext("cl1")
		cluster.AddTimer(time.Second*time.Duration(i), fmt.Sprintf("id-%d", i), context, Call1)
	}

	select {}
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCall

func AddCall(id CallId, call Call)

AddCall 添加全剧回调函数

func CallExist

func CallExist(id CallId) bool

func Int added in v1.0.1

func Int(str string) int

func Int64 added in v1.0.1

func Int64(str string) int64

func RemoveCall

func RemoveCall(id CallId)

Types

type Call

type Call func(ctx *Context)

func GetCall

func GetCall(id CallId) Call

type CallId

type CallId int

type CallPool

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

func (*CallPool) Add

func (pool *CallPool) Add(id CallId, call Call)

func (*CallPool) Exist

func (pool *CallPool) Exist(id CallId) bool

func (*CallPool) Remove

func (pool *CallPool) Remove(id CallId)

type ClusterTimeWheel

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

func (*ClusterTimeWheel) AddTimer

func (wheel *ClusterTimeWheel) AddTimer(delay time.Duration, id string, ctx *Context, callId CallId)

func (*ClusterTimeWheel) RemoveTimer

func (wheel *ClusterTimeWheel) RemoveTimer(id string)

func (*ClusterTimeWheel) Run

func (wheel *ClusterTimeWheel) Run() TimeWheel

func (*ClusterTimeWheel) Stop

func (wheel *ClusterTimeWheel) Stop()

func (*ClusterTimeWheel) UnMarkLocation added in v1.0.1

func (wheel *ClusterTimeWheel) UnMarkLocation(id string)

type Context

type Context struct {
	Name string                 `json:"name,omitempty"`
	Data map[string]interface{} `json:"data,omitempty"`
}

Context 定时器上下文

func NewContext

func NewContext(name string) *Context

func (Context) Val added in v1.0.1

func (c Context) Val() string

type LocalTimeWheel

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

LocalTimeWheel 基于本地内存实现

func (*LocalTimeWheel) AddTimer

func (wheel *LocalTimeWheel) AddTimer(delay time.Duration, id string, ctx *Context, callId CallId)

func (*LocalTimeWheel) RemoveTimer

func (wheel *LocalTimeWheel) RemoveTimer(id string)

func (*LocalTimeWheel) Run

func (wheel *LocalTimeWheel) Run() TimeWheel

func (LocalTimeWheel) Stop

func (wheel LocalTimeWheel) Stop()

type Node

type Node struct {
	Element *Target // 任务
	Next    *Node   // 下一个节点
}

type Options

type Options struct {
	Interval time.Duration `json:"interval,omitempty"`
	SlotNums int           `json:"slotNums,omitempty"`
	MergeVal bool          `json:"merge,omitempty"`
}

func (*Options) Val added in v1.0.1

func (options *Options) Val() string

type OrderList

type OrderList struct {
	Header *Node
	// contains filtered or unexported fields
}

func NewOrderList

func NewOrderList() *OrderList

func (*OrderList) Add

func (list *OrderList) Add(task *Target)

func (*OrderList) Consumer

func (list *OrderList) Consumer(each func(task *Target) bool)

func (*OrderList) Foreach

func (list *OrderList) Foreach(each func(task *Target))

func (*OrderList) Remove

func (list *OrderList) Remove(task *Target)

type Target added in v1.0.1

type Target struct {
	Id      string   `json:"id"`
	Delay   int64    `json:"delay"`
	Circle  int      `json:"circle"`
	Context *Context `json:"context"`
	CallId  CallId   `json:"callId"`
}

type TimeWheel

type TimeWheel interface {
	AddTimer(delay time.Duration, id string, ctx *Context, callId CallId)
	RemoveTimer(id string)
	Run() TimeWheel
	Stop()
}

func NewCluster

func NewCluster(client *redis.Client, key string, options *Options) TimeWheel

func NewLocal

func NewLocal(interval time.Duration, slotNums ...int) TimeWheel

Jump to

Keyboard shortcuts

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