package module
Version: v0.0.0-...-bf22e5c Latest Latest

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

Go to latest
Published: Apr 6, 2017 License: MIT Imports: 6 Imported by: 0


Events Timer

Build Status Coverage Status Go Report Card

Min Heap Events Timer




View Source
const (
	// InfiniteDuration is the default time that timer bLocks.
	InfiniteDuration = time.Duration(1<<63 - 1)


View Source
var (
	// DefaultAllocCap is the expanding capicity when the timer has no capicity to store more events.
	DefaultAllocCap = 1024

	// ErrStarted represents timer has started
	ErrStarted = errors.New("timer has started")
	// ErrNotStarted represents timer has not started
	ErrNotStarted = errors.New("timer has not started")
	// ErrStopped represents timer has stopped
	ErrStopped = errors.New("timer has stopped")


This section is empty.


type Event

type Event struct {
	// contains filtered or unexported fields

An Event represents an elemenet of the events in the timer.

func (*Event) Delay

func (e *Event) Delay() time.Duration

Delay is used to give the duration that event will expire.

func (*Event) Less

func (e *Event) Less(o *Event) bool

Less is used to compare expiration with other events.

func (*Event) String

func (e *Event) String() string

type ExpireFunc

type ExpireFunc func()

ExpireFunc represents a function will be executed when a event is trigged.

type Timer

type Timer struct {
	// contains filtered or unexported fields

A Timer represents a set that manage events which is related with time. It uses min-heap structure to orginaze and handle events.

func New

func New() *Timer

New returns an timer instance with the default allocate capicity

func NewWithCap

func NewWithCap(cap int) *Timer

NewWithCap returns an timer instance with the given allocate capicity

func (*Timer) Add

func (t *Timer) Add(ttl time.Duration, fn ExpireFunc) *Event

Add is used to add new event

func (*Timer) Del

func (t *Timer) Del(event *Event)

Del is used to remove event from timer. If event is nil, will retrun directly.

func (*Timer) Events

func (t *Timer) Events() []*Event

Events return the currently events in the timer.

func (*Timer) Init

func (t *Timer) Init(cap int)

Init inits the timer instance with the given allocate capicity

func (*Timer) IsStopped

func (t *Timer) IsStopped() bool

IsStopped is used to show the timer is whether stopped.

func (*Timer) Len

func (t *Timer) Len() int

Len return the length of min heap array.

func (*Timer) Set

func (t *Timer) Set(event *Event, ttl time.Duration) bool

Set is used to update the ttl of specified event. It returns true if the call reset event, false if the vents has already deleted.

func (*Timer) Start

func (t *Timer) Start() error

Start is used to start the timer.

func (*Timer) Stop

func (t *Timer) Stop() error

Stop is used to stop the timer.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL