gtimer

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

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 4 Imported by: 1

README

timer

golang-ci

Timer based on delayqueue

Design and implementation

The implementation is inspired by Java DelayQueue.java Portal: DelayQueue.java

The dependent structures are as follows:

timer -> delayqueue -> priorityqueue -> heap

Installation

Install:

go get -u github.com/vearne/gtimer

Import:

import "github.com/vearne/gtimer"

Quick Start

more examples

package main

import (
	"fmt"
	"github.com/vearne/gtimer"
	"time"
)

func main() {
	startTime := time.Now()
	gtimer.Add(3*time.Second, func() {
		fmt.Println(time.Since(startTime))
	})
	go func() {
		time.Sleep(5 * time.Second)
		gtimer.Stop()
	}()
	gtimer.Wait()
	fmt.Println("---end---")
}

Performance

CPU Model Name: 2.3 GHz Intel Core i5
CPU Processors: 4
Memory: 8GB

Benchmark Test Results
produce goroutines count consume goroutines count qps(per second)
1 1 285714
10 10 90090
10 100 89285
100 100 23255

Documentation

Overview

This example demonstrates a priority queue built using the heap interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Remove

func Remove(task *Item)

func Set

func Set(timer *SuperTimer)

func Size

func Size() int

func Stop

func Stop()

func Wait

func Wait()

Types

type AtomicBool

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

atomicBool is a wrapper around uint32 for usage as a boolean value with atomic access.

func NewAtomicBool

func NewAtomicBool(flag bool) *AtomicBool

func (*AtomicBool) IsSet

func (ab *AtomicBool) IsSet() bool

IsSet returns whether the current boolean value is true

func (*AtomicBool) IsTrue

func (ab *AtomicBool) IsTrue() bool

func (*AtomicBool) Set

func (ab *AtomicBool) Set(value bool)

Set sets the value of the bool regardless of the previous value

type Delayed

type Delayed interface {
	GetDelay() time.Duration
}

type Item

type Item struct {
	// when task is ready, execute OnTrigger function
	OnTrigger func(scheduledExecTime time.Time, param interface{})
	// contains filtered or unexported fields
}

nolint: govet An Item is something we manage in a priority queue.

func Add

func Add(d time.Duration, f func()) *Item

func AddComplex

func AddComplex(scheduledExecTime time.Time, f func(scheduledExecTime time.Time, param interface{}),
	param interface{}) *Item

func NewDelayedItemFunc

func NewDelayedItemFunc(triggerTime time.Time, param interface{}, f func(time.Time, interface{})) *Item

triggerTime is time of the task should be execute

func (*Item) GetDelay

func (item *Item) GetDelay() time.Duration

type PriorityQueue

type PriorityQueue []*Item

A PriorityQueue implements heap.Interface and holds Items.

func (*PriorityQueue) Clear

func (pq *PriorityQueue) Clear()

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

func (*PriorityQueue) Less

func (pq *PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek() interface{}

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(item *Item)

func (*PriorityQueue) Swap

func (pq *PriorityQueue) Swap(i, j int)

func (*PriorityQueue) Update

func (pq *PriorityQueue) Update(item *Item, value string, priority int64)

update modifies the priority and value of an Item in the queue.

type SuperTimer

type SuperTimer struct {
	Wgp *sync.WaitGroup

	UniTimer    *time.Timer
	WorkerCount int
	ExitChan    chan int
	PQ          PriorityQueue
	RunningFlag *AtomicBool
	// contains filtered or unexported fields
}

nolint: govet

func NewSuperTimer

func NewSuperTimer(workCount int) *SuperTimer

func (*SuperTimer) Add

func (st *SuperTimer) Add(pItem *Item)

func (*SuperTimer) Consume

func (timer *SuperTimer) Consume()

func (*SuperTimer) Remove

func (st *SuperTimer) Remove(pItem *Item)

func (*SuperTimer) Size

func (st *SuperTimer) Size() int

func (*SuperTimer) Stop

func (st *SuperTimer) Stop()

func (*SuperTimer) Take

func (st *SuperTimer) Take() *Item

func (*SuperTimer) Wait

func (st *SuperTimer) Wait()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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