timer

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

README

timer

The timer module provides a context-aware Sleep function for Go applications.

Go Reference Go Report Card

Installation

To install the timer module, use the following command:

go get github.com/goaux/timer

Usage

Here's a basic example of how to use the Sleep function:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"

    "github.com/goaux/timer"
)

func main() {
    // Create a context that will be canceled when SIGINT is received
    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
    defer stop()

    fmt.Println("Starting sleep for 10 seconds. Press Ctrl+C to interrupt.")

    // Try to sleep for 10 seconds
    err := timer.SleepCause(ctx, 10*time.Second)

    if err != nil {
        fmt.Printf("Sleep interrupted: %v\n", err)
    } else {
        fmt.Println("Sleep completed successfully")
    }

    // Note: The actual output may vary depending on whether 
    // the user interrupts the program or not.
    // Example output if interrupted:
    // Starting sleep for 10 seconds. Press Ctrl+C to interrupt.
    // Sleep interrupted: context canceled
}

Function

SleepCause
func SleepCause(ctx context.Context, d time.Duration) error

SleepCause pauses the current goroutine for the specified duration or until the context is canceled. It returns nil if the sleep completes normally, or the cause of the context cancellation (as returned by context.Cause) if canceled.

Sleep
func Sleep(ctx context.Context, d time.Duration) error

Sleep pauses the current goroutine for the specified duration or until the context is canceled. It returns nil if the sleep completes normally, or the context's error if the context is canceled before the duration elapses.

Documentation

Overview

Package timer provides a context-aware Sleep function.

It offers a way to pause execution for a specified duration, while respecting context cancellation.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/goaux/timer"
)

func main() {
	// Create a context with a timeout of 2 seconds
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	// Try to sleep for 1 second
	fmt.Println("Starting sleep for 1 second")
	err := timer.SleepCause(ctx, 1*time.Second)
	if err != nil {
		fmt.Printf("Sleep interrupted: %v\n", err)
	} else {
		fmt.Println("Sleep completed successfully")
	}

	// Try to sleep for 3 seconds (longer than the context timeout)
	fmt.Println("Starting sleep for 3 seconds")
	err = timer.Sleep(ctx, 3*time.Second)
	if err != nil {
		fmt.Printf("Sleep interrupted: %v\n", err)
	} else {
		fmt.Println("Sleep completed successfully")
	}
}
Output:

Starting sleep for 1 second
Sleep completed successfully
Starting sleep for 3 seconds
Sleep interrupted: context deadline exceeded

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep pauses the current goroutine for the specified duration or until the context is canceled.

It returns nil if the sleep completes normally, or the context's error if the context is canceled before the duration elapses.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/goaux/timer"
)

func main() {
	// Create a context with a timeout of 2 seconds
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	// Try to sleep for 1 second
	fmt.Println("Starting sleep for 1 second")
	err := timer.Sleep(ctx, 1*time.Second)
	if err != nil {
		fmt.Printf("Sleep interrupted: %v\n", err)
	} else {
		fmt.Println("Sleep completed successfully")
	}

	// Try to sleep for 3 seconds (longer than the context timeout)
	fmt.Println("Starting sleep for 3 seconds")
	err = timer.Sleep(ctx, 3*time.Second)
	if err != nil {
		fmt.Printf("Sleep interrupted: %v\n", err)
	} else {
		fmt.Println("Sleep completed successfully")
	}
}
Output:

Starting sleep for 1 second
Sleep completed successfully
Starting sleep for 3 seconds
Sleep interrupted: context deadline exceeded

func SleepCause added in v1.1.0

func SleepCause(ctx context.Context, d time.Duration) error

SleepCause pauses the current goroutine for the specified duration or until the context is canceled. It returns nil if the sleep completes normally, or the cause of the context cancellation (as returned by context.Cause) if canceled.

This function requires Go 1.20+ to use context.Cause. It provides more detailed error information compared to Sleep.

Types

This section is empty.

Jump to

Keyboard shortcuts

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