service

package
v1.4.20 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package service provides simple service framework on top of Module interface. Ready made modules can be found under: github.com/elisasre/go-common/service/module.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPanic = errors.New("recovered from panic")

Functions

func Run

func Run(svc Service) error

Run runs svc using following control flow:

  1. Exec Init() for each module in order. If error is occurred Run returns immediately.
  2. Exec Run() for each module in own goroutine.
  3. Wait for any Run() function to return. When that happens move to Stop sequence.
  4. Exec Stop() for modules in reverse order.
  5. Wait for all Run() and Stop() calls to return.
  6. Return all errors or nil

Possible panics inside modules are captured to allow graceful shutdown of other modules. Captured panics are converted into errors and ErrPanic is returned.

Example
package main

import (
	"fmt"
	"net/http"
	"os"
	"syscall"
	"time"

	"github.com/elisasre/go-common/service"
	"github.com/elisasre/go-common/service/module/httpserver"
	"github.com/elisasre/go-common/service/module/httpserver/pprof"
	"github.com/elisasre/go-common/service/module/siglistener"
)

func main() {
	// Send SIGINT after 5 second.
	go func() {
		time.Sleep(time.Second * 5)
		syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint: errcheck
	}()

	err := service.Run(service.Modules{
		siglistener.New(os.Interrupt),
		httpserver.New(
			httpserver.WithAddr(":6062"),
			pprof.WithProfiling(),
		),
		httpserver.New(
			httpserver.WithServer(&http.Server{ReadHeaderTimeout: time.Second}),
			httpserver.WithAddr(":6060"),
			httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Fprint(w, "Hello")
			})),
		),
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Service exited successfully")
}
Output:

Service exited successfully

func RunAndExit

func RunAndExit(svc Service)

Types

type Module

type Module interface {
	Name() string
	Init() error
	Run() error
	Stop() error
}

type Modules

type Modules []Module

Modules is a convenience type for modules to avoid implementing Service when not necessary.

func (Modules) Modules

func (m Modules) Modules() []Module

type Service

type Service interface {
	Modules() []Module
}

Service is a container for modules.

Directories

Path Synopsis
module
httpserver
Package httpserver provides http server as module.
Package httpserver provides http server as module.
httpserver/pprof
Package pprof provides pprof handler options for httpserver module.
Package pprof provides pprof handler options for httpserver module.
httpserver/prom
Package prom provides prometheus metrics handler options for httpserver module.
Package prom provides prometheus metrics handler options for httpserver module.
sentry
Package sentry provides sentry functionality as a module.
Package sentry provides sentry functionality as a module.
siglistener
Package siglistener provides signal listening as a module.
Package siglistener provides signal listening as a module.
ticker
Package ticker provides ticker functionality as a module.
Package ticker provides ticker functionality as a module.

Jump to

Keyboard shortcuts

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