grace

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Grace

Grace is a Go package designed to assist in implementing graceful shutdown processes for Go applications. It provides a straightforward and efficient way to manage long-running processes, background tasks, or any operations that need to be cleanly shut down when your application receives a termination signal such as SIGINT or SIGTERM.

Features

  • Graceful Shutdown: Grace offers a simple yet powerful mechanism to handle graceful shutdowns, ensuring all your operations are properly completed before the application exits.
  • Concurrency Safe: With built-in concurrency control, Grace safely manages multiple operations across different goroutines, ensuring thread-safety.
  • Singleton Design: Grace implements a singleton design pattern, ensuring that only one instance of the shutdown manager is created throughout the application lifecycle. This approach simplifies managing graceful shutdowns and concurrency across your entire application by providing a central point of control.

Installation

To use Grace in your Go project, run go get github.com/TechMDW/grace in your terminal.

Usage

Import the Grace package into your Go application:

import "github.com/TechMDW/grace"

Below is a simple example demonstrating how to use Grace to handle a graceful shutdown:

package main

import (
  "context"
  "os"
  "os/signal"
  "syscall"
  "time"

  "github.com/TechMDW/grace"
)


func main() {
	// Some program logic
	go func() {
		grace.Add()          // Register a new task
		grace.AddX(2)        // Register multiple tasks at once
		defer grace.Done()   // Unregister a task
		defer grace.DoneX(2) // Unregister multiple tasks at once
		time.Sleep(10 * time.Second)
	}()

	// Setup a signal handler
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

	// Wait for a termination signal
	// Note: This will block until a SIGINT or SIGTERM is received
	<-sigChan

	// Add context with timeout so we don't wait forever just in case
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	fmt.Println("Waiting for all resources to finish:", grace.Count())
	// This will block until all resources are finished or the context timeouts/cancel.
	grace.WaitCtx(ctx)
	os.Exit(0)
}

This example sets up a basic signal handler that waits for SIGINT or SIGTERM. Upon receiving one of these signals, it initiates the graceful shutdown process with a 30-second timeout.

License

This project is licensed under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add()

Adds 1 resource to wait for.

func AddX

func AddX(x int)

Adds x resources to wait for.

func BlockAdd

func BlockAdd()

BlockAdd preventing Add() operations from modifying the count.

func Count

func Count() int64

Count returns the current amount of resources being waited for.

func Done

func Done()

Mark 1 resource as done.

func DoneX

func DoneX(x int)

Mark x resources as done.

func Grace

func Grace(ctx context.Context)

Grace blocks function execution until a SIGINT or SIGTERM is received. It will then wait for all resources to finish before returning. Pass a context to Grace to allow for a timeout. If the context is canceled, Grace will return immediately without waiting.

If a SIGINT or SIGTERM is received again, Grace will force shutdown after forceShutdownTries attempts (default: 2).

This function serves as more of a convenience/example function for a common use case. If you need more control over the shutdown process, you should use the Wait() or WaitCtx() functions.

func IsEmpty

func IsEmpty() bool

IsEmpty checks if the count variable is 0.

func Reset

func Reset()

Reset resets Grace to its initial state.

func SetForceShutdownTries

func SetForceShutdownTries(tries int)

SetForceShutdownTries sets the number of attempts to force shutdown after a SIGINT or SIGTERM is received.

This function only affects the grace.Grace() function.

func UnblockAdd

func UnblockAdd()

UnblockAdd allows Add() operations to modify the count.

func Wait

func Wait()

Wait blocks until the count becomes 0, indicating all Add() and Del() operations are completed.

func WaitCtx

func WaitCtx(ctx context.Context) error

WaitCtx blocks until the count becomes 0 or the provided context is canceled.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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