xrun

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

xrun build PkgGoDev Go Report Card

Utilities around starting multiple components which are long running components, example: an HTTP server or a background worker

Install

$ go get github.com/ajatprabha/xrun

Usage

API reference

Credits

RunnableManager source modified from sigs.k8s.io/controller-runtime

Documentation

Overview

package xrun provides utilities around starting multiple components which are long running components, example: an HTTP server or a background worker

package main

import (
    "net/http"
    "os"

    "github.com/ajatprabha/xrun"
)

func main() {
    m := xrun.NewRunnableManager()
    server := http.Server{
        Addr: ":9090",
    }
    _ = m.Add(xrun.HttpServer(&server, xrun.NoTimeout))

    if err := m.Start(xrun.SetupSignalHandler()); err != nil {
        os.Exit(1)
    }
}

Index

Constants

View Source
const NoTimeout = time.Duration(0)

Variables

This section is empty.

Functions

func SetupSignalHandler

func SetupSignalHandler() context.Context

SetupSignalHandler registers for SIGTERM and SIGINT. A stop channel is returned which is closed on one of these signals. If a second signal is caught, the program is terminated with exit code 1.

Types

type Option

type Option func(*manager)

Option changes behaviour of RunnableManager

func WithGracefulShutdownTimeout

func WithGracefulShutdownTimeout(timeout time.Duration) Option

WithGracefulShutdownTimeout allows max timeout after which RunnableManager exits

type Runnable

type Runnable interface {
	// Start starts running the component. The component will stop running
	// when the context is closed. Start blocks until the context is closed or
	// an error occurs.
	Start(context.Context) error
}

Runnable allows a component to be started. It's very important that Start blocks until it's done running.

type RunnableFunc

type RunnableFunc func(context.Context) error

RunnableFunc implements Runnable using a function. It's very important that the given function block until it's done running.

func HttpServer

func HttpServer(s *http.Server, shutdownTimeout time.Duration) RunnableFunc

HttpServer adds an http.Server to RunnableManager and waits for shutdownTimeout while shutting it down

func (RunnableFunc) Start

func (rf RunnableFunc) Start(ctx context.Context) error

type RunnableManager

type RunnableManager interface {
	Runnable

	// Add will include the component, and the component will be
	// started when Start is called.
	Add(Runnable) error
}

RunnableManager helps to start multiple components and waits for them to complete

func NewRunnableManager

func NewRunnableManager(opts ...Option) RunnableManager

Jump to

Keyboard shortcuts

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