goservicing

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 9 Imported by: 0

README

goservicing

Build Status GoDevDoc Code lines Comments

Package to manage services start and graceful shutdown synchronize.

Example
package main

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

	"github.com/dohernandez/goservicing"
)

// service implementing the interface goservicing.Service
type service struct {
	// use mainly to log the start of the service and in case the failure shutdown.
	name string
	addr string

	shutdownSignal <-chan struct{}
	shutdownDone   chan<- struct{}

	srv *http.Server
}

func NewService() *service {
	return &service{
		name: "Service",
		addr: "8080",
	}
}

// WithShutdownSignal adds channels to wait for shutdown and to report shutdown finished.
func (s *service) WithShutdownSignal(shutdown <-chan struct{}, done chan<- struct{}) goservicing.Service {
	s.shutdownSignal = shutdown
	s.shutdownDone = done

	return s
}

// Name Service name.
func (s *service) Name() string {
	return s.name
}

// Addr service address.
func (s *service) Addr() string {
	return s.addr
}

// Start begins listening and serving.
func (s *service) Start() error {
	s.handleShutdown()

	router := http.NewServeMux()

	router.HandleFunc("/test", func(res http.ResponseWriter, req *http.Request) {
		res.WriteHeader(200)
		res.Write([]byte("Test is what we usually do"))
	})

	s.srv = &http.Server{
		Handler: router,
		Addr:    fmt.Sprintf(":%s", s.addr),
	}

	return s.srv.ListenAndServe()
}

// handleShutdown will handle the shutdown signal that comes to the server
// and shutdown the server.
func (s *service) handleShutdown() {
	if s.shutdownSignal == nil {
		return
	}

	go func() {
		<-s.shutdownSignal

		if err := s.srv.Shutdown(context.Background()); err != nil {
			_ = s.srv.Close() // nolint: errcheck
		}

		close(s.shutdownDone)
	}()
}

func main() {
	srv := NewService()

	servicing := &goservicing.ServiceGroup{}

	err := servicing.Start(
		context.Background(),
		time.Minute,
		func(ctx context.Context, msg string) {
			log.Println(msg)
		},
		srv,
	)

	log.Fatalln(err)
}

Documentation

Overview

Package goservicing provides synchronization, error propagation, and Context cancellation for groups of services

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GracefulShutdownFunc

type GracefulShutdownFunc func(ctx context.Context)

GracefulShutdownFunc function used to close all opened connections gracefully.

type Service

type Service interface {
	WithShutdownSignal(shutdown <-chan struct{}, done chan<- struct{}) Service
	Start() error
	Name() string
	Addr() string
}

Service is an interface used by ServiceGroup.

type ServiceGroup

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

ServiceGroup manages services start and graceful shutdown synchronize.

func WithGracefulSutDown

func WithGracefulSutDown(gracefulShutdownFuncs ...GracefulShutdownFunc) *ServiceGroup

WithGracefulSutDown returns a new ServiceGroup with GracefulShutdownFunc functions.

func (*ServiceGroup) Close

func (sg *ServiceGroup) Close() (err error)

Close invokes services to termination.

func (*ServiceGroup) Start

func (sg *ServiceGroup) Start(ctx context.Context, timeout time.Duration, log func(ctx context.Context, msg string), srvs ...Service) error

Start starts services synchronize and blocks until all services finishes by a notify signal.

Returns error in case any of the services fail to starting or fail to shut down.

Jump to

Keyboard shortcuts

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