ctxutils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 6 Imported by: 0

README

ctxutils Package

The ctxutils package provides utilities for managing context, closing resources, and handling graceful shutdowns in a safe and error-handling manner.

Types

Closeable

The Closeable interface defines methods for resources that can be closed with error handling.

type Closeable interface {
    IsClosed() bool
    Close() error
}
Methods
  • IsClosed() bool: Checks if the resource is already closed. Returns true if closed, otherwise false.
  • Close() error: Closes the resource and returns an error if any issues occur during the closing process.
ShutdownFunc

The ShutdownFunc type defines the function signature for shutdown procedures.

type ShutdownFunc func() error

Functions

CloseResource

The CloseResource function attempts to close a Closeable resource if it is not already closed.

func CloseResource(c Closeable) error
Parameters
  • c Closeable: The resource that implements the Closeable interface.
Returns
  • error: Returns nil if the resource is already closed or closed successfully. If an error occurs during closing, it returns an error wrapped with a descriptive message.
Example
err := ctxutils.CloseResource(myResource)
if err != nil {
    log.Fatalf("Failed to close resource: %v", err)
}
WaitForShutdown

The WaitForShutdown function waits for an interrupt or context cancellation signal to perform a graceful shutdown.

func WaitForShutdown(ctx context.Context, shutdown ShutdownFunc, cancel context.CancelFunc)
Parameters
  • ctx context.Context: The context to wait for cancellation or interrupt signal.
  • shutdown ShutdownFunc: The function to execute for shutdown procedures.
  • cancel context.CancelFunc: The function to cancel the context.
Example
ctx, cancel := context.WithCancel(context.Background())
shutdownFunc := func() error {
    // Perform your shutdown procedures here.
    return nil
}

go ctxutils.WaitForShutdown(ctx, shutdownFunc, cancel)

// Simulate some work
time.Sleep(10 * time.Second)

// To trigger shutdown from code
cancel()
Usage Example
package main

import (
    "context"
    "log"
    "time"
    "github.com/Sectoid-Systems/sectoid-go-kit/ctxutils"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())

    shutdownFunc := func() error {
        // Perform your shutdown procedures here.
        return nil
    }

    go ctxutils.WaitForShutdown(ctx, shutdownFunc, cancel)

    // Simulate some work
    log.Println("Working...")
    time.Sleep(10 * time.Second)

    // To trigger shutdown from code
    cancel()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAsNeeded added in v0.3.0

func CloseAsNeeded(c CloseableWithCheck, lf misc.LoggerFunc)

CloseAsNeeded closes the resource if it is not already closed. It logs any errors that occur during the closing process.

func CloseResource

func CloseResource(c Closeable, lf misc.LoggerFunc)

CloseResource attempts to close the resource and logs any errors that occur.

func WaitForShutdown

func WaitForShutdown(ctx context.Context, shutdown ShutdownFunc)

WaitForShutdown waits for an interrupt or context cancellation signal to perform a shutdown.

Types

type Closeable

type Closeable interface {
	Close() error
}

Closeable is an interface that defines a resource with a Close method.

type CloseableWithCheck added in v0.3.0

type CloseableWithCheck interface {
	Closeable
	IsClosed() bool
}

CloseableWithCheck extends Closeable, adding the IsClosed method to check if the resource is already closed.

type ShutdownFunc

type ShutdownFunc func() error

ShutdownFunc defines the function signature for shutdown procedures.

Jump to

Keyboard shortcuts

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