ticker

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

README

ticker

Import path: github.com/InsideGallery/core/ticker

Overview

ticker provides a global tick counter, a periodic handler manager, and a delayed one-shot executor.

Main APIs

  • Tick, Get, and Reset manage the package-level atomic tick counter.
  • Handler is implemented by periodic workers with Tick(context.Context) and GetID().
  • TickHandler combines a context, interval, cancel function, and handler.
  • NewTickHandler creates a cancellable handler wrapper.
  • TickManager manages handlers with Add, Remove, Stop, Handlers, GetHandlers, Run, and CountTicksInProgress.
  • ExecuteWithDelay runs a callback after a delay with Start, Stop, and IsActive.

Usage

type heartbeat struct {
	id uint64
}

func (h heartbeat) ID() uint64 { return h.id }
func (h heartbeat) GetID() uint64 { return h.id }
func (h heartbeat) Tick(context.Context) {}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

manager := ticker.NewTickManager()
manager.Add(ticker.NewTickHandler(ctx, time.Second, heartbeat{id: 1}))

go manager.Run()

Notes

Run blocks until all handler loops finish. Each interval tick runs the handler in its own goroutine with a context timeout equal to the handler interval. Stop cancels all handlers, and Remove cancels and deletes one handler. ExecuteWithDelay.Start ignores a new start while the executor is already active.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get() uint64

Get return global tick counter

func Reset

func Reset()

Reset reset ticker

func Tick

func Tick(_ context.Context)

Tick increase global tick counter

Types

type ExecuteWithDelay

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

ExecuteWithDelay execute method with delay

func NewExecuteWithDelay

func NewExecuteWithDelay() *ExecuteWithDelay

NewExecuteWithDelay return new execute in delay

func (*ExecuteWithDelay) IsActive

func (e *ExecuteWithDelay) IsActive() bool

IsActive return true if active

func (*ExecuteWithDelay) Start

func (e *ExecuteWithDelay) Start(ctx context.Context, h func(ctx context.Context), d time.Duration)

Start start timer

func (*ExecuteWithDelay) Stop

func (e *ExecuteWithDelay) Stop() bool

Stop stop executing

type Handler

type Handler interface {
	Tick(ctx context.Context)
	// Deprecated: use ID.
	GetID() uint64
}

Handler describe tick handler

type TickHandler

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

TickHandler contains handler and interval

func NewTickHandler

func NewTickHandler(ctx context.Context, interval time.Duration, handler Handler) *TickHandler

NewTickHandler return new tick handler

type TickManager

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

TickManager describe TickManager

func NewTickManager

func NewTickManager() *TickManager

NewTickManager return tick manager

func (*TickManager) Add

func (t *TickManager) Add(h *TickHandler) uint64

Add add tick handler

func (*TickManager) CountTicksInProgress

func (t *TickManager) CountTicksInProgress() int32

CountTicksInProgress return count of ticks in progress

func (*TickManager) GetHandlers deprecated

func (t *TickManager) GetHandlers() map[uint64]*TickHandler

GetHandlers returns all handlers.

Deprecated: use Handlers.

func (*TickManager) Handlers added in v1.1.0

func (t *TickManager) Handlers() map[uint64]*TickHandler

Handlers returns all handlers.

func (*TickManager) Remove

func (t *TickManager) Remove(id uint64)

Remove handler

func (*TickManager) Run

func (t *TickManager) Run()

Run all tickers

func (*TickManager) Stop

func (t *TickManager) Stop()

Stop all handlers

Jump to

Keyboard shortcuts

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