flower

package module
v0.0.0-...-cd38c7e Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 4 Imported by: 0

README

Flower

flower (flow er) is a package that provides an idiomatic, simple and an expressive way of defining how services (goroutines) should be started and gracefully closed.

Go Reference Build Status Coverage Status Go Report Card

Features

  • Simple API
  • Uses context.Context
  • Hookable functions (e.g. BeforeServiceStart)
  • Configurable panic recovery

Installing

go get -u github.com/advbet/flower

Concept

The key concept comes from flower's ServiceGroup structure. It is a unit that is composed out of many services (goroutines). Within a service group, all goroutines start/close nondeterministically. However, the order of service groups are respected; each service group will start only after the previous group has started, and will close only when the superseding service group is closed. See _example for more details.

Important

Project is still in experimental phase, breaking changes may happen.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, opts Options, groups ...ServiceGroup)

Run runs the provided service groups. If more than one service group is passed, it will run groups in the same order as they were provided. Each service group represents a collection of services that are independent. Upon context cancellation, a graceful terminate is initiated; it will start by closing the last service group and then close the group before it. While doing so, it ensures that it begins closing the second group if and only if the group before it gracefully closed (i.e., all services returned). For example, let us say that we have 3 service groups, SG1, SG2, SG3. If we do Run(ctx, logger, SG1, SG2, SG3), it will run the groups in following sequence: SG1 -> SG2 -> SG3. When ctx is cancelled, it will shut down in the following order: SG3 -> SG2 -> SG1. In case any service during runtime panics, it will restart it with the provided RecoverDuration (or default, if not set).

Types

type Options

type Options struct {
	// RecoverDuration specifies the duration after which a service
	// will attempt to recover after panic.
	RecoverDuration time.Duration

	// BeforeServiceStart is a function that is executed when a service
	// is about to start.
	BeforeServiceStart func(name string)

	// AfterServiceStop is a function that is executed when a service
	// stops. It is not invoked after a panic.
	AfterServiceStop func(name string)

	// AfterServicePanic is a function that is executed when a service
	// panics.
	AfterServicePanic func(name string, stack []byte)
}

Options specifies additional options that can be provided when running flower.

type Service

type Service interface {
	// Run should run the service with the provided context and logger.
	// If context is cancelled, the service should close gracefully.
	Run(context.Context)
}

Service is an interface that represents a service that can be ran as a separate, independent goroutine.

type ServiceFunc

type ServiceFunc func(context.Context)

ServiceFunc is a wrapper around a service function that enables it to behave like a Service.

func (ServiceFunc) Run

func (sf ServiceFunc) Run(ctx context.Context)

Run runs the provided service.

type ServiceGroup

type ServiceGroup map[string]Service

ServiceGroup represents a group of services where the key is service name and the value is the service itself.

Jump to

Keyboard shortcuts

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