Help us shape the future of Go! Take the 2026 Go Developer Survey

serverd

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

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

Go to latest
Published: Oct 23, 2019 License: MIT Imports: 9 Imported by: 4

README

serverd

Package serverd provides a component to create server-type applications, helping to manage the life cycle of services and operating system signals.

It allows:

  • Sequence startup and shutdown
  • Set a timeout for shutdown and force stop
  • Reload registered services
  • Ping registered services for checking status
  • Install signal handlers and manage interrupt and hup (reload) signals

Usage

See this simple example:

package main

import (
	"context"
	"log"
	"net"
	"net/http"

	"github.com/luisguillenc/serverd"
	"github.com/sirupsen/logrus"
)

func main() {
	// init logger
	logger := logrus.New()
	logger.SetLevel(logrus.DebugLevel)

	// creates serverd manager
	srv := serverd.New(serverd.SetLogger(logger))

	// creates first service
	lis1, err := net.Listen("tcp", "127.0.0.1:8111")
	if err != nil {
		log.Fatalf("listening: %v", err)
	}
	http1 := &http.Server{}
	// register first service
	srv.Register(serverd.Service{
		Name: "http1",
		Start: func() error {
			go http1.Serve(lis1)
			return nil
		},
		Shutdown: func() { http1.Shutdown(context.Background()) },
	})

	// creates second service
	lis2, err := net.Listen("tcp", "127.0.0.1:8112")
	if err != nil {
		log.Fatalf("listening: %v", err)
	}
	http2 := &http.Server{}
	// register second service
	srv.Register(serverd.Service{
		Name: "http2",
		Start: func() error {
			go http2.Serve(lis2)
			return nil
		},
		Shutdown: func() { http2.Shutdown(context.Background()) },
	})

	// run server
	err = srv.Run()
	if err != nil {
		logger.Fatal(err)
	}
}

Output with ctr+c:

$ go run main.go 
INFO[0000] starting services                            
DEBU[0000] starting service http1                       
DEBU[0000] starting service http2                       
^CINFO[0003] shutting down services                       
DEBU[0003] shutting down service http2                  
DEBU[0003] shutting down service http1            

Documentation

Overview

Package serverd provides a component to create server-type applications, helping to manage the life cycle of services and operating system signals.

This package is a work in progress and makes no API stability promises.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is the component that manages the services

func New

func New(opt ...Option) *Manager

New creates a new manager

func (*Manager) Ping

func (m *Manager) Ping() error

Ping will ping all registered services.

func (*Manager) Register

func (m *Manager) Register(svc Service) error

Register resgisters a new service in the manager. Services cannot be registered if it has already been started.

func (*Manager) Reload

func (m *Manager) Reload() error

Reload will reload all registered services.

func (*Manager) Run

func (m *Manager) Run() error

Run will initialize all services, install the operating system's signal handlers and block waiting for the shutdown signal. When this signal arrives, it will turn off all registered services.

func (*Manager) Shutdown

func (m *Manager) Shutdown()

Shutdown will stop all services in the opposite order to which they were registered. It will try to execute the "shutdown" function and, if it is turned off in a given time, it will execute the "stop" function of the service.

func (*Manager) Start

func (m *Manager) Start() error

Start starts managed services in the same order they were registered. If there is an error then it will stop.

type Option

type Option func(*options)

Option is used for component configuration

func SetLogger

func SetLogger(l yalogi.Logger) Option

SetLogger option allows set a custom logger

func ShutdownTimeout

func ShutdownTimeout(d time.Duration) Option

ShutdownTimeout sets timeout for shutdowns

type PingFn

type PingFn func() error

PingFn sets definition for ping functions

type ReloadFn

type ReloadFn func() error

ReloadFn sets definition for reload functions

type Service

type Service struct {
	Name     string
	Start    StartupFn
	Shutdown ShutdownFn
	Stop     StopFn
	Reload   ReloadFn
	Ping     PingFn
}

Service defines data struct for services

type ShutdownFn

type ShutdownFn func()

ShutdownFn sets definition for gracely shutdown functions

type StartupFn

type StartupFn func() error

StartupFn sets definition for startup functions

type StopFn

type StopFn func()

StopFn sets definition for stop functions (force)

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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