timekeeper

package
v1.0.0-rc4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package timekeeper provides a high-performance cached time implementation optimized for high-throughput applications that need fast time access without the overhead of syscalls on every time.Now() call.

This implementation uses atomic operations and periodic updates to provide microsecond-precision cached time values with configurable refresh rates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Now

func Now() time.Time

Now returns the cached current time using the global cache instance. This is the primary function for high-throughput time access.

func SetRefreshInterval

func SetRefreshInterval(interval time.Duration)

SetRefreshInterval changes the global cache refresh interval. Use this to tune performance vs precision for your specific workload.

func Since

func Since(t time.Time) time.Duration

Since returns the time elapsed since t using the cached current time.

func Stop

func Stop()

Stop stops the global cache refresh goroutine. This should typically only be called during application shutdown.

Types

type Cache

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

Cache holds the cached time value and provides thread-safe access

func NewCache

func NewCache(refreshInterval time.Duration) *Cache

NewCache creates a new time cache with the specified refresh interval. A smaller interval provides more accurate time but uses more CPU. Recommended intervals:

  • 1ms for high-precision requirements
  • 5ms for balanced performance (default)
  • 10-50ms for maximum throughput with lower precision needs

func (*Cache) Now

func (c *Cache) Now() time.Time

Now returns the cached current time This is an extremely fast operation using only an atomic load

func (*Cache) SetRefreshInterval

func (c *Cache) SetRefreshInterval(interval time.Duration)

SetRefreshInterval changes the refresh interval dynamically

func (*Cache) Start

func (c *Cache) Start()

Start begins the background refresh goroutine

func (*Cache) Stop

func (c *Cache) Stop()

Stop halts the background refresh goroutine

Jump to

Keyboard shortcuts

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