Start and finish your Go apps gracefully, even in the case of panics

Relax

Relax - verb antonym for panic.

In the context of Go programs, relax means to make critical failures less severe so that graceful shutdown is never jeopardized.

Relaxed Go programs start and finish gracefully, even in the case of SIGINT, SIGTERM, and concurrent panics.

Relaxed Go programs will only shutdown after all running operations and connections have completed and closed, respectively.

This module is intended to aid in the development of relaxed Go programs. The main challenge in achieving this is to ensure that all panicking goroutines are recovered and lead to the graceful shutdown of the program. Read about panic and recover here for more detail.

Usage

Import the pkg:

import (
	"github.com/sergerad/relax"
)

Instantiate the error group and main context in the main goroutine.

func main() {
	mainCtx, cancel := relax.MainContext()

This ensures that the main goroutine is relaxed against SIGINT and SIGTERM.

If you have multiple, long running processes to run in your program, you can use errgroup to launch them.

	g, ctx := errgroup.WithContext(mainCtx)

Launch your goroutines and make sure to defer relax.Recover(cancel) so that any panics do not get in the way of graceful shutdown of the program.

	g.Go(func() error {
		defer relax.Recover(cancel)
		return myLongRunningProcess(ctx)
	})

You can use relax.Recover(cancel) for any sub-goroutine that you launch in your program at all. If you launch goroutines without deferring

Finally, in the main goroutine, make sure to wait for the error group:

	if err := g.Wait(); err != nil {
		if err != nil {
			fmt.Println("error from group", err)
		}
	}

For more detailed usage, see examples.

Owner
Similar Resources

Best simple, lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqL Database and Clean Architecture

GOLANG FIBER API (CLEAN ARCHITECTURE) Best simple, lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqLDatabase using

Sep 2, 2022

Flexible E-Commerce Framework on top of Flamingo. Used to build E-Commerce "Portals" and connect it with the help of individual Adapters to other services.

Flamingo Commerce With "Flamingo Commerce" you get your toolkit for building fast and flexible commerce experience applications. A demoshop using the

Dec 31, 2022

gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具

gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具

中文文档 Automatic parameter binding base on go-gin doc Golang gin automatic parameter binding Support for RPC automatic mapping Support object registrati

Jan 3, 2023

A quick and easy way to setup a RESTful JSON API

Go-Json-Rest A quick and easy way to setup a RESTful JSON API Go-Json-Rest is a thin layer on top of net/http that helps building RESTful JSON APIs ea

Jan 3, 2023

A small and evil REST framework for Go

go-rest A small and evil REST framework for Go Reflection, Go structs, and JSON marshalling FTW! go get github.com/ungerik/go-rest import "github.com/

Dec 6, 2022

hiboot is a high performance web and cli application framework with dependency injection support

Hiboot - web/cli application framework About Hiboot is a cloud native web and cli application framework written in Go. Hiboot is not trying to reinven

Nov 20, 2022

Package macaron is a high productive and modular web framework in Go.

Package macaron is a high productive and modular web framework in Go.

Macaron Package macaron is a high productive and modular web framework in Go. Getting Started The minimum requirement of Go is 1.6. To install Macaron

Jan 2, 2023

Mango is a modular web-application framework for Go, inspired by Rack, and PEP333.

Mango Mango is a modular web-application framework for Go, inspired by Rack and PEP333. Note: Not actively maintained. Overview Mango is most of all a

Nov 17, 2022
Related tags
Go-app is a package to build progressive web apps with Go programming language and WebAssembly.
Go-app is a package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package to build progressive web apps with Go programming language and WebAssembly.

Dec 30, 2022
Web framework for creating apps using Go in Google AppEngine

Welcome to app.go v3.0 app.go is a simple web framework for use in Google AppEngine. Just copy the app folder to your working folder and import it fro

Mar 21, 2021
A framework for apps written entirely in YAML, powered by ytt.

yapp A framework for apps written entirely in YAML, powered by ytt. Highly experimental! Do not use! # start the server... go run . -f examples/hello-

May 6, 2022
🚀‏‏‎ ‎‏‏‎‏‏‎‎‎‎‎‎Copper is a Go toolkit complete with everything you need to build web apps.

Copper Copper is a Go toolkit complete with everything you need to build web apps. It focuses on developer productivity and makes building web apps in

Jan 7, 2023
A simple blog framework built with GO. Uses HTML files and a JSON dict to give you more control over your content.

Go-Blog A simple template based blog framework. Instructions Built for GO version: 1 See the Documentation or Getting Started pages in the wiki. Notes

Sep 10, 2022
Include files in your binary the easy way

New Projects should use the official embed package instead, which was added in go 1.16. binclude binclude is a tool for including static files into Go

Dec 18, 2022
Impersonate Your GitHub App In A GitHub Action

Impersonate Your GitHub App In A GitHub Action This action helps you retrieve an authenticated app token with a GitHub app id and a app private key. Y

Nov 29, 2021
REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base
REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base

Backend API Example FiberGo Framework Docs : https://github.com/gofiber Info This application using firebase ecosystem Firebase Auth Cloud Storage Fir

May 31, 2022
Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.
Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.

Flamingo Framework Flamingo is a web framework based on Go. It is designed to build pluggable and maintainable web projects. It is production ready, f

Jan 5, 2023
Dec 28, 2022