service

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 4 Imported by: 3

Documentation

Overview

Package service enhances service orchestration by grouping related functionality, wrapping functions to support context-aware execution and add lifecycle-oriented functionality like signal handling in cloud-native environments.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context added in v0.1.20

func Context() (context.Context, context.CancelFunc)

Context creates a new context with a cancel function that listens for SIGTERM, SIGINT, SIGQUIT, and SIGKILL signals.

func RegisterOnContextDone added in v0.1.20

func RegisterOnContextDone(ctx context.Context, fn func())

RegisterOnContextDone waits for the context to be done and then calls the function.

Types

type Function

type Function[IN, OUT any] func(ctx context.Context, in IN) (out OUT, err error)

Function gathers together things that change for the same reasons. A context must be handled to be "cloud native" because it allows propagation of deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. This ensures efficient resource utilization and proper handling of asynchronous workflows in distributed systems.

func Wrap

func Wrap[IN, OUT any](fn func(in IN) (OUT, error)) Function[IN, OUT]

Wrap converts a simple function into one that respects a provided context. This enables the wrapped function to respond to context cancellation or timeout.

Example
package main

import (
	"context"
	"fmt"

	"github.com/andygeiss/cloud-native-utils/service"
)

func main() {
	// Wrap turns a plain function into one that honours a context.
	double := service.Wrap(func(n int) (int, error) { return n * 2, nil })

	out, err := double(context.Background(), 21)
	fmt.Println(out, err)

	// A cancelled context stops the call before the function runs.
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	_, err = double(ctx, 21)
	fmt.Println(err)
}
Output:
42 <nil>
context canceled

Jump to

Keyboard shortcuts

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